Help with increment operator

This is a discussion on Help with increment operator within the C Programming forums, part of the General Programming Boards category; how does this work Code: main() { int i=3; int j; j = sizeof(++i+ ++i); // ++i = 4, again ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    21

    Unhappy Help with increment operator

    how does this work
    Code:
    main()
    {
      int i=3;
      int j;
    
      j = sizeof(++i+ ++i);  // ++i = 4, again ++i = 5. Then ++i + ++i = 5+5 = 10?
    
      printf("i=%d j=%d", i ,j);
    }
    but out put is i =3 and j =4;
    Last edited by capvirgo; 02-18-2008 at 10:47 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    Okay, I was about to quote concerning evaluation order, but in this case it does not matter.

    The answer is that sizeof does not evaluate its operand, so what you are seeing is equivalent to sizeof(int), which in this case is 4.
    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
    Feb 2008
    Posts
    3
    i think that the pre increment or post does not vary w.r.t to i and the j depends on size of operator

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. increment and decrement operator
    By jaipandya in forum C Programming
    Replies: 5
    Last Post: 10-20-2004, 06:54 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. increment operator Question from a beginner
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2001, 07:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21