Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is the value of the variable not increasing after sizeof(++)

The value temp is 1, but why is temp still 1 after sizeof(++i)? I only know sizeof is an operator.

int main() {
    int i = 1;
    sizeof(i++);
    std::cout << i << std::endl;  // 1
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sizeof does not evaluate its operand. It determines the operand’s type and returns its size. It is not necessary to evaluate the operand in order to determine it’s size. This is actually one of the fundamental core principles of C++: the types of all objects — and by consequence of all expressions — is known at compile time.

And since the operand does not get evaluated there are no side effects from its evaluation. If you sizeof() something that makes a function call the function does not actually get called: sizeof(hello_world()) won’t call this function, but just determines the size of whatever it returns.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading