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.

The double type provides more precision than the float type. It simply means that the double type provides more digits to the right of decimal point than the float type. To be precise, the float provides 6 digits of precision, while the double provides 14 digits of precision.

It is important to note that the float and double represents the same type – floating point numbers. The only difference is in the number of precision.

The C language also 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 qualifiers

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. The range of values of signed data type is always less than the range of values of an unsigned type. Further, these qualifiers can only be used with int and char types.

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 the data type is fixed. In C, the size of the data type is machine dependent. For an old 16-bit machine, the size of int is 2 bytes. Since 2 bytes equals 2*8=16 bits, on 16-bit machine an int can take on values from -32768 to 32767.

If, on the other hand, you are on a 32-bit or 64-bit machine, then the size of int is 4 bytes. In other words, on 32-bit or 64-bit system, the int can take on values from -2147483648 to 2147483647.

The following table shows size and range of different data types on a 32-bit machine.

Data Types Data Types with qualifiers Size (in byte) 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 1.1754e-38 to 3.4028e+38
double double 8 2.2250e-308 to 1.7976e+308
long double 10 3.3621e-4932 to 1.1897e+4932

To determine the range and size of different types on your system, run the follwoing program:

Expected Output:

Note 1: The preceding output is from a 64 bit machine and may vary depending upon your system.

Note 2: The headers files limits.h and float.h defines some symbolic constants to represent the smallest and largest values of integers and floats respectively.

Don’t get into the details of how this program works. We will discuss everything in great detail in the upcoming lessons.

You might be wondering if char type is used to characters why it has an integer range.

The answer is – internally characters are represented using numbers. We are discussing this topic in detail in the next lesson.

Quiz - Data Types in C

Test your knowledge about Data Types in C.

Leave a Comment

%d bloggers like this: