Thread: HEX-numbers after incrementation x in for-loop.

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    HEX-numbers after incrementation x in for-loop.

    SORRY FOR THE DOUBLE POST IN THE FORUM, PLEASE ADMIN REMOVE ONE OF THEM, THANKS

    Hello Folks,

    I made a little simple program to practice C++, My goals was too see how the two different methods of incrementation (example: x++ and ++x) would be handled in a for-loop.

    this is the program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void ForIncrementFront()
    {
        for(int x = 0; x < 10; ++x)
        {
            cout << x << "\t";
        }
    }
    
    void ForIncrementBehind()
    {
        for(int x = 0; x < 10; x++)
        {
            cout << x << "\t";
        }
    }
    
    int main()
    {
        cout << "Function: ForIncrementFront  (++x)\n";
        ForIncrementFront();
        cout << "Function: ForIncrementBehind (x++)\n";
        ForIncrementBehind();
    
        return 0;
    }
    this was my output:
    Code:
    Function: ForIncrementFront (++x):
    0 1 2 3 4 5 6 7 8 9 F
    Function: ForIncrementBehind (x++):
    0 1 2 3 4 5 6 7 8 9

    The output of function ForIncrementFont surprised me a little bit because 10 is printed as F and not as 10. what I would like to know is why F instead of 10?, is this a proof of the fact that the PC "speaks" only Hexadecimal and binary? if this is true, why is the output of the code below: 10.

    Code:
    cout << 9+1;

    I hope you understand my question. Sorry if my English grammar is bad, IŽam dutch.
    Last edited by Jelte; 08-04-2009 at 12:15 PM.
    The Programming Dutchman

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I cannot duplicate your output, and indeed I do not expect such output. I expect the two functions to print the exact same output, and they do so when I tested your program.
    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
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    @laserlight hey thanks for your quick message .

    maybe some specs of my Dev Software helps.?

    OS: Ubuntu 9.04.
    Dev prog: Code::blocks 8.02.
    Compiler: GNU GCC Compiler .

    Edit:

    i am not surprised about the output of the functions, this is normal. But i am surprised about 10 is printed as F in situation 1, and in situation 2 as 10.
    Last edited by Jelte; 08-04-2009 at 12:20 PM.
    The Programming Dutchman

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you copy and paste the code that you actually compiled?
    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

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    i am not surprised about the output of the functions, this is normal. But i am surprised about 10 is printed as F in situation 1, and in situation 2 as 10.
    10 is not printed as F. Your loop goes from 0-9, so 10 will never get printed at all.

    My best guess is there is an issue with your terminal that is word wrapping the output text. Your correct output should be:
    Code:
    Function: ForIncrementFront (++x):
    0 1 2 3 4 5 6 7 8 9 Function: ForIncrementBehind (x++):
    0 1 2 3 4 5 6 7 8 9
    Note that there is NOT a newline between the 9 and the "Function: ForIncrementBehind" text.
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    @laserlight: no this is my own program.

    @bithub:

    i think 10 should be printed because there is a difference between X++ and ++X. i can show you that by this program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        unsigned short numberx = 0;
        unsigned short numbery = 0;
    
        cout << "incrementie-front\t"  << ++numberx   << endl;
        cout << "incrementie-behind\t" << numbery++   << endl;
        cout << endl;
        cout << "numberx:\t" << numberx << endl;
        cout << "numbery:\t" << numbery << endl;
    
        return 0;
    }
    As you can see there is a moment when numberx and numbery are different, because numbery get updated AFTER it is printed on the screen. i think this should be the same in a for-loop. and so 10/F should be printed on the screen.

    please correct me if i am wrong...

    Thanks for help.

    Jelte.
    The Programming Dutchman

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    i think 10 should be printed because there is a difference between X++ and ++X. i can show you that by this program:
    No, that's not correct. It doesn't matter if you pre-increment or post-increment the value in the for loop because its return value is not evaluated. In other words, you are not assigning it to anything.

    It basically looks like:
    Code:
     for(int x = 0; x < 10; ++x)
         cout << x << endl;
    // is the same as...
    int x = 0;
    while(x < 10)
    {
        cout << x << endl;
        ++x; // notice that it doesn't matter if you have ++x or x++
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    @bithub: ok. thanks for you correction and your clear explanation.

    also you where right about i realised that F is is not 10 in HEX. i do not know why i thought 10 in HEX was F instead of A :P

    so, what else could F be? i do not use the ubuntu terminal but the console of code::blocks.
    The Programming Dutchman

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Jelte View Post
    so, what else could F be? i do not use the ubuntu terminal but the console of code::blocks.
    Then this is what you see (and notice the complete absence of an extra F):

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Can you please post your ACTUAL code? I know that this cannot possibly be your code, because this line:

    Code:
        cout << "Function: ForIncrementFront  (++x)\n";
    Cannot produce this output:

    Code:
    Function: ForIncrementFront (++x):
    Since there is no ':' character in the string. Post the ACTUAL code and maybe we can avoid wasting our time
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jelte
    no this is my own program.
    What I meant is whether you copied and pasted the code from your text editor as opposed to rewriting it out in your post (i.e, what brewbuck restated about posting your actual code). Anyway, I think bithub's explanation is as likely as a stray character in the output that I had in mind: the console window opened by Code::Blocks can have the same effect that bithub described. You just need to be careful in interpreting the output, but perhaps it would be simpler to just introduce a new line:
    Code:
    void ForIncrementFront()
    {
        for(int x = 0; x < 10; ++x)
        {
            cout << x << ' ';
        }
        cout << '\n';
    }
    
    void ForIncrementBehind()
    {
        for(int x = 0; x < 10; x++)
        {
            cout << x << ' ';
        }
        cout << '\n';
    }
    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

  12. #12
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55
    Owh.., i got it and it is sooo stupid , Sorry everybody for this ... not existing problem post (it is a little bit funny now). see attached image.

    you all may start Rolling On Floor Laughing :$

    Greeting

    Jelte,
    Last edited by Jelte; 08-05-2009 at 10:40 AM.
    The Programming Dutchman

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  2. Printing numbers in hex format
    By miclus in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2005, 07:04 AM
  3. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  4. converting hex to dec
    By jibbles in forum C Programming
    Replies: 20
    Last Post: 08-07-2004, 11:40 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM