Home arrow C programming arrow Data Types, Variables, and Constants

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
Data Types, Variables, and Constants PDF Print E-mail
Written by Hemanshu Patel   
Friday, 19 October 2007
Article Index
Data Types, Variables, and Constants
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8

Type Qualifiers

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

const

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.

volatile

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.>

restrict

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++.




 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits