C Tutorial C References

C - sizeof() Operator



The C sizeof() operator returns the size of data type, constants, variable. It is a compile-time operator as it returns the size of any variable or a constant at the compilation time. It can also be used to get the size of structures, unions and any other user-defined data type.

Syntax

//returns the size in bytes of the 
//object representation of type.
sizeof(type)

//returns the size in bytes of the object 
//representation of the type of expression, 
//if the expression is evaluated.
sizeof expression

Return Value

Returns the size in bytes of the object representation of type or type of expression.

Example:

The example below shows the usage of sizeof() operator.

#include <stdio.h>

int main (){
  int x = 10;
  int A[10];
  int * ptr;
  ptr = A;

  //displaying size in bytes of 
  //various object representation 
  printf("Size of int : %ld\n", sizeof(int));
  printf("Size of float : %ld\n", sizeof(float));
  printf("Size of double : %ld\n", sizeof(double));
  printf("Size of long double : %ld\n", sizeof(long double));  
  printf("Size of variable x : %ld\n", sizeof(x));
  printf("Size of array A : %ld\n", sizeof A);
  printf("Size of pointer ptr : %ld\n", sizeof &ptr);

  return 0;
}

The output of the above code will be:

Size of int : 4
Size of float : 4
Size of double : 8
Size of long double : 16
Size of variable x : 4
Size of array A : 40
Size of pointer ptr : 8

❮ C - Operators