Thread: Question the increment and decrement in this code.

  1. #1
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61

    Question the increment and decrement in this code.

    Parts of this program are missing. The last few lines are confusing, since the variable 'a' gets incremented then decremented. But there are no loops. I understand that the value of 'a' is passed to 'c' before 'a' is changed in both cases.

    But where, and when, do the changes take place? Is the decrement ever processed? Is there a better way to write these lines?

    Code:
     main(){ int a = 21;int b = 10;int c ;    
       c = a++;    
    cout << "Line 6 - Value of c is :" << c << endl ;
       c = a--;
       cout << "Line 7 - Value of c is  :" << c << endl ;
       return 0;}

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Keep in mind, these operators are really just doing
    Code:
    a = a + 1;
    
    // or...
    
    a = a - 1;
    Out of curiousity, what's your output and is it as you expect it?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I understand that the value of 'a' is passed to 'c' before 'a' is changed in both cases.
    It would be more clear to say the value of 'a' is assigned to 'c' before 'a' is updated.

    But where, and when, do the changes take place?
    Think of it as a two-step process:

    (1) The value of 'a' is assigned to 'c'
    (2) 'a' is incremented (or decremented)

    Is the decrement ever processed?
    Have you tried printing out the value of 'a' to see what it is?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    int c = a--;
    <==>
    int c = a;
    --a;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2014
    Location
    Central Arizona
    Posts
    61
    Thank you all for the replies. I'm aware of what all of you have written above. I guess I was not clear when I asked my question. It is hard to put this into words.

    The code is written so the increment happens after c = a++; and the decrement happens after c = a--;. Therefore Line 3 prints out 21 and Line 5 prints out 22. (exactly as I expected).

    I hope this makes sense. Where exactly do these changes (increment & decrement) take effect? Does the code do this automatically after each cout lines (number 3 & 5)? Or does the compiler make the change?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by papagym177 View Post
    Thank you all for the replies. I'm aware of what all of you have written above. I guess I was not clear when I asked my question. It is hard to put this into words.

    The code is written so the increment happens after c = a++; and the decrement happens after c = a--;. Therefore Line 3 prints out 21 and Line 5 prints out 22. (exactly as I expected).

    I hope this makes sense. Where exactly do these changes (increment & decrement) take effect? Does the code do this automatically after each cout lines (number 3 & 5)? Or does the compiler make the change?
    They happen after the value is read from, after the previous line of code, and before the next line of code. Nothing else is guaranteed. Because of this, it is, in the general case, undefined behavior to read and write from the same memory location within the same statement.

    C++ defines virtual model of how a program runs. But that model isn't what actually happens in hardware. The compiler ensures that generated machine code obeys the constraints of the C++ programing model. In doing so the compiler is free to reorder code arbitrarily, so long as the externally visible behavior of the program is the same. For instance, in this example, a compiler could make a and c not exists in memory at all, instead using the constants 20 and 21 directly.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LCD Increment and Decrement For Display
    By AJITnayak in forum C Programming
    Replies: 13
    Last Post: 02-27-2014, 10:22 PM
  2. Increment and Decrement
    By Johnathan1707 in forum C Programming
    Replies: 4
    Last Post: 07-25-2009, 02:20 PM
  3. newb need help with increment and decrement
    By cgurl05 in forum C Programming
    Replies: 2
    Last Post: 03-05-2006, 04:50 PM
  4. increment and decrement operators
    By ee0u22ba in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2003, 04:57 AM
  5. increment/decrement operators
    By ZakkWylde969 in forum C++ Programming
    Replies: 10
    Last Post: 07-10-2003, 04:17 PM