>
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.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
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.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
>
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.
>>
>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.
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.
>>
>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.
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.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Oct 29, 8:35 pm, Lynn <lowski_0...@16 3.comwrote:
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.
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.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On Oct 29, 8:35 pm, Lynn <lowski_0...@16 3.comwrote:
>Excuse me.
> Is the sentence below a statement or a function;
>srand(time(NUL L));
>
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:
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.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
In article <87wsfqc5jy.fsf @bsb.me.uk>, Ben Bacarisse
<ben.usenet@bsb .me.ukwrote:
WANG Cong <xiyou.wangcong @gmail.comwrite s:
>
Peter Nilsson wrote:
CBFalconer <cbfalco...@yah oo.comwrote:
>>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.
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.
Isn't a function call already an expression? Do you mean turn a function
call into a statement without adding a semicolon? If so, all I'm coming up
with is putting it in a condition:
In article <87wsfqc5jy.fsf @bsb.me.uk>, Ben Bacarisse
<ben.usenet@bsb .me.ukwrote:
<snip>
>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.
>
Isn't a function call already an expression? Do you mean turn a function
call into a statement without adding a semicolon?
Yes, typo.
If so, all I'm coming up
with is putting it in a condition:
>
if ( func() ) { } // no semicolon needed
>In article <87wsfqc5jy.fsf @bsb.me.uk>, Ben Bacarisse
><ben.usenet@bs b.me.ukwrote:
<snip>
>>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.
>>
>Isn't a function call already an expression? Do you mean turn a function
>call into a statement without adding a semicolon?
>
Yes, typo.
>
>If so, all I'm coming up
>with is putting it in a condition:
>>
> if ( func() ) { } // no semicolon needed
>
That's what I had in mind.
That and the grossly larger
switch(func()){ }
while(bar(),0){ }
cover all the bases, I think.
Phil
--
We must respect the other fellow's religion, but only in the sense and to the
extent that we respect his theory that his wife is beautiful and his children
smart. -- Henry Louis Mencken (1880-1956), American editor and critic
Comment