Thread: Baffling interview question I found - post-increment operator

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    Baffling interview question I found - post-increment operator

    Hi.
    I found this interview question online, and it really confuses me...

    Consider the following code snippet:

    Code:
    int main(int argc, char* argv[]) {
    
        int x=97;
        int y=sizeof(x++);
    
        printf("x is %d\n", x);
    
        return 0;
    }
    What is the output?
    1. x is 97
    2. x is 98
    3. x is 99
    4. run-time error

    My guess was 2, but when I compiled and ran the code, I was amazed to find out that this produce "x is 97"!
    What's going on here?


    *EDIT

    Funny thing is when I change the above to this:

    Code:
    int main(int argc, char* argv[]) {
    
        int x=97;
        int y=x++;
    
        printf("x is %d\n", x);
    
        return 0;
    }
    It produces "x is 98" as expected...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unless its operand is a variable length array, sizeof does not evaluate its operand.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    228
    So sizeof cancels out the post-increment operator?

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    228
    And what about this?

    Code:
    int main(int argc, char* argv[]) {
    
        int x=97;
        int y=97;
        int z=sizeof(x+y);
    
        printf("z is %d\n", z);
    
        return 0;
    }
    This produce "z is 4".
    But the only way to determine that is to first evaluate what x+y is, no?

  5. #5
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    As laserlight stated, sizeof does not evaluate its operand in this case. Meaning sizeof(x+y) will give the same result as sizeof(x*y*x*y*x*y) and sizeof(x).
    This parameter is reserved

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Absurd
    So sizeof cancels out the post-increment operator?
    Not really: it just does not evaluate it. Consider this idiom that you may have seen me recommend:
    Code:
    int *p = malloc(sizeof(*p) * n);
    If sizeof evaluated its operand, then sizeof(*p) should be a mistake: p is being initialised at that point, so what could it point to? Indeed, this was my objection when I first saw the use of this idiom, until realising that my objection was invalid because sizeof does not evaluate its operand.

    Quote Originally Posted by Absurd
    But the only way to determine that is to first evaluate what x+y is, no?
    No, the one only needs to determine what are the types of x and y, and hence what is the type of (x + y). No evaluation is needed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Unless its operand is a variable length array, sizeof does not evaluate its operand.
    O_o

    I think the statement could use some code.

    Code:
    #include <stdio.h>
    
    void DoSomething1()
    {
        printf("%zu\n", sizeof(char[printf("Hello, World!\n")]));
    }
    
    void DoSomething2()
    {
        printf("%zu\n", sizeof(printf("Hello, World!\n")));
    }
    
    int main()
    {
        DoSomething1();
        DoSomething2();
        return(0);
    }
    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post Increment an Pre Increment operators in c++
    By anil_ in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2011, 08:27 PM
  2. post and pre increment operator
    By _arjun in forum C Programming
    Replies: 8
    Last Post: 10-31-2011, 12:56 PM
  3. Question on increment operator
    By noodles in forum C Programming
    Replies: 1
    Last Post: 09-05-2006, 11:40 PM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 08:23 AM