Search code examples
c++operatorssizeof

increment operator not working with sizeof


Just wondering why the increment operator is not working in the below code snippet:

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

Output-

a: 10

b: 4


Solution

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