Datatypes in C

            Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Let us briefly describe them one by one:

Following are the examples of some very common data types used in C:

Char : The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
int : As the name suggests, an int variable is used to store an integer.
Float : It is used to store decimal numbers (numbers with floating point value) with single precision.
Double : It is used to store decimal numbers (numbers with floating point value) with double precision. 
         Different data types also have different ranges upto which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.
 

Data Type 
  Memory (bytes) 
  Range 
  Format Specifier 
 
Data type : short int 
 Memory :  2 
   Range :  -32,768 to 32,767 
 Format specifier :  %hd 
 
Data type : unsigned short int 
Memory :   2 
  Range : 0 to 65,535 
 Format specifier :  %hu 
 
 Data type : unsigned int 
  Memory : 4 
Range :   0 to 4,294,967,295 
Format specifier :   %u 
 
Data type : int 
Memory :   4 
  Format specifier :  -2,147,483,648 to 2,147,483,647 
Format specifier :   %d 
 
Data type : long int 
Memory :   4 
Range :   -2,147,483,648 to 2,147,483,647 
 Format specifier :  %ld 
 
Data type : unsigned long int 
  Memory : 4 
 Range :  0 to 4,294,967,295 
Format specifier  :   %lu 
 
Data type : long long int 
Memory :   8 
 Range :  -(2^63) to (2^63)-1 
Format specifier :   %lld 
 
Data type : unsigned long long int 
 Memory :  8 
Range :   0 to 18,446,744,073,709,551,615 
Format specifier :   %llu 
 
Data type : signed char 
Memory :   1 
Range :   -128 to 127 
Format specifier :   %c 
 
Data type : unsigned char 
 Memory :  1 
  Range : 0 to 255 
  Format specifier : %c 
 
Data type : float 
Memory :   4 
Format specifier :  %f 
 
Data type : double 
Memory :   8 
  Format specifier :   %lf 
 
Data type : long double 
 Memory :  16 
  Format specifier :   %Lf 
 
We can use the sizeof() operator to check the size of a variable. See the following C program for the usage of the various data types:
 
#include <stdio.h>
int main()
{
    int a = 1;
    char b = 'G';
    double c = 3.14;
    printf("Hello World!\n");
 
    // printing the variables defined
    // above along with their sizes
    printf("Hello! I am a character. My value is %c and "
           "my size is %lu byte.\n",
           b, sizeof(char));
    // can use sizeof(b) above as well
 
    printf("Hello! I am an integer. My value is %d and "
           "my size is %lu bytes.\n",
           a, sizeof(int));
    // can use sizeof(a) above as well
 
    printf("Hello! I am a double floating point variable."
           " My value is %lf and my size is %lu bytes.\n",
           c, sizeof(double));
    // can use sizeof(c) above as well
 
    printf("Bye! See you soon. :)\n");
 
    return 0;
}
Output : 

Hello World!
Hello! I am a character. My value is G and my size is 1 byte.
Hello! I am an integer. My value is 1 and my size is 4 bytes.
Hello! I am a double floating point variable. My value is 3.140000 and my size i
s 8 bytes.
Bye! See you soon. :)

 
 







Posted on by