Data Types in C

Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities.

C language supports 2 different type of data types:

  1. Primary data types(primitive):

These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.

  1. Derived data types:

Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointer. These are discussed in details later.

Here we will study about primary or primitive datatype

Primary data types:

  • char: The most basic data type in C. It stores a single character and requires a single byte (8bits)of memory in almost all compilers.it can store an renage
  • int: As the name suggests, an int variable is used to store an integer.

The regular integer that we use has a size of 2 bytes (16 bits) on a 16-bit machine. However, most of the modern systems have 32 or 64-bit configurations. The size of an integer in such environment is 4 bytes.

  • float: It is used to store decimal numbers (numbers with floating point value) with single precision. Its size is 4bytes irrrespective of 32 or 64 bit
  • double: It is used to store decimal numbers (numbers with floating point value) with double precision. Its size is 8bytes irrrespective of 32 or 64 bit

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

TYPE OF DATA

MEMORY SIZE

RANGE

Int

Integer

2 Bytes

-32768 to 32767

Char

Character

1 Byte

-128 to 127

Float

Floating point number

4 Bytes

3.4e-38 to 3.4e+38

Double

Floating point number with higher precetion

8 Bytes

1.7e-308 to 1.7e+308

 

Program to check size of an datatypes:

#include <stdio.h>

int main()

{

          int a = 1;

          char b ='G';

          float f = 7.77;

          double c = 3.14;

          printf("Hello World!\n");

          printf("I am a character. My value is %c and "

          "my size is %lu byte.\n", b,sizeof(char));

          printf("I am an integer. My value is %d and "

          "my size is %lu bytes.\n", a,sizeof(int));

          printf("I am a float with single floating point variable."" My value is %f and     my size is %lu         bytes.\n",f,sizeof(float));

          printf("I am a double floating point variable."" My value is %lf and my size is %lu           bytes.\n",c,sizeof(double));

          printf("Bye! See you soon. :)\n");

          return 0;

}

Output of the program:

Hello World!

I am a character. My value is G and my size is 1 byte.

I am an integer. My value is 1 and my size is 4 bytes.

I am a float with single floating point variable. My       value is       7.770000 and my size is 4           bytes.

I am a double floating point variable. My value is         3.140000 and my size is 8       bytes.

Bye! See you soon. :)