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

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

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    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, 03:34 AM