◄Up► ◄Contents► ◄Index► ◄Back► ─────C/C++ Language───────────────────────────────────────────────────────── In both C and C++, the const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it. For example: const int i = 5; i = 10; // Error i++; // Error In C++, you can use the const keyword instead of the #define preprocessor directive to define constant values. Values defined with const are subject to type checking, and can be used in place of constant expressions. For example, in C++ you can specify the size of an array with a const variable as follows: const int maxarray = 255; char store_char[maxarray]; // Legal in C++; illegal in C In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files. The const keyword can also be used in pointer declarations. For example: char *const aptr = mybuf; // Constant pointer *aptr = 'a'; // Legal aptr = yourbuf; // Error const char *bptr = mybuf; // Pointer to constant data *bptr = 'a'; // Error bptr = yourbuf; // Legal You can use pointers to constant data as function parameters in order to prevent the function from modifying a parameter passed through a pointer. See: define -♦-