Data Types in C
C language supports four fundamental data types:
int - It is used to store integer values like
char - It is used to store single character like
float - It is used to store single precision floating point number.
double - It is used to store a double precision floating point number.
double type provides more precision than
float type. It simply means that double type provides more digits to the right of decimal point than
C also have has something called type qualifiers which you can apply to these basic data types to get some more types.
Two types of qualifiers:
- Size qualifiers -
- Sign qualifiers -
The reason behind providing these qualifiers is that programmer can precisely choose a range of numbers suitable for a program whenever possible, which makes the program even more efficient.
signed and unsigned
unsigned qualifier is used the number is always positive, and when signed is used number may be positive or negative. If the qualifier is not mentioned then
signed qualifier is assumed. The
unsigned qualifier is commonly used when we know in advance that the number will always be positive. Always remember the range of values of
signed data type is always less than the range of values of an unsigned type. These qualifiers can only be used with
short and long
short qualifier is used the range of a type is reduced, on the other hand, use of
long qualifier increases the range of the type. int type can use both qualifiers, double can only use
long . They can't be used with
Unlike languages like Java, C# where the size of data type is fixed, in C the range of data types entirely compiler dependent. For old 16-bit compiler like Turbo C/C++ the range of integer constant is
2 bytes). For new 32-bit compilers like GCC and MSVC (stands for Microsoft Visual C++ ) , the integer range is
2147483647 (i.e 4 bytes).
The following table represents sign and range of different data types for 32 - bit machine.
|Data Types||Data Types with qualifiers||Size||Range|
|char||char or signed char||1||-128 to 127|
|unsigned char||1||0 to 255|
|int||int or signed int||4||-2147483648 to 2147483647|
|unsigned int||4||0 to 4294967295|
|short int or short signed int||2||-32768 to 32767|
|unsigned short int||2||0 to 65535|
|long int or signed long int||4||-2147483648 to 2147483647|
|unsigned long int||4||0 to 4294967295|
|float||float||4||3.4E-38 to 3.4E+38|
|double||double||8||1.7E-308 to 1.7E+308|
|long double||10||3.4E-4932 to 3.4E+4932|
Take a closer look at the first two rows of the table and try to answer the following two questions.
It is obvious that
int can be
unsigned but how char can have a sign ?
Why the character type has integer range ? It should have character range like
The answers are discussed in the next chapter.