C Qualifiers
C Qualifiers
Qualifiers alters the meaning of base data types to yield a new data type.
Size qualifiers
Size qualifiers alters the size of a basic type. There are two size qualifiers, long and short. For example:
long double i;
The size of double is 8 bytes. However, when long keyword is used, that variable becomes 10 bytes.
Learn more about long keyword in C programming.
There is another keyword short which can be used if you previously know the value of a variable will always be a small number.
Sign qualifiers
Integers and floating point variables can hold both negative and positive values. However, if a variable needs to hold positive value only, unsigned data types are used. For example:
// unsigned variables cannot hold negative value unsigned int positiveInteger;
There is another qualifier signed which can hold both negative and positive only. However, it is not necessary to define variable signed since a variable is signed by default.
An integer variable of 4 bytes can hold data from -231 to 231-1. However, if the variable is defined as unsigned, it can hold data from 0 to 232-1.
It is important to note that, sign qualifiers can be applied to int and char types only.
Constant qualifiers
An identifier can be declared as a constant. To do so const keyword is used.
const int cost = 20;
The value of cost cannot be changed in the program.
Volatile qualifiers
A variable should be declared volatile whenever its value can be changed by some external sources outside the program. Keyword volatile is used for creating volatile variables.
Comments
Post a Comment