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.
sizeof(type - name)chevron_rightfilter_noneLet’s look at the code:
C
#include <stdio.h>intmain(){printf("%lu\n",sizeof(char));printf("%lu\n",sizeof(int));printf("%lu\n",sizeof(float));printf("%lu",sizeof(double));return0;}chevron_rightfilter_noneC++
#include <iostream>usingnamespacestd;intmain(){cout <<sizeof(char)<<"\n";cout <<sizeof(int)<<"\n";cout <<sizeof(float)<<"\n";cout <<sizeof(double)<<"\n";return0;}chevron_rightfilter_noneOutput:1 4 4 8
- expression: The expression can be specified with or without the parentheses.
// First typesizeofexpression// Second typesizeof(expression)chevron_rightfilter_noneThe 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>intmain(){inti = 5;intint_size =sizeof(i++);// Displaying the size of the operandprintf("\n size of i = %d", int_size);// Displaying the value of the operandprintf("\n Value of i = %d", i);getchar();return0;}chevron_rightfilter_noneC++
#include <iostream>usingnamespacestd;intmain(){inti = 5;intint_size =sizeof(i++);// Displaying the size of the operandcout <<"\n size of i = "<< int_size;// Displaying the value of the operandcout <<"\n Value of i = "<< i;return0;}// This code is contributed by SHUBHAMSINGH10chevron_rightfilter_noneOutput:size of i = 4 Value of i = 5
References:
http://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#The-sizeof-Operator
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Is sizeof for a struct equal to the sum of sizeof of each member?
- Difference between sizeof(int *) and sizeof(int) in C/C++
- Evaluation order of operands
- Order of operands for logical operators
- sizeof operator in C
- G-Fact 1 | (Sizeof is an operator)
- Implement Your Own sizeof
- Do not use sizeof for array parameters
- How to find size of array in C/C++ without using sizeof ?
- sizeof() for Floating Constant in C
- Difference between strlen() and sizeof() for string in C
- Anything written in sizeof() is never executed in C
- Why does sizeof(x++) not increment x in C?
- vector::operator= and vector::operator[ ] in C++ STL
- deque::operator= and deque::operator[] in C++ STL
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- Copy constructor vs assignment operator in C++
- Self assignment check in assignment operator
- Rules for operator overloading

