Why does sizeof(x++) not increment x in C?
According to C99 Standards, the sizeof() operator only takes into account the type of the operand, which may be an expression or the name of a type (i.e int, double, float etc) and not the value obtained on evaluating the expression. Hence, the operand inside the sizeof() operator is not evaluated. It is evaluated only if the type of the operand is variable length array because in that case, the size can be determined only after the expression is evaluated.
Case 1:
When the sizeof() method is passed a fixed size structure:
In this case, the sizeof() operator does not evaluate the parameter. Only the type of the parameter is checked and its size is returned.
Below figure shows the workflow of sizeof() operator in this scenario:

Example 1:
The output value of x after increment is still 3 as compared to the expected value which is 4. This is because sizeof operator doesn’t need to evaluate the expression to obtain the size as the data type of the operand doesn’t change and hence the size remains the same.
#include <stdio.h> int main() { int x = 3; printf("%d\n", sizeof(x++)); printf("x = %d", x); return 0; } |
Output:
4 x = 3
Case 2:
When the sizeof() method is passed a variable size structure:
In this case, the sizeof() operator evaluates the parameter to see if there is any change of size. If found, then first the size is modified then the final size is returned.
Below example illustrate the sizeof() operator in this scenario:
Example 2:

In this example, the sizeof operator needs to evaluate the expression in order to calculate the size of the array which is shown in the figure. Hence, in this case, we get the value of x after incrementation.
#include <stdio.h> int main() { int x = 3; printf("%d\n", sizeof(int[x++])); printf("x = %d", x); return 0; } |
Output:
12 x = 4
Example 3:

Again in this example, the sizeof operator needs to evaluate the expression in order to calculate the size of the array which is shown in the figure. In this example, the post-increment operator is used and hence the output in the first line is 16. In the second line, the value of x is printed after incrementation.
#include <stdio.h> int main() { int x = 3; printf("%d\n", sizeof(int[++x])); printf("x = %d", x); return 0; } |
Output:
16 x = 4
Recommended Posts:
- Pre-increment and Post-increment in C/C++
- Pre-increment (or pre-decrement) in C++
- CSS | counter-increment Property
- Increment (Decrement) operators require L-value Expression
- Print matrix after applying increment operations in M ranges
- Count of suffix increment/decrement operations to construct a given array
- How to create Responsive Bottom Navigation Bar using Bootstrap ?
- How to save an HTML 5 Canvas as an image on the server ?
- How to accordion scroll to top to open content in Bootstrap ?
- PHP | Imagick paintTransparentImage() Function
- PHP | Imagick cycleColormapImage( ) Function
- PHP | Imagick liquidRescaleImage() Function
- PHP | Imagick implodeImage() Function
- PHP | Imagick affineTransformImage() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rituraj_jain



