Data Types in C

C language supports four fundamental data types:

  1. int
  2. char
  3. float
  4. double

int  - It is used to store integer values like 1  , -99  , 1000  .
char  - It is used to store single character like 'a'  , 'b' , 'z'  .
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 float type.

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:

  1. Size qualifiers - short  , long
  2. Sign qualifiers - signed  , unsigned

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

When the 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 int and char.

short and long

When 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 char and float.

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 -32768  to 32767  (i.e 2  bytes). For new 32-bit compilers like GCC and MSVC (stands for Microsoft Visual C++ ) , the integer range is -2147483648  to 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 -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 signed and unsigned but how char can have a sign ?

Why the character type has integer range ? It should have character range like 'a'  to 'z' .

The answers are discussed in the next chapter.