how to think about parentheses

V

vlsidesign

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 :)
 
E

Eric Sosman

vlsidesign said:
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.
 
C

Chris Dollin

vlsidesign said:
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).
 
R

Richard Tobin

vlsidesign said:
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
 
P

pete

vlsidesign said:
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
 
P

Philip Potter

pete said:
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.
 
J

jameskuyper

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
 
R

Richard Tobin

sizeof operator? sizeof(char *) doesn't come under any of the above
uses, I believe.
[/QUOTE]
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top