C Programming

Sizeof Operator

Managing space complexity is the most crucial matter in C language; it is the more precious aspect in any language. For this we have to be aware of the time of coding in C language. sizeof () operator is the memory measuring operator by which we can measure any size of a variable or data type or any constant. It is a unary operator. It means it only takes one operand to operate on the data.

Size of () is an operator operates on three types of data. They are;

  • size of (data type)
  • size of (variable)
  • size of (constant)

(Size of operator tells us the size of a data type or a variable.)

Programming Example 1:

Here we show how sizeof () operator works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main ()

{

    int  x ;            // declaring a variable.
    x = sizeof ( float ) ;      // using size of operator.
    printf ( " %d ", x ) ;


return 0 ;

}

Output:

Explanation:

In this programming example we see how sizeof () operator measure the size of float data type. As we know, a float data type takes 4 bytes in the memory. When we pass the float data type inside the parenthesis of sizeof () operator, it returns 4. This 4 is assigned to the variable x with the help of assignment operator. So, the output of the program is 4.

Programming Example 2:

Here we show how sizeof () operator work.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main ()

{

    int  x, y ;         // declaring variables.
    x = sizeof ( y ) ;      // using sizeof operator.
    printf ( " %d ", x ) ;

return 0 ;

}

Output:

Explanation:

In this programming example we see how sizeof () operator measure the size of integer type of variable. As we know, an integer type of variable takes 4 bytes in the memory. When we pass the integer type variable inside the parenthesis of sizeof () operator, it returns 4. This 4 is assigned to the variable x with the help of assignment operator. So, the output of the program is 4.

Programming Example 3:

In this programming example we show another example of how sizeof () operator works on a character type of variable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>

int main ()

{

    int  x ;                // declaring variable.
    char  y ;
    x = sizeof ( y ) ;      // using sizeof operator.
    printf ( " %d \n ", x ) ;

return 0 ;

}

Output:

Explanation:

In this programming example we see how sizeof () operator measure the size of character type of variable. As we know, a character type of variable takes 1 bytes in the memory. When we pass the character type variable inside the parenthesis of sizeof () operator, it returns 1 and this 1 is assigned to the variable x with the help of assignment operator. So, the output of the program is 1.

Programming Example 4:

In this programming example we show another example of sizeof () operator works on constant.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>

int main ()

{
    int  x, y, z ;          // declaring variable.
    x = sizeof ( 34 ) ;     // using sizeof operator.
    y = sizeof ( 3.56 ) ;
    z = sizeof ( ' a ' ) ;
    printf ( " %d, %d, %d ", x, y , z ) ;
    printf ( "\n" ) ;
return 0 ;
    }

Output:

Explanation:

ASCII: American Standard Code for Information Interchange is a collection of code ranges between 0 and 255, which converts every character constant to integer constant.

  1. As integer constant consumes 4 bytes in memory so, the value of x = 4.

Real constant are by default double so, real constant consumes 8 bytes in memory.

  1. There is a difference between float and double. Float is single precession whereas double is double precession.

So, the accuracy of double is larger than accuracy of float. The size of double is larger than the size of float. For this double can store a point number more accurately compare to float. So, by default real constant are double. So, the value of y = 8.

  1. Character constant is also by default treated as integer character. Constant is not converted by the binary number. For ASCII coding technique exists.

For this every character has an ASCII code. The codes are 0 to 255 and it is represented as integers. For example: a->97 the size of 97 means the size of integer constant = 4.

So, the value of z = 4.

Programming Example 5:

Here we show more example of sizeof () operator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()

{

int  x = 81 ;       // declaring variables.
   printf ( " Size of variable: %d\n ", sizeof ( x ) ) ;        // using sizeof operator.
    printf ( " Size of float data type : %d\n ", sizeof ( float ) ) ;   // passing data type inside sizeof operator.
   printf ( " Size of char data type : %d\n ", sizeof ( char ) ) ;
   printf  (" Size of int data type : %d\n ", sizeof ( int ) ) ;
    printf ( " Size of double data type : %d\n ", sizeof ( double ) ) ;


return 0 ;

}

Output:

Explanation:

In this programming example we see how does sizeof () operator measure the size of character, integer, float and double types of variables. As we know, a character type of variable takes 1 byte. An integer type of variable takes 4 byte; a float type of variable takes 4 byte, and a variable takes 8 bytes in the memory when we pass the character type variable inside the parenthesis of sizeof () operator.

Conclusion:

In this article, we explained in details about the concept of a unary operator called sizeof () operator. From this discussion we have come to this conclusion that it is an important operator in C language. Through this operator we can measure different types of data which is very important in terms of space management in C language.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com