Variables constantes en c

Páginas: 29 (7036 palabras) Publicado: 21 de marzo de 2011
RETURN

Using the const Qualifier... Properly! Dan Saks

ABSTRACT The const qualifier has various uses in C and C++. You can use it to define symbolic constants, to declare data that can be placed in read-only memory, or to constrain the effects of function calls. Judicious use of the const qualifier turns logic errors that would be run-time bugs into compile-time errors that are much easierto correct. Using the const qualifier can make your programs more portable. Occasionally, it can even reduce your program’s code size and execution time. This session covers the most common uses for the const qualifier in C and C++, and what const really means in each context. It also suggests guidelines for when to use const, and when not to use it.

SYMBOLIC CONSTANTS C programmers typicallydefine symbolic constants as macros: #define MAX 16 ... int a[MAX]; Unfortunately, some compilers don’t preserve macro names among the symbols they pass on to their debuggers. Even worse, macros do not observe the scope rules. For example, you can’t restrict a macro to a local scope: void foo() { #define MAX 16 int a[MAX]; ... }

// non-local

Consequently, macros might substitute in places youdon’t want them to. For example, after macro substitution:

#define max 16 ... void sort(int a[], size_t max); becomes: void sort(int a[], size_t 16); // syntax error

Inadvertent macro substitution often produces a diagnostic, albeit a puzzling one. Most programmers avoid name collisions between macros and other identifiers by adhering to a style convention, such as spelling macros namesentirely in UPPERCASE. Both C and C++ offer alternatives that avoid the ill-effects of macros. One alternative is enumeration constants. The general form of an enumeration is: enum E { A = M, B = N, ... }; most of which (the underlined part) is optional. This allows for a useful special case, as in: enum { max = 16 }; which defines max as an integer- valued constant. An enumeration constant is acompile-time constant, so you can use it as an array dimension: int a[max]; or as a case label. Enumeration constants obey the scope rules. However, enumeration constants are integers, so you can’t use them for floating constants. enum { pi = 3.1415926 }; // truncates pi to 3 // OK

Such truncation often produces a warning from the compiler. In C++ (but not C), const objects are yet another option:int const max = 16; ... int a[max];

// OK in C++, not C

In C, a const object in file scope has external linkage by default (as if declared with the keyword extern). In C++, a const object in file (or any namespace) scope has internal linkage by default (as if declared with the keyword static). /* file1.c */ int const max; /* file2.c */ int const max = 16;

/* OK */

/* OK */

In C,the definition for max in file2.c satisfies the reference to max in file1.c. // file1.cpp int const max; // file2.cpp int const max = 16;

// error

// OK

In C++, the definition for max (in file1.cpp) is in error because it must have an initializer. An enumeration constant is an rvalue (it is not addressable): enum { max = 16 }; ... p = &max;

// error

A const object is an lvalue (it isaddressable): int const max = 16; ... p = &max;

// OK

Although it is an lvalue, a const object is a non-modifiable lvalue: int int ... n = max const max = 16; n; max; = n; // OK // error

In other words, the program cannot change the value of a const object, which is as it should be.

Since a const object is addressable, the compiler may generate storage to hold a copy of the constobject. In fact, C compilers always generate that storage. If a C++ compiler can determine that the program never needs the storage for a particular const object, it need not allocate storage for that object. Rarely do you want the compiler generating storage for symbolic constants, but you usually want symbols to obey the scope rules. • When defining symbolic constants, prefer const objects to...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Variables Constantes y Estaticas C#
  • Variables y constantes en c
  • Constantes Y Variables
  • Variables y constantes
  • variables y constantes
  • Constantes Y Variables
  • CONSTANTES Y VARIABLES
  • Variables en c#

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS