Thursday, July 18, 2013

sizeof() operator in C

sizeof() operator is a unary operator used to find the size of the arrays or the size of a variable or size of structures in terms of bytes. If we use sizeof(variable), it returns the size taken by it in terms of bytes.

Syntax:
sizeof(variable_name);
Let us the application of the sizeof operator in the following example.

Example:

  1. #include<stdio.h>
  2. main()
  3. {
  4. int a=3;
  5. int b[2];
  6. printf("%d\n",sizeof(a));
  7. printf("%d\n",sizeof(b));
  8. return 0;

This program returns the output as

  • 2
  • 4
This is because, the variable a is of int type, hence it takes 2 bytes.
And the array b is of int type and the array size is 2. Hence it takes 4 bytes.