srand

C

CBFalconer

Lynn said:
Is the sentence below a statement or a function;

srand(time(NULL));

Since srand is a void function, it is a function call that does not
return a value. C functions are peculiar, in that they include
what are procedures in other languages. The call of the function
is a statement.
 
P

Peter Nilsson

You might like to explain what _you_ think and why.
As stated, your question just looks like homework.
Since srand is a void function, it is a function
call that does not return a value.

I don't see how that's relevant.
 C functions are peculiar, in that they include
what are procedures in other languages.

You mean other languages are peculiar because they
have a separate name for functions that map to an
empty set. ;)
 The call of the function is a statement.

No, that's an expression. At least one more thing
is needed to make it a statement.
 
K

Keith Thompson

Lynn said:
Excuse me.
Is the sentence below a statement or a function;
srand(time(NULL));

C doesn't have anything called a "sentence"; "line" would be a better
term.

srand and time are functions. srand(time(NULL)) is a function call,
whose single argument happens to be another function call. A function
call is one form of expression. An expression followed by a semicolon
is one form of statement.
 
W

WANG Cong

Peter said:
No, that's an expression. At least one more thing
is needed to make it a statement.

Yes, it is, according to C99.

6.5
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.

6.8
A statement specifies an action to be performed. Except as indicated,
statements are executed in sequence.
 
B

Ben Bacarisse

WANG Cong said:
Yes, it is, according to C99.

6.5
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.

6.8
A statement specifies an action to be performed. Except as indicated,
statements are executed in sequence.

What has that got to do with it? Peter Nilsson is referring to the
';' that (in this case) turns the function call into expression into
an expression statement. A function call on its own is not a
statement.

Curiously, it is possible to turn a function call into an expression
without adding a semicolon, which is presumably why he said "at least
one more thing" rather than saying you need to add a semicolon.
 
K

Keith Thompson

WANG Cong said:
Yes, it is, according to C99.

6.5
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.

6.8
A statement specifies an action to be performed. Except as indicated,
statements are executed in sequence.

Those are not (and I presume are not intended to be) rigorous
definitions of the words "expression" and "statement". And in fact,
if interpreted literally, the above definition of "expression" is
incorrect. For example, ``42'' is obviously an expression, but it has
no operators or operands (42 can't be an operand unless it's the
operand of some operator), so it doesn't satisfy the definition.

To understand exactly what is and is not an expression, and what is
and is not a statement, you have to look at the grammar. Note that
expressions and statements are disjoint; no one construct can be both
an expression and a statement.

The line in question was:
srand(time(NULL));
This:
srand(time(NULL))
is an expression. This:
srand(time(NULL));
is a statement. The difference is the semicolon.

Strictly speaking, a function call is a kind of expression (one of the
9 kinds of postfix-expression), *not* a kind of statement. Given a
function call, you can always create a statement from it by adding a
semicolon, but it's the combination of the function call and the
semicolon that forms a statement. But informally, it's ok to refer to
srand(time(NULL);
as a function call.
 
P

Paul Hsieh

Excuse me.
 Is the sentence below a statement or a function;
srand(time(NULL));

In total, its an expression. So between the two, statement is
closer. In C, functions are called or declared or defined. In the
above case there are two functions being called, but the nested way in
which they are connected (the result of time(NULL) being fed to
srand()) is an expression.
 
K

Keith Thompson

Keith Thompson said:
Strictly speaking, a function call is a kind of expression (one of the
9 kinds of postfix-expression), *not* a kind of statement. Given a
function call, you can always create a statement from it by adding a
semicolon, but it's the combination of the function call and the
semicolon that forms a statement. But informally, it's ok to refer to
srand(time(NULL);
as a function call.

Before anybody else points it out, yes, I left out a ')'. I meant to
say that, informally, it's ok to refer to
srand(time(NULL));
as a function call.
 
K

Keith Thompson

Paul Hsieh said:
In total, its an expression. So between the two, statement is
closer. In C, functions are called or declared or defined. In the
above case there are two functions being called, but the nested way in
which they are connected (the result of time(NULL) being fed to
srand()) is an expression.

No, it's not an expression. Take a look at the grammar.
srand(time(NULL));
with the semicolon is a statement, not an expression.
srand(time(NULL))
without the semicolon is an expression, not a statement.

The latter matches each of the following named non-terminals:

postfix-expression
unary-expression
cast-expression
multiplicative-expression
additive-expression
shift-expression
relational-expression
equality-expression
AND-expression
exclusive-OR-expression
inclusive-OR-expression
logical-AND-expression
logical-OR-expression
conditional-expression
assignment-expression
expression

It would match function-call if that were a named non-terminal, but
that's just listed as one of the 9 forms of postfix-expression:
postfix-expression ( argument-expression-list_opt )

Of course, if you're not either writing a C parser or trying to track
down a subtle syntax error, it's sufficient just to say that it's an
expression.
 
P

Phil Carmody

Ben Bacarisse said:
Yes, typo.


That's what I had in mind.

That and the grossly larger
switch(func()){}
while(bar(),0){}
cover all the bases, I think.

Phil
 
B

Ben Bacarisse

.... statement (corrected so I can snip some more) without adding a ;
That and the grossly larger
switch(func()){}
while(bar(),0){}

In general (and in the specific case that started this sub-topic) they
all need the 'call(), 0' trick in order to be valid C when the return
type is void.
cover all the bases, I think.

Well, you can add an else to the if. That has the advantage (shared
by the switch and the while) that the resulting construct plays the
(syntactic) role of a full statement which will not alter the parsing
of anything that follows. The elseless if does not have that bonus.
 

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top