Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does sizeof(x++) not increment x?

Tags:

c

sizeof

Here is the code compiled in dev c++ windows:

#include <stdio.h>  int main() {     int x = 5;     printf("%d and ", sizeof(x++)); // note 1     printf("%d\n", x); // note 2     return 0; } 

I expect x to be 6 after executing note 1. However, the output is:

4 and 5 

Can anyone explain why x does not increment after note 1?

like image 960
Neigyl R. Noval Avatar asked Nov 22 '11 11:11

Neigyl R. Noval


People also ask

Why is sizeof () an operator and not a function?

sizeof operator is compile time entity not runtime and don't need parenthesis like a function. When code is compiled then it replace the value with the size of that variable at compile time but in function after function gets execute then we will know the returning value.

What does sizeof return?

Answer: sizeof returns the size of the type in bytes.

Why does sizeof int return 4?

On a 32-bit Machine, sizeof(int*) will return a value 4 because the address value of memory location on a 32-bit machine is 4-byte integers. Similarly, on a 64-bit machine it will return a value of 8 as on a 64-bit machine the address of a memory location are 8-byte integers.

What is sizeof () operator?

The sizeof operator yields the size in bytes of the operand, which can be an expression or the parenthesized name of a type.


1 Answers

From the C99 Standard (the emphasis is mine)

6.5.3.4/2

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

like image 138
pmg Avatar answered Oct 03 '22 19:10

pmg