OverIQ.com

Data Types in C

Last updated on September 24, 2020


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.4E-4932 to 3.4E+4932

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**************************************************
 Program to determine size and range of data types
***************************************************/

#include<stdio.h> // include stdio.h library
#include<limits.h>
#include<float.h>

int main(void)
{    
    printf("%30s %12s %28s\n", "", "Size", "Range");

    printf("%-30s %10lu %25d - %d\n", "char or signed char", sizeof(char), CHAR_MIN, CHAR_MAX);
    printf("%-30s %10lu %25d - %d\n", "unsigned char", sizeof(unsigned char), 0, UCHAR_MAX);
    printf("%-30s %10lu %25d - %d\n", "int or signed int", sizeof(int), INT_MIN, INT_MAX);
    printf("%-30s %10lu %25d - %ud\n", "unsigned int", sizeof(unsigned int), 0, UINT_MAX);
    printf("%-30s %10lu %25hd - %hd\n", "short int or short signed int", sizeof(short int), SHRT_MIN, SHRT_MAX);
    printf("%-30s %10lu %25d - %d\n", "unsigned short int", sizeof(unsigned short int), 0, USHRT_MAX);
    printf("%-30s %10lu %25ld - %ld\n", "long int or signed long int", sizeof(long int), LONG_MIN, LONG_MAX);    
    printf("%-30s %10lu %25d - %lu\n", "unsigned long int", sizeof(unsigned long int), 0, ULONG_MAX);

    printf("%-30s %10lu %25le - %le\n", "float", sizeof(float), FLT_MIN, FLT_MAX);
    printf("%-30s %10lu %25le - %le\n", "double", sizeof(double), DBL_MIN, DBL_MAX);
    printf("%-30s %10lu %25Le - %Le\n", "long double", sizeof(long double), LDBL_MIN, LDBL_MAX);

    return 0; // return 0 to operating system
}

Try it now

Expected Output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Size                        Range
char or signed char                     1                      -128 - 127
unsigned char                           1                         0 - 255
int or signed int                       4               -2147483648 - 2147483647
unsigned int                            4                         0 - 4294967295d
short int or short signed int           2                    -32768 - 32767
unsigned short int                      2                         0 - 65535
long int or signed long int             8      -9223372036854775808 - 9223372036854775807
unsigned long int                       8                         0 - 18446744073709551615
float                                   4              1.175494e-38 - 3.402823e+38
double                                  8             2.225074e-308 - 1.797693e+308
long double                            16            3.362103e-4932 - 1.189731e+4932

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

! 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.