Typo in C Primer Plus? or something else? Conditional operator ?:

This is a discussion on Typo in C Primer Plus? or something else? Conditional operator ?: within the C Programming forums, part of the General Programming Boards category; There's a line in a program in C Primer Plus that is: Code: cans += ((sq_feet % COVERAGE == 0)) ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    84

    Typo in C Primer Plus? or something else? Conditional operator ?:

    There's a line in a program in C Primer Plus that is:

    Code:
    cans += ((sq_feet % COVERAGE == 0)) ? 0 : 1 ;
    One set of parenthesis works, but is there a reason he put in two or is it just a typo?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    There is an off-chance that they meant to type this, to just emphasis the precedence.
    Code:
    cans += ((sq_feet % COVERAGE) == 0) ? 0 : 1 ;
    But the precedence of ?: is so low that you don't need any of them
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    84
    That crossed my mind too.
    Okay, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional operator
    By nasser in forum C Programming
    Replies: 2
    Last Post: 10-23-2011, 07:18 PM
  2. Conditional Operator
    By markcocoa10 in forum C Programming
    Replies: 7
    Last Post: 10-15-2010, 06:22 PM
  3. Conditional Operator
    By arjunajay in forum C Programming
    Replies: 8
    Last Post: 07-10-2008, 08:17 AM
  4. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 12:24 PM
  5. Conditional operator ? :)
    By ER in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2001, 02:34 AM

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