sizeof operator in C – GeeksforGeeks

 

Sizeof is a much used operator in the C or C++. It is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t. sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union etc.
Usage 
sizeof() operator is used in different way according to the operand type. 
 

 

1. When operand is a Data Type. 
When sizeof() is used with the data types such as int, float, char… etc it simply returns the amount of memory is allocated to that data types.
Let’s see example: 
 

Tóm Tắt

C




#include <stdio.h>

int main()

{

    printf("%lu\n", sizeof(char));

    printf("%lu\n", sizeof(int));

    printf("%lu\n", sizeof(float));

    printf("%lu", sizeof(double));

    return 0;

}



C++




#include <iostream>

using namespace std;

 

int main()

{

    cout << sizeof(char)<<"\n";

    cout << sizeof(int)<<"\n";

    cout << sizeof(float)<<"\n";

    cout << sizeof(double)<<"\n";

    return 0;

}



Output

1
4
4
8

Note: sizeof() may give different output according to machine, we have run our program on 32 bit gcc compiler.

2. When operand is an expression. 
When sizeof() is used with the expression, it returns size of the expression. Let see example: 
 

C




#include <stdio.h>

int main()

{

    int a = 0;

    double d = 10.21;

    printf("%lu", sizeof(a + d));

    return 0;

}



C++




#include <iostream>

using namespace std;

int main()

{

    int a = 0;

    double d = 10.21;

    cout << sizeof(a + d);

    return 0;

}



Output

8

As we know from first case size of int and double is 4 and 8 respectively, a is int variable while d is a double variable. The final result will be a double, Hence the output of our program is 8 bytes.

Type of operator

sizeof() is a compile time operator. compile time refers to the time at which the source code is converted to a binary code. It doesn’t execute (run) the code inside (). Lets see an example.

C++




#include <iostream>

 

using namespace std;

int main() {

 

 

 int y;

 

 int x = 11;

 

 y = sizeof(x++);

 

 cout<<y<<" "<<x;

 

}



C




 

#include <stdio.h>

 

int main(void)

{

   

  int y;

 

  int x = 11;

 

  y = sizeof(x++);

 

  printf("%i %i", y, x);

   

  return (0);

 

}

 



Output

4 11

If we try to increment the value of x, it remains the same. This is because, x is incremented inside the parentheses and sizeof() is a compile time operator.  

Need of Sizeof 
1. To find out number of elements in a array. 
Sizeof can be used to calculate number of elements of the array automatically. Let see Example : 
 

C




#include <stdio.h>

int main()

{

    int arr[] = { 1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14 };

    printf("Number of elements:%lu ", sizeof(arr) / sizeof(arr[0]));

    return 0;

}



C++




#include <iostream>

using namespace std;

 

int main()

{

    int arr[] = { 1, 2, 3, 4, 7, 98,

    0, 12, 35, 99, 14 };

    cout << "Number of elements: "

    <<(sizeof(arr) / sizeof(arr[0]));

    return 0;

}



Output

Number of elements:11 

2. To allocate a block of memory dynamically. 
sizeof is greatly used in dynamic memory allocation. For example, if we want to allocate memory for which is sufficient to hold 10 integers and we don’t know the sizeof(int) in that particular machine. We can allocate with the help of sizeof.
 

C




int* ptr = (int*)malloc(10 * sizeof(int));



References 
https://en.wikipedia.org/wiki/Sizeof
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 

My Personal Notes

arrow_drop_up