Data types, their sizes in C programming

Do you want to learn C programming from scratch? Then you are at right place. In this post, data types and their sizes in C programming will be discussed in detail.

In C programming, before using any variable, the programmer has to declare the data type it is going to hold in it.

For example, if a variable is going to store a value which is integer then before using it, its data type has to be set as int. eg: int  value;

Here value is the name of the variable which can be anything whereas, int define the data type it is going to hold. Thus, in the example, variable ‘value’ can store integer value.

Program to assign various data types

Example program to assign various data types in C programming

#include <stdio.h>

void main()
{
char a = ‘S’;
short b = 123;
short c = -123;
int d = 1600;
int e = -1600;
long f = 12345;
long g = -12345;
float h = 10.552;
float i = -10.552;
double j = 11.1234567890;
return;
}

Data types in C programming

Broadly speaking, there are four basic data types in C programming. They are

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

char

char data type in C programming is used to store one character. Its size is a byte.

Example: char c= ‘u’;

Here c is a variable of char type that stores character ‘u’ and its size is one byte or 8 bits.

int

This data type is used to store integer. Its size can be either two or four bytes that depend on size of the integer on the machine where the program is running.

Example: int n= 5;

In this example, n is a variable of integer type and stores the value 5.

Below is the program to find the exact size of the integer data type in C programming on your mach ine. It also give the maximum and minimum number that can be stored in integer data type on your computer.

#include <stdio.h>

#include <limits.h>

void main()
{
printf(“Size for int = %d \n”, sizeof(int));
printf(“Maximum integer number allowed = %d\n”, INT_MAX);
printf(“Minimum integer number allowed = %d\n”, INT_MIN);
return;
}

The output of the above program on my machine is as follow

Size for int = 4
Maximum integer number allowed = 2147483647
Minimum integer number allowed = -2147483648

float and double

They are data types that are single precision and double precision floating point respectively. The storage sizes required for float and double are four and 8 bytes respectively.

Program to find size of data types in C

Below is the program to find size of data types in c programming. The program prints the size of char, int, float and double data types.

#include <stdio.h>

#include<limit.h>

#include<float.h>

void main()
{
printf(“Size for char= %ld \n”, sizeof(char));
printf(“Size for short= %ld \n”, sizeof(short));
printf(“Size for int= %ld \n”, sizeof(int));
printf(“Size for long= %ld \n”, sizeof(long));
printf(“Size for float= %ld \n”, sizeof(float));
printf(“Size for double= %ld \n”, sizeof(double));
return;
}

Output of the program is as follows

Size for char= 1
Size for short= 2
Size for int= 4
Size for long= 8
Size for float= 4
Size for double= 8

If you are beginner and unable to understand the programs given above, don’t worry, at the end of the tutorial series, you will not only be able to understand the program but will be able to improve the code further as per your need.

Escape sequences in C programming language explained