how to think about parentheses

Discussion in 'C Programming' started by vlsidesign, Dec 3, 2007.

  1. vlsidesign

    vlsidesign Guest

    I am not sure how to think about parentheses. It seems when they are
    used alongside characters it indicates a function, like addTwoNums().
    But is also is used to enforce which operators and operands are
    evaluated first.

    Can I think of the parentheses being used as "grouping" things
    together or how should I think about them? Thanks :)
    vlsidesign, Dec 3, 2007
    #1
    1. Advertising

  2. vlsidesign

    Eric Sosman Guest

    vlsidesign wrote:
    > I am not sure how to think about parentheses. It seems when they are
    > used alongside characters it indicates a function, like addTwoNums().
    > But is also is used to enforce which operators and operands are
    > evaluated first.
    >
    > Can I think of the parentheses being used as "grouping" things
    > together or how should I think about them? Thanks :)


    Several characters have multiple meanings in C source code,
    depending on where they are used. Here are a few such:

    . can be the decimal point in 3.14159, or the divider
    between the name of a struct or union and one of its
    elements in thing.field, or part of the ... ellipsis
    that declares a variable-arguments function

    / can be the division operator operator in 3.2/x, or
    part of a comment introducer /* or //, or part of a
    comment terminator */

    * can be the multiplication operator in x*y or x*=y, or
    the pointer dereference operator in *ptr, or part of
    a comment introducer or terminator

    , can be the comma operator, or a separator in various
    kinds of lists (function arguments, enum constants,
    initializers, ...)

    ; can be the statement terminator, or a declaration
    terminator, or the separator between clauses of `for'

    () can surround a list of macro arguments, or a list of
    function arguments, or a list of function parameter
    declarations (in a couple of forms), or a sub-expression
    within an expression, or assorted pieces of the `for',
    `while', 'do-while', and `switch' statements

    {} can surround blocks of statements and declarations,
    or can delimit parts of various kinds of declarations,
    or can surround groups and sub-groups of initializers

    : can separate the name of a struct or union element from
    its width in bits, or can separate a label from its
    statement

    E can be an identifier or part of an identifier as in
    ERANGE, or can be part of a numeric literal like 2.7E26

    & can be the address-of operator as in &thing, or the
    bitwise AND operator as in flags&0x4 or byte&=0xFF,
    or part of the logical AND operator as in x>0&&x<42

    .... and so on, and so on. The meaning attached to a character
    depends on the context in which it appears, and there is seldom
    one single rule that covers all the possible contexts.

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 3, 2007
    #2
    1. Advertising

  3. vlsidesign

    Chris Dollin Guest

    vlsidesign wrote:

    > I am not sure how to think about parentheses. It seems when they are
    > used alongside characters it indicates a function, like addTwoNums().


    Expressions F(X), where F is an expression (and of a kind which is
    typically very small) and X is a series [possibly empty] of expressions
    separated by commas [which in this case are not comma operators], are
    calls to the function F with arguments X, yes.

    > But is also is used to enforce which operators and operands are
    > evaluated first.


    Typically no; they're used to say which operands go with which operators,
    but they /don't/ do anything themselves about order of evaluation, which
    is specified [or not] by the /operators/ themselves.

    > Can I think of the parentheses being used as "grouping" things
    > together


    Yes, when they're not part of a function call (or declaration).

    --
    Chris "groups rock!" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Dec 3, 2007
    #3
  4. In article <>,
    vlsidesign <> wrote:

    >I am not sure how to think about parentheses. It seems when they are
    >used alongside characters it indicates a function, like addTwoNums().
    >But is also is used to enforce which operators and operands are
    >evaluated first.


    The same is true in mathematics and in most other programming
    languages.

    You can imagine the parentheses around a function's arguments as
    grouping them into a single object to be passed and then unpacked, but
    that's not really useful in C, because you need the parentheses even if
    there's only one argument. So it's probably better just to think of
    them as having two different uses.

    -- Richard

    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Dec 3, 2007
    #4
  5. vlsidesign

    pete Guest

    vlsidesign wrote:
    >
    > I am not sure how to think about parentheses. It seems when they are
    > used alongside characters it indicates a function, like addTwoNums().
    > But is also is used to enforce which operators and operands are
    > evaluated first.
    >
    > Can I think of the parentheses being used as "grouping" things
    > together or how should I think about them? Thanks :)


    You seem to have encountered parentheses
    as a function-call operator and also as a punctuator,
    which are two out of the four
    distinct uses of parentheses in C code
    that are mentioned in the N869 index:

    ( ) (cast operator), 6.5.4
    ( ) (function-call operator), 6.5.2.2
    ( ) (parentheses punctuator), 6.7.5.3, 6.8.4, 6.8.5
    ( ){ } (compound-literal operator), 6.5.2.5

    --
    pete
    pete, Dec 3, 2007
    #5
  6. pete wrote:
    > vlsidesign wrote:
    >> I am not sure how to think about parentheses. It seems when they are
    >> used alongside characters it indicates a function, like addTwoNums().
    >> But is also is used to enforce which operators and operands are
    >> evaluated first.
    >>
    >> Can I think of the parentheses being used as "grouping" things
    >> together or how should I think about them? Thanks :)

    >
    > You seem to have encountered parentheses
    > as a function-call operator and also as a punctuator,
    > which are two out of the four
    > distinct uses of parentheses in C code
    > that are mentioned in the N869 index:
    >
    > ( ) (cast operator), 6.5.4
    > ( ) (function-call operator), 6.5.2.2
    > ( ) (parentheses punctuator), 6.7.5.3, 6.8.4, 6.8.5
    > ( ){ } (compound-literal operator), 6.5.2.5
    >


    sizeof operator? sizeof(char *) doesn't come under any of the above
    uses, I believe.
    Philip Potter, Dec 3, 2007
    #6
  7. vlsidesign

    Guest

    On Dec 3, 5:22 pm, Philip Potter <> wrote:
    > pete wrote:
    > > vlsidesign wrote:
    > >> I am not sure how to think about parentheses. It seems when they are
    > >> used alongside characters it indicates a function, like addTwoNums().
    > >> But is also is used to enforce which operators and operands are
    > >> evaluated first.

    >
    > >> Can I think of the parentheses being used as "grouping" things
    > >> together or how should I think about them? Thanks :)

    >
    > > You seem to have encountered parentheses
    > > as a function-call operator and also as a punctuator,
    > > which are two out of the four
    > > distinct uses of parentheses in C code
    > > that are mentioned in the N869 index:

    >
    > > ( ) (cast operator), 6.5.4
    > > ( ) (function-call operator), 6.5.2.2
    > > ( ) (parentheses punctuator), 6.7.5.3, 6.8.4, 6.8.5
    > > ( ){ } (compound-literal operator), 6.5.2.5

    >
    > sizeof operator? sizeof(char *) doesn't come under any of the above
    > uses, I believe.


    Falls in the first case.
    , Dec 3, 2007
    #7
  8. vlsidesign

    Guest

    wrote:
    > On Dec 3, 5:22 pm, Philip Potter <> wrote:
    > > pete wrote:

    ....
    > > > You seem to have encountered parentheses
    > > > as a function-call operator and also as a punctuator,
    > > > which are two out of the four
    > > > distinct uses of parentheses in C code
    > > > that are mentioned in the N869 index:

    > >
    > > > ( ) (cast operator), 6.5.4
    > > > ( ) (function-call operator), 6.5.2.2
    > > > ( ) (parentheses punctuator), 6.7.5.3, 6.8.4, 6.8.5
    > > > ( ){ } (compound-literal operator), 6.5.2.5

    > >
    > > sizeof operator? sizeof(char *) doesn't come under any of the above
    > > uses, I believe.

    >
    > Falls in the first case.


    No, the parenthesis are a fundamental part of the production in 6.5.3:

    unary-expression:
    ...
    sizeof ( type-name )

    Which is completely independent from the fact that they are also a
    part of the production in 6.5.4:

    cast-expression:
    unary-expression
    ( type-name ) cast-expression
    , Dec 3, 2007
    #8
  9. In article <>,
    <> wrote:

    >> > ( ) (cast operator), 6.5.4
    >> > ( ) (function-call operator), 6.5.2.2
    >> > ( ) (parentheses punctuator), 6.7.5.3, 6.8.4, 6.8.5
    >> > ( ){ } (compound-literal operator), 6.5.2.5


    >> sizeof operator? sizeof(char *) doesn't come under any of the above
    >> uses, I believe.


    >Falls in the first case.


    No, the use of parentheses with sizeof is a special case in the
    production for unary-expression. sizeof(char *) does not involve a
    cast.

    I'm not sure that "parentheses punctuator" is the same sort of thing
    as the others. Is it supposed to include both expression grouping and
    for-loop syntax, for example?

    -- Richard

    --
    "Consideration shall be given to the need for as many as 32 characters
    in some alphabets" - X3.4, 1963.
    Richard Tobin, Dec 3, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    'AddressOf' operand must be the name of a method; no parentheses a

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 18, 2004, in forum: ASP .Net
    Replies:
    15
    Views:
    7,539
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=
    Oct 19, 2004
  2. Replies:
    7
    Views:
    9,194
    Jeff Dillon
    May 17, 2006
  3. Replies:
    0
    Views:
    586
  4. Replies:
    1
    Views:
    856
    Jack Klein
    Apr 13, 2005
  5. David Mark
    Replies:
    17
    Views:
    224
    David Mark
    Mar 23, 2010
Loading...

Share This Page