The sizeof operator is used to return the size of its operand, in bytes. This operator always precedes its operand. The operand either may be a data-type or an expression. Let’s look at both the operands through proper examples.
- type-name: The type-name must be specified in parentheses.
Let’s look at the code:
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;
}
|
- expression: The expression can be specified with or without the parentheses.
sizeof expression
sizeof (expression)
|
The expression is used only for getting the type of operand and not evaluation. For example, below code prints value of i as 5 and the size of i a
C
#include <stdio.h>
int main()
{
int i = 5;
int int_size = sizeof (i++);
printf ( "\n size of i = %d" , int_size);
printf ( "\n Value of i = %d" , i);
getchar ();
return 0;
}
|
C++
#include <iostream>
using namespace std;
int main()
{
int i = 5;
int int_size = sizeof (i++);
cout << "\n size of i = " << int_size;
cout << "\n Value of i = " << i;
return 0;
}
|
Output:
size of i = 4
Value of i = 5
References:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#The-sizeof-Operator
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 May, 2020
Like Article
Save Article