Skip to content
Home » Programming » C Examples » C program to find the size of data types using sizeof operator

C program to find the size of data types using sizeof operator

C Program to find the size of data types using sizeof operator

In this example, we will find the size of the data types using sizeof operator in C.

We will rely on sizeof operator to find the size of the data type. The sizeof operator is a unary operator. This operator is used to find the size of the operand. It can be used to find the size of all types of data types like, int, char, structure, array, etc.

For example:

Value = sizeof(int) // this will give the size of the int data type

Note:

  • The sizeof data operator actually returns the amount of memory which is allocated for the provided data type, variable or the expression.
  • The value returned will be in bytes.
  • The output may vary according to the compiler used.

Program to find the size of data type using sizeof operator

#include<stdio.h>

int main() {

     // find size of the data types
     printf("Size of int: %ld bytes\n", sizeof(int));
     printf("Size of float: %ld bytes\n", sizeof(float));
     printf("Size of double: %ld bytes\n", sizeof(double));
     printf("Size of char: %ld byte\n", sizeof(char));

     return 0;
}

Output

Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte

Explanation

  • Here, we are using the sizeof operator to find the size of the data type. As you can see we are passing the data type itself to find the size.
sizeof(int)
sizeof(float)
sizeof(double)
sizeof(char)

Here the value returned by them is the size (in bytes) which a variable will hold if they are declared of the same data type.

The value returned by sizeof(int) is 4 bytes. This means that when a variable of data type int is declared it occupies 4 bytes.

Also Read: C Program to Swap two numbers without Using a Temporary Variable

Program to find the size of variable/expression using sizeof operator

Also, we can find the size of a variable or expression. In this case, sizeof operator will return the size of the variable/expression.

#include<stdio.h>

int main() {
     int intVar = 20;
     char charVar = 'c';
     float floatVar = 20.3;
     double doubleVar = 54.20;

     // find size of the variable
     printf("Size of intVar variable: %ld bytes\n", sizeof(intVar));
     printf("Size of floatVar variable: %ld bytes\n", sizeof(floatVar));
     printf("Size of doubleVar variable: %ld bytes\n", sizeof(doubleVar));
     printf("Size of charVar variable: %ld byte\n", sizeof(charVar));

     // find size of the expression
     printf("Size of a expression: %ld bytes\n", sizeof(intVar + doubleVar));

     return 0;
}

Output

Size of intVar variable: 4 bytes
Size of floatVar variable: 4 bytes
Size of doubleVar variable: 8 bytes
Size of charVar variable: 1 byte
Size of a expression: 8 bytes

Explanation

  • Formerly, we have checked the size of data types. Now, we will be finding the size of variable/expression using sizeof operator.
sizeof(intVar)
sizeof(floatVar)
sizeof(doubleVar)
sizeof(charVar)

Here sizeof will give the size of the variable it will hold in the memory. sizeof(intVar) gives the value 4 (which is in bytes) because the intVar holds 4 bytes in the memory.

  • Similarly, we can also find the size of the whole expression using this operator. It will give the highest size of the variables involved in the expression.
sizeof(intVar + doubleVar)

It gives 8 bytes which is the size of the doubleVar variable. Here, the size occupied by the doubleVar is higher than the other variable i.e. intVar so, it gives the size of the doubleVar.

More Examples

C Program to check whether a number is positive, negative or zero
C program to find ASCII value of a character

Leave a Reply

Your email address will not be published. Required fields are marked *