increment operator not working with sizeof

sizeof does not evaluate its argument. It calculates the argument's size statically at compile-time without causing any code to be executed.


When the type of the expression to sizeof is not a variably modified array type, then the expression is not evaluated because the type is completely known at compile time. int has no variably modified parts.

In C++ (up to at least C++11) there are no variably modified types (at least not as in the concept of C - you can argue that new int[a++] uses a variably modified array type; but the type does not escape to any other part of the language. In particular, not to sizeof), so in C++, the expression to sizeof is never evaluated. In C, it is unspecified whether an expression is evaluated if it doesn't influence the size of a variably modified array type. For example

int main()
{
    int a = 10;
    int b = sizeof(int[a++ ? 1 : 1]);
    cout<<"a: "<<a<<endl;
    cout<<"b: "<<b<<endl;
    return 0;
}

In C (from C99 onwards), this may output 11 for a, but it may also output 10, depending on whether the compiler is clever enough to omit evaluating a++, deducing that the sizeof int[10] is computed at compile time.


Footnote: Variably modified array types are also called VLA (variable length array) types. In short, a variably modified type is a type that is either a VLA type or a type that depends on one. For example int(*)[a++].


The operand of the sizeof operator is unused, it's not evaluated. This is standard behavior.