nomenclature issue

M

Mantorok Redgormor

in the standard, function arguments are never
referred to as expressions.

but we can certainly have expressions in function
arguments but not all arguments can qualify as
expressions.

func(4); /* 4 is not an expression */

int foo = 4;

func(foo); /*
* foo is an expression because it
* designates an object
*/


is this all correct?
 
J

Joona I Palaste

Mantorok Redgormor said:
in the standard, function arguments are never
referred to as expressions.
but we can certainly have expressions in function
arguments but not all arguments can qualify as
expressions.

I don't agree with this at all. All function arguments are
expressions, at least the way I see it.
func(4); /* 4 is not an expression */

4 is very much an expression.
int foo = 4;
func(foo); /*
* foo is an expression because it
* designates an object
*/
Yes.

is this all correct?

Well, other than that 4 really is an expression, it's all correct.

In C, pretty much every statement is an expression. The only
statements that *aren't* expressions are usually if clauses, or
for, while or do...while loops, or jump statements such as return,
break, continue or goto, or switch statements.
 
D

Dan Pop

In said:
In C, pretty much every statement is an expression. The only
statements that *aren't* expressions are usually if clauses, or
for, while or do...while loops, or jump statements such as return,
break, continue or goto, or switch statements.

In other words, with exactly one exception (expression statements),
C statements aren't expressions.

Dan
 
J

Joona I Palaste

In other words, with exactly one exception (expression statements),
C statements aren't expressions.

Yes, but a typical C program consists of way more expression statements
than other statements.
 
M

Mantorok Redgormor

Joona I Palaste said:
I don't agree with this at all. All function arguments are
expressions, at least the way I see it.


4 is very much an expression.



Well, other than that 4 really is an expression, it's all correct.

In C, pretty much every statement is an expression. The only
statements that *aren't* expressions are usually if clauses, or
for, while or do...while loops, or jump statements such as return,
break, continue or goto, or switch statements.

But here is the problem with considering 4 to be an expression

Anexpression is a sequence of operators and operands that
specifies computation of a value, or that designates an object
or a function, or that generates side effects, or that
performs a combination thereof.

4 does not fall into that definition so we can't
consider it an expression, I don't think

I could be missing something though!
 
J

Joona I Palaste

But here is the problem with considering 4 to be an expression
Anexpression is a sequence of operators and operands that
specifies computation of a value, or that designates an object
or a function, or that generates side effects, or that
performs a combination thereof.
4 does not fall into that definition so we can't
consider it an expression, I don't think
I could be missing something though!

AFAIK 4 is a sequence of 1 operand, and 0 operators, that specifies
the computation of a value. That value is 4. I don't see anything wrong
with 4 meeting the above definition of an expression.


--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The obvious mathematical breakthrough would be development of an easy way to
factor large prime numbers."
- Bill Gates
 
P

Peter Pichler

Mantorok Redgormor said:
Joona I Palaste <[email protected]> wrote:

But here is the problem with considering 4 to be an expression

"The" problem? What problem?
Anexpression is a sequence of operators and operands that
specifies computation of a value, or that designates an object
or a function, or that generates side effects, or that
performs a combination thereof.

I cannot see a problem. 4 is an operand. An expression does not
necessarily need operators:

6.5.1 Primary expressions
1 primary-expression:
identifier
constant
string-literal
( expression )
Semantics
2 An identifier is a primary expression, provided it has been declared as
designating an
object (in which case it is an lvalue) or a function (in which case it is a
function
designator).76)
3 A constant is a primary expression. Its type depends on its form and
value, as detailed in
6.4.4.

Peter
 
A

Alan Balmer

"The" problem? What problem?


I cannot see a problem. 4 is an operand. An expression does not
necessarily need operators:

Even more clearly, 4 is a constant, therefore a primary expression by
the definition below.
 
P

pete

Dan said:
In other words, with exactly one exception (expression statements),
C statements aren't expressions.

Is this an expression:
(5)
?

