#include <stdio.h>
int main()
{
int number = 10;
printf("Number is: %d\n", number);
printf("Sizeof of number is: %d\n", sizeof(number++));
printf("Number now is: %d\n", number);
return 0;
}
The post-increment operator doesnt seem to work here. The value of the
number remains the same even after the call to the sizeof operator.
Can someone clarify this please?
Thanks,
Anjali.
Hi Anjali,
The problem is not that of the ++ operator...but the 'sizeof'
operator.
The C Standard states that the operand of sizeof can be either a type
name or an expression.
e.g. sizeof (int) OR sizeof (a+b)
If it is an expression,then it will not be evaluated; See section
A.7.4.8 of K&R2 - [The C Programming Language 2/ed - Kernighan &
Ritchie ], Appendix A.
For the sake of convenience I'm reproducing the contents verbatim
here :
A.7.4.8 Sizeof Operator
The sizeof operator yields the number of bytes required to store an
object of the type of its operand. The operand is either an
expression,***_which is not evaluated_**, or a parenthesized type
name. When sizeof is applied to a char, the result is 1; when applied
to an array, the result is the total number of bytes in the array.
When applied to a structure or union, the result is the number of
bytes in the object, including any padding required to make the object
tile an array: the size of an array of n elements is n times the size
of one element. The operator may not be applied to an operand of
function type, or of incomplete type, or to a bit-field. The result is
an unsigned integral constant; the particular type is
implementation-defined.
Hence your expresion number++ never gets evaluated.So the value of
number doesn't increase.
HTH
Nitin