Thread: Do parens make a difference here?

  1. #1
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

    Do parens make a difference here?

    If I am testing a bit (and please don't tell me better ways to test a bit - I know there are), are the inner parens redundant here?

    Code:
    if ((work->flagbyte & 0x80) ==0) { ... }
    Mainframe assembler programmer by trade. C coder when I can.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Yes, needed. The Operator man page would have told you this!
    The bitwise operator has a lower precedence than the equality operator.
    Last edited by rstanley; 12-22-2023 at 03:45 PM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Ah yes, I see this now. No man pages on the system I'm using! Not all development systems are *nix.

    Thank you!
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Dino View Post
    Ah yes, I see this now. No man pages on the system I'm using! Not all development systems are *nix.

    Thank you!
    A good up-to-date book on the C Programming Language would also have this information. Should be on your bookshelf!

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    Here's a good link for you: C Operator Precedence - cppreference.com

    The cppreference website is obviously targeted at C++, but they have C pages for a huge set of relevant topics. Probably the only thing they are missing is the POSIX/Windows fundamental stuff which is arguably not language-related, but which informs the language so heavily that it's hard to not have.

    I recommend adding cppreference to your library of bookmarks as a useful place to go to find important language and library trivia.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    My C precedence cheat sheet, designed to be very small.
    All operators in a category are the same precedence, except for Binary.
    In the Binary category, operators lower down are lower, as are operators to the right of a semicolon.
    Code:
    Postfix (LtoR)  x++ x-- f() a[] o.m p->m (type){list}
    Prefix  (RtoL)  ++x --x +x -x ! ~ *p &o (type) sizeof _Alignof
    Binary  (LtoR)  * / % ; + - 
                    << >> ; < > <= >= ; == !=
                    & ; ^ ; | ; && ; ||
    Ternary (RtoL)  a ? b : c
    Assign  (RtoL)  = += -= *= /= %= <<= >>= &= ^= |=
    Comma   (LtoR)  ,
    It seems idiotic that & is below ==, but perhaps it was meant to be occasionally used as a non-short-circuiting version of &&?
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > It seems idiotic that & is below ==, but perhaps it was meant to be occasionally used as a non-short-circuiting version of &&?
    Way way back in the mists of time....

    In B and BCPL (the precursors of C), & and | were short-circuit when using in a boolean context.

    And then it got messy....

    Chistory
    Read the section "Neonatal C"

    Also Dennis Ritchie on & | vs. ==
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 10-08-2013, 06:45 AM
  2. Replies: 10
    Last Post: 08-18-2005, 01:17 AM
  3. Replies: 1
    Last Post: 01-10-2003, 10:08 PM
  4. What A Difference 30 Years Make...
    By -dcx- in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-18-2002, 06:38 PM
  5. gmake or make, what's the difference?
    By Hubas in forum C++ Programming
    Replies: 0
    Last Post: 02-13-2002, 04:42 AM

Tags for this Thread