N869
6.5 Expressions
[#1] An expression is a sequence of operators and operands
that specifies computation of a value, or that designates an
object or a function, or that generates side effects, or
that performs a combination thereof.

I see two punctuators and an integer constant,
but no operators and no operand.
I don't know if "computation" means "evaluation".
 
B

Barry Schwarz

Dan said:
In other words, with exactly one exception (expression statements),
C statements aren't expressions.

Is this an expression:
(5)
?

N869
6.5 Expressions
[#1] An expression is a sequence of operators and operands
that specifies computation of a value, or that designates an
object or a function, or that generates side effects, or
that performs a combination thereof.

I see two punctuators and an integer constant,
but no operators and no operand.
I don't know if "computation" means "evaluation".

The parentheses are an operator. They appear at the top of operator
table on page 52 of K&R II. Since you can't have an operator without
an operand, it seems pretty safe that the 5 is the operand.



<<Remove the del for email>>
 
J

Jeremy Yallop

Barry said:
Is this an expression:
(5)
? [..]
I see two punctuators and an integer constant,
but no operators and no operand.
I don't know if "computation" means "evaluation".

The parentheses are an operator. They appear at the top of operator
table on page 52 of K&R II. Since you can't have an operator without
an operand, it seems pretty safe that the 5 is the operand.

The parentheses in the precedence table in K&R refer to the function
call "operator", not grouping parentheses.

Jeremy.
 
P

Peter Nilsson

pete said:
Is this an expression:
(5)
?

See 6.5.1p5

A parenthesized expression is a primary expression. Its type and
value are identical to those of the unparenthesized expression.
It is an lvalue, a function designator, or a void expression if
the unparenthesized expression is, respectively, an lvalue, a
function designator, or a void expression.

[Aside: There was some discussion in csc about (0) not being a null pointer
constant. The oversight is in this paragraph.]
 
J

Jack Klein

in the standard, function arguments are never
referred to as expressions.

but we can certainly have expressions in function
arguments but not all arguments can qualify as
expressions.

func(4); /* 4 is not an expression */

int foo = 4;

func(foo); /*
* foo is an expression because it
* designates an object
*/


is this all correct?

No, it is not correct. In your example:

func(4);

....4 is indeed an expression. From the standard:

<quote>

6.5.1 Primary expressions
Syntax
1 primary-expression:
identifier
constant
string-literal
( expression )

Semantics
2 An identifier is a primary expression, provided it has been declared
as designating an object (in which case it is an lvalue) or a function
(in which case it is a function designator).

3 A constant is a primary expression. Its type depends on its form and
value, as detailed in 6.4.4.

<quote>

Section 6.6 then goes into greater detail about constant expressions.
 
P

pete

Mantorok said:
in the standard, function arguments are never
referred to as expressions.

N869
6.5.2.2 Function calls
Semantics
[#4] An argument may be an expression of any object type.
In preparing for the call to a function, the arguments are
evaluated, and each parameter is assigned the value of the
corresponding argument.
 
M

Mantorok Redgormor

Peter Pichler said:
"The" problem? What problem?


I cannot see a problem. 4 is an operand. An expression does not
necessarily need operators:

6.5.1 Primary expressions
1 primary-expression:
identifier
constant
string-literal
( expression )
Semantics
2 An identifier is a primary expression, provided it has been declared as
designating an
object (in which case it is an lvalue) or a function (in which case it is a
function
designator).76)
3 A constant is a primary expression. Its type depends on its form and
value, as detailed in
6.4.4.

Peter

Yeah but we can't consider primary expressions as
expressions can we?

The definition given for "expressions" is:

"An expression is a sequence of operators and
operands that specifies computation of a value,
or that designates an object or a function, or
that generates side effects, or that
performs a combination thereof."

given something like:

func(foo, 4);

we can't say that func takes two arguments
each of which is an expression

I would like to generalize it and say that
but I don't think it is correct given the
definition of expression.

We would have to say "foo" is an expression
and 4 is a primary expression, I think.

4 alone is not a sequence of operators and operands
(both are plural and imply more than one).
it doesn't specify computation of a value
since the value is a constant, no computation
is performed. it doesn't designate an object
or a function or give side-effects.

I'm only asking this because of the term
"value of the expression"

if 4 does not fall under the definition of expression
but instead the definition of primary expression

and we do the following:

int func(int);
...
func(4);
...
int func(int a)
{
...

do we say "a" has the value of the primary expression
"4"? though I don't see that term used in the standard.
I just see "value of the expression".
 
K

Keith Thompson

pete said:
Is this an expression:
(5)
?

N869
6.5 Expressions
[#1] An expression is a sequence of operators and operands
that specifies computation of a value, or that designates an
object or a function, or that generates side effects, or
that performs a combination thereof.

I see two punctuators and an integer constant,
but no operators and no operand.
I don't know if "computation" means "evaluation".

The grammar clearly implies that
(5)
or just
5
is an expression. (A constant is a primary-expression, which is a
postfix-expression, which (skipping a few steps) is an expression.)

The wording of the definition in 6.5 just barely misses capturing the
obvious intent. (No, 5 is not an operand in this context; the word
"operand" is defined in 6.4.6 as "an entity on which an operator
acts", and there's no operator.)

It's probably worthwhile to fix the wording, but not to spend too much
time worrying about it.
 
P

Peter Pichler

Mantorok Redgormor said:
Yeah but we can't consider primary expressions as
expressions can we?

See a parallel post by Keith Thompson or, even better, read the C gramar.
Constants are primary expressions. Primary expressions are expressions.
 
M

Martin Dickopp

Yeah but we can't consider primary expressions as expressions can we?

We can. Every primary-expression is a postfix-expression (6.5.2#1);
every postfix-expression is a unary-expression (6.5.3#1); every unary-
expression is a cast-expression (6.5.4#1); every cast-expression is a
multiplicative-expression (6.5.5#1); every multiplicative-expression is
an additive-expression (6.5.6#1); every additive-expression is a shift-
expression (6.5.7#1); every shift-expression is a relational-expression
(6.5.8#1); every relational-expression is an equality-expression
(6.5.9#1); every equality-expression is an AND-expression (6.5.10#1),
every AND-expression is an exclusive-OR-expression (6.5.11#1); every
exclusive-OR-expression is an inclusive-OR-expression (6.5.12#1); every
inclusive-OR-expression is a logical-AND-expression (6.5.13#1); every
logical-AND-expression is a logical-OR-expression (6.5.14#1); every
logical-OR-expression is a conditional-expression (6.5.15#1); every
conditional-expression is an assignment-expression (6.5.16#1); and
finally, every assignment-expression is an expression (6.5.17#1).
Therefore, every primary-expression is also an expression.

Martin
 
R

Richard Heathfield

Barry said:
The parentheses are an operator. They appear at the top of operator
table on page 52 of K&R II.

Page 53, actually. And yes, those parens actually mean "function call." :)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top