Type Qualifiers

The type qualifiers provide additional information about the variables they precede.


Objects of type const cannot be changed by your program during execution. Also, an object pointed to by a const pointer cannot be modified. The compiler is free to place variables of this type into read-only memory (ROM). A const variable will receive its value either from an explicit initialization or by some hardware-dependent means. For example,

const int a = 10;

will create an integer called a with a value of 10 that may not be modified by your program. It can, however, be used in other types of expressions.


The modifier volatile tells the compiler that a variable’s value may be changed in ways not explicitly specified by the program. For example, a global variable’s address may be passed to the clock routine of the operating system and updated with each clock tick. In this situation, the contents of the variable are altered without any explicit assignment statements in the program. This is important because compilers will sometimes automatically optimize certain expressions by making the assumption that the contents of a variable are unchanging inside an expression. This is done to achieve higher performance. The volatile modifier will prevent this optimization in those rare situations where this assumption is not the case.

Programming Tip 

If a class member function is modified by const, it cannot alter the object that invokes the function. To declare a const member function, put const after its parameter list. For example,

class MyClass {   int i; public:   // a const function   void f1(int a) const {     i = a; // Error! can't modify invoking object   }   void f2(int a) {    i = a; // OK, not const function   } }; 

As the comments suggest, f1( ) is a const function and it cannot modify the object that invokes it.


C99 adds a new type qualifier called restrict. This qualifier applies only to pointers. A pointer qualified by restrict is initially the only means by which the object it points to can be accessed. Access to the object by another pointer can occur only if the second pointer is based on the first. Thus, access to the object is restricted to expressions based on the restrict-qualified pointer. Pointers qualified by restrict are primarily used as function parameters, or to point to memory allocated via malloc( ). The restrict qualifier does not change the semantics of a program. restrict is not supported by C++.

C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net