Thread: while (scanf("%d", &n) == 1 && n >= 0)

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    while (scanf("%d", &n) == 1 && n >= 0)

    Hello,
    What's the order of precedence and associativity of the next expression:
    while (scanf("%d", &n) == 1 && n >= 0)
    your help will be appreciated
    Thank you.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    && is evaluated from left to right
    if the left part is false - right part is not evaluated

    what else do you need to know about this expression?

    BTW http://www.lmgtfy.com/?q=C+precedence
    Last edited by vart; 03-06-2009 at 04:02 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    isn't the order as follow (according to a table of precedence and associativity):
    scanf() is called first because of its highest precedence.
    second: n >= 0 is evaluated
    third:scanf() return value is compared to 1
    fourth: && is invoked

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mashour06 View Post
    isn't the order as follow (according to a table of precedence and associativity):
    scanf() is called first because of its highest precedence.
    second: n >= 0 is evaluated
    third:scanf() return value is compared to 1
    fourth: && is invoked
    right part of && is evaluated only after left part, and only if left part is true
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In this case I think the left part will always be true because && has precedence and the left term is just 1. To make this work properly you need
    Code:
    while ((scanf("%d", &n) == 1) && (n >= 0))
    some more parantheses
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mashour06
    isn't the order as follow (according to a table of precedence and associativity):
    As the 1999 edition of C standard says:
    Quote Originally Posted by C Standard, 1999 edition, section 6.5, paragraph 3
    The grouping of operators and operands is indicated by the syntax. Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.
    Hence you should refer to vart's post #4 as to the order of evaluation since operator && is involved.

    Quote Originally Posted by MK27
    In this case I think the left part will always be true because && has precedence and the left term is just 1.
    No, operators >= and == have precedence over operator &&. Incidentally, operator >= has precedence over operator ==, but that is not relevant in this case.
    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

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    Thank you

    Thanks for you all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  2. Separate long string into multiple arrays
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-27-2007, 02:57 AM
  3. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 09:50 PM
  4. Error message in tic tac toe problem please help
    By Kross7 in forum C++ Programming
    Replies: 17
    Last Post: 04-10-2007, 01:50 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM