In C or C++, increment and decrement operator (++n
, --n
) are not performed when it is in a sizeof()
operator.
int n = 100; int size_int = sizeof(++n); std::cout<<n;
I have written this code and run the program. Of course, I think 101 will be showed for me. But, n
was not 101, it was 100.
Why is that?
Pre-increment and Post-increment concept in C/C++?Decrement operator decrease the value by one. Pre-increment (++i) − Before assigning the value to the variable, the value is incremented by one. Post-increment (i++) − After assigning the value to the variable, the value is incremented.
The sizeof() operator allows a user to avoid the specification of the machine-dependent type of data sizes in any program. We generally use the sizeof() operator in the C language so that we can determine the total size of the data type or the expression that is specified in the storage units of char-size.
The int parameter is a dummy parameter used to differentiate between prefix and postfix versions of the operators. When the user-defined postfix operator is called, the value passed in that parameter is always zero, although it may be changed by calling the operator using function call notation (e.g., a.
In programming (Java, C, C++, JavaScript etc.), the increment operator ++ increases the value of a variable by 1. Similarly, the decrement operator -- decreases the value of a variable by 1.
In C++, the expressions inside sizeof
is not evaluated, except for C99's VLA as mentioned in comments, since this was earlier tagged for C too
The sizeof
operator is calculated at compile time.
Only the type of the expression (that is calculated at compile time) is then used by sizeof
.
From C++ Standard § 5.3.3 Sizeof
The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.
Some C++ compilers provide VLAs as an extension as commented below.
In C the operand of sizeof
is not evaluated at except for variable length arrays:
6.5.3.4. p2:
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
If you put n++
into a variable length array, for example:
int n = 1 ; sizeof( int(*)[n++] ) ;
then it is unspecified if the operand is evaluated.
6.7.6.2. p5
If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero. The size of each instance of a variable length array type does not change during its lifetime. Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With