Advertisement

Is it just me...

Started by July 28, 2000 11:27 AM
5 comments, last by NeRo 24 years, 4 months ago
I with all the talk of char in that other post, I was playing around and got an output I didn't expect.
        
#include<iostream.h>
#include<iomanip.h>

void main()
{
  for (char mc=65; mc<127; )
    cout << setw(6) << mc << ' ' << ++mc << endl;
}
        
it gave me this: A A B B C C... why isn't it more like this: A B B C C D.. ??????? -Sean Edited by - NeRo on 7/28/00 11:28:48 AM
|============================|| Things Smell VERY different|| to a midget in an elevator||============================|
it would give you
A B
B C
C D etc..
if you used mc++

mc++ increments the variable, then uses.. ++mc uses the variable then increments it, ie

    #include <iostream.h>int main(void) {    int number=1;    cout << ++number << ' ' << number << endl;    return(0);}    

will output:
1 2

Edited by - fuzzyai on July 28, 2000 12:41:59 PM
Advertisement
Are java's and c++'s unary operators that different???

        class highASCII{  public static void main(String[] args)  {    for (char mc=65; mc<127; )    {      System.out.println((mc)+" "+(++mc)+"\n");    }  }}        

in java it gives you:
A B
B C
C D


Edited by - NeRo on July 28, 2000 1:01:20 PM
|============================|| Things Smell VERY different|| to a midget in an elevator||============================|
Actually, it should give you this:
B B
C C
D D

...there''s no "A A". Does this make more sense?

Hint: it has to do with how C++ evaluates parameters to a function call (or in this case, a chain of function calls).
Java should behave the same was as C++ does as far as the ++ operator goes. If you put ++ before a variable it should increment it first and then perform the operation. If you put it after it will perform the operation and then increment the variable.
You''re right Stoffel!
I didn''t think about "<<" being evaluated in reverse order.
For a minute there, I thought I forgot how to use the unary operators. Especially when fuzzyai explained them in reverse
quote: mc++ increments the variable, then uses.. ++mc uses the variable then increments it, ie

Thanks


-Sean
|============================|| Things Smell VERY different|| to a midget in an elevator||============================|
Advertisement
First, a correction:

quote:
mc++ increments the variable, then uses.. ++mc uses the variable then increments it


Incorrect; you have the two definitions backwards. ++mc is called "pre-increment", which means it first increases the variable and then returns the value. mc++ is called "post-increment" which means it first returns the value then increments. See example #1 below to see this.

Now, back to the problem. Your output statement is not terribly safe nor predictable. The order of evaluation between "mc" and "++mc" as in your original code is compiler''s choice. It''s true that the calls to operator<< are in a predictable order (left-to-right), but not necessarily the individual parameters.

Look at example #2 for a simple example that will show my point. The author is expecting 7, but maybe he''ll get 8! The problem is that the compiler can choose to evaluate either term first: the "++a" or the "a". If the "a" is evaluated first, the compiler generates 4+3 and gets 7. If the "++a" is evaluated first, the compiler generates 4+4 and gets 8.

operator+ is evaluated in a particular order (L2R or R2L, I forget), but that applies to the operator and is most noticeable when dealing with multiple operator+. (For example, a+b+c is evaluated as a+(b+c) assuming R2L precedence.) But that ordering says nothing about when the compiler evaluates particular terms.

So going back to the original, it''s possible that the "++mc" term that appears after may be evaluated before the "mc" term, even though "mc" is printed out before "++mc". Or it may work as expected. Since you can''t really know, it''s unsafe and should be rewritten. Something more like example #3 or #4.


    //example #1int a, b, c;  a = 3;    //a == 3b = ++a;  //a == 4, b == 4c = a++;  //a == 5, b == 4, c == 4    //example #2int a, b;  a = 3;         //a == 3b = ++a + a;   //a == 4, b == ???    //example #3for (char mc=65; mc<127; ++mc)  cout << setw(6) << mc << '' '' << mc+1 << endl;    //example #4for (char mc=65; mc<127; ) {  cout << setw(6) <<   mc << '' '';  cout << setw(6) << ++mc << endl;}    





---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

This topic is closed to new replies.

Advertisement