Thread: ++k and k++ are different processes, or not?

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52

    Question ++k and k++ are different processes, or not?

    I noticed a problem when I glanced at my studying document. The problem is about post and pre increment operator ++.

    Here are the code in mydocument:
    Code:
    #include <iostream>
    #include <conio.h>
    
    
    using namespace std;
    
    
    int k = 0; //global k
    
    
    int main()
    {
        int k; //local k
    
    
        for (k = 0; k < 10; k++) {  // LOOK AT HERE !!!!!!!!!!!!!!!!!!!!
               cout << " local variable " << k ;
               cout << " global variable " << ::k <<endl;
        }
    
    
        ::k++;
    
    
        cout << endl;
    
    
        cout << " After loop;\n" ;
        cout << " local variable : " << k ;
        cout << "\n global variable : " << ::k <<endl;
    
    
        getch();
    
    
        return 0;
    }
    Here are the output:
    ++k and k++ are different processes, or not?-1-png

    So what if the for loop was that ;
    for (k = 0; k < 10; ++k)
    Does the output change ?

    I expect the same output except for line 1 in the output:
    local variable 1 global variable 0

    Then,
    ---------------------------------
    local variable 2 global variable 0
    local variable 3 global variable 0
    ...
    bla bla bla
    ...
    ---------------------------------
    Why the k is not 1 in the first looping in for loop ? Or is k++ and ++k the same thing? I know that these are different. But there are as if the same. Can anybody solve this problem in my mind?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > for (k = 0; k < 10; ++k)
    > Does the output change ?
    No, because whether you write k++ or ++k, it still happens after the body of the loop has executed.

    If you're just using it for the side effect of incrementing the variable, there is no difference at all (for the built-in types(*)).
    You only see the difference when you inspect the value of k with the side effect applied.

    Compare
    int a = k++;
    int b = ++k;
    Now compare a, b and k



    (*)When you come to overload the operators in a C++ class however, you should be mindful of the differences.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Location
    Ankara
    Posts
    52
    Okay, I understand. Thank you for your answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Processes
    By wise_ron in forum C Programming
    Replies: 3
    Last Post: 09-11-2006, 09:22 PM
  2. Want to end a few processes
    By Swordsalot in forum Windows Programming
    Replies: 7
    Last Post: 02-20-2006, 11:00 PM
  3. processes
    By iain in forum Windows Programming
    Replies: 3
    Last Post: 06-25-2004, 01:53 PM
  4. Processes
    By MathFan in forum C++ Programming
    Replies: 2
    Last Post: 12-25-2002, 11:46 AM
  5. processes
    By Nor in forum Windows Programming
    Replies: 1
    Last Post: 03-23-2002, 03:52 AM

Tags for this Thread