



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of data type modifiers and type qualifiers in C++. Data type modifiers include signed, unsigned, long, and short, which can be applied to integer and char base types. Type qualifiers provide additional information about variables and include const, volatile, restrict, and storage classes such as auto, register, static, extern, and mutable.
Typology: Lecture notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




C++ allows the char, int, and double data types to have modifiers preceding them. A modifier is used to alter the meaning of the base type so that it more precisely fits the needs of various situations. The data type modifiers are listed here − signed unsigned long short The modifiers signed, unsigned, long, and short can be applied to integer base types. In addition, signed and unsigned can be applied to char, and long can be applied to double. The modifiers signed and unsigned can also be used as prefix to long or short modifiers. For example, unsigned long int. C++ allows a shorthand notation for declaring unsigned, short, or long integers. You can simply use the word unsigned, short, or long, without int. It automatically implies int. For example, the following two statements both declare unsigned integer variables. unsigned x; unsigned int y; To understand the difference between the way signed and unsigned integer modifiers are interpreted by C++, you should run the following short program − #include
The above result is because the bit pattern that represents 50,000 as a short unsigned integer is interpreted as -15,536 by a short.
The type qualifiers provide additional information about the variables they precede. Sr.N o Qualifier & Meaning 1 const Objects of type const cannot be changed by your program during execution. 2 volatile The modifier volatile tells the compiler that a variable's value may be changed in ways not explicitly specified by the program. 3 restrict A pointer qualified by restrict is initially the only means by which the object it points to can be accessed. Only C99 adds a new type qualifier called restrict.
// Function declaration void func(void); static int count = 10 ; /* Global variable */ main() { while(count--) { func(); } return 0 ; } // Function definition void func( void ) { static int i = 5 ; // local static variable i++; std::cout << "i is " << i ; std::cout << " and count is " << count << std::endl; } When the above code is compiled and executed, it produces the following result − i is 6 and count is 9 i is 7 and count is 8 i is 8 and count is 7 i is 9 and count is 6 i is 10 and count is 5 i is 11 and count is 4 i is 12 and count is 3 i is 13 and count is 2 i is 14 and count is 1 i is 15 and count is 0
The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined. When you have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding extern is used to declare a global variable or function in another file. The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.
#include
int count ; extern void write_extern(); main() { count = 5 ; write_extern(); }
#include
The mutable specifier applies only to class objects, which are discussed later in this tutorial. It allows a member of an object to override const member function. That is, a mutable member can be modified by a const member function.