order of execution

This is a discussion on order of execution within the C++ Programming forums, part of the General Programming Boards category; In what order will the following line of code be executed? Code: while((char c = cin.get()) != 'q') Is my ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    order of execution

    In what order will the following line of code be executed?

    Code:
    while((char c = cin.get()) != 'q')
    Is my assumption correct that the sequence of execution will be in the following order.

    1) cin.get() gets called
    2) its return value is assigned to c
    3) then c's value is compared to 'q'

    Is it correct? I was reading Thinking in C++ by Bruce Eckel and in the third Chapter I read this

    Excerpt from the book:

    For example, you cannot have any parentheses. That is, you cannot say:

    while((char c = cin.get()) != 'q')

    The addition of the extra parentheses would seem like an innocent and useful thing to do, and because you cannot use them, the results are not what you might like. The problem occurs because ‘!=’ has a higher precedence than ‘=’, so the char c ends up containing a bool converted to char. When that’s printed, on many terminals you’ll see a smiley-face character.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You get a syntax error is what you get. If you put the declaration of c outside the while statement, then you'd be fine -- but you can't nest a declaration of a variable like that.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Quote Originally Posted by tabstop View Post
    You get a syntax error is what you get. If you put the declaration of c outside the while statement, then you'd be fine -- but you can't nest a declaration of a variable like that.
    A what? Clarify please

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A syntax error. What you have typed is not legal, valid, C++. You can do this:
    Code:
    while((c = cin.get()) != 'q') //fine
    no problem. But you cannot nest a variable declaration inside extra parentheses as you have tried to do above.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    How come this statement is valid?

    Code:
    while(char c = cin.get() != 'q')

  6. #6
    Registered User CrissyCrisCris's Avatar
    Join Date
    Aug 2009
    Posts
    13
    What complier does your book tell you to use? In Microsoft Visual C++ (MSVC) you can not declare a variable inside the while's condition like that.
    Last edited by CrissyCrisCris; 08-13-2009 at 08:53 AM. Reason: Correcting Grammer

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    So you mean that (my last-most post's code) is not valid?

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,170
    It is not.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by manzoor View Post
    How come this statement is valid?

    Code:
    while(char c = cin.get() != 'q')
    It is syntactically correct, yes. It is logically incorrect, as the != will happen first and so char c will be assigned either one or zero, not the result of cin.get(), but it will compile (unlike the first example).

  10. #10
    Registered User CrissyCrisCris's Avatar
    Join Date
    Aug 2009
    Posts
    13
    Code:
    while(char c = cin.get() != 'q')
    You can hot have declare a variable in there. You are trying to declare c in there with char.

    Code:
    char c;
    while((c = cin.get()) != 'q')
    You have to declare the variable outside.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Atlast, the day is saved by PowerPuff Girls, la la laaaaaaaaaaaaa


    Thanks guys, this forum has always been a helpful and a good learning resource

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by CrissyCrisCris View Post
    Code:
    while(char c = cin.get() != 'q')
    You can hot have declare a variable in there. You are trying to declare c in there with char.
    Yes you can. That's syntactically correct code. You could have at least tried it before expounding false things.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Quote Originally Posted by brewbuck View Post
    Yes you can. That's syntactically correct code. You could have at least tried it before expounding false things.
    I believe CrissyCrisCris did?
    Quote Originally Posted by CrissyCrisCris View Post
    What complier does your book tell you to use? In Microsoft Visual C++ (MSVC) you can not declare a variable inside the while's condition like that.
    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.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    Quote Originally Posted by Elysia
    I believe CrissyCrisCris did?
    This compiles with MSVC8:
    Code:
    char foo()
    {
        return 0;
    }
    
    int main()
    {
        while(char c = foo() != 'q');
    }
    Last edited by laserlight; 08-13-2009 at 01:12 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Ah, mistake on his/her part then, or perhaps something else.
    Syntactically correct, semantically incorrect. So nice.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. order of execution of tokens in a c++ source code
    By sandy.sandipanc in forum C++ Programming
    Replies: 4
    Last Post: 10-04-2008, 12:34 PM
  3. Weird code execution order problem
    By Syko in forum C Programming
    Replies: 2
    Last Post: 05-12-2005, 02:29 PM
  4. expression execution order
    By _Elixia_ in forum C Programming
    Replies: 3
    Last Post: 10-02-2003, 04:01 PM
  5. randomizing order of execution of function
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-03-2002, 06:50 PM

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