srand

Discussion in 'C Programming' started by Lynn, Oct 30, 2008.

  1. Lynn

    Lynn Guest

    Excuse me.
    Is the sentence below a statement or a function;
    srand(time(NULL));
    Lynn, Oct 30, 2008
    #1
    1. Advertising

  2. Lynn

    CBFalconer Guest

    Lynn wrote:
    >
    > 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.
    CBFalconer, Oct 30, 2008
    #2
    1. Advertising

  3. CBFalconer <> wrote:
    > Lynn wrote:
    > > Is the sentence below a statement or a function;


    You might like to explain what _you_ think and why.
    As stated, your question just looks like homework.

    > > srand(time(NULL));

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

    --
    Peter
    Peter Nilsson, Oct 30, 2008
    #3
  4. Lynn <> writes:
    > 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_Keith) <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"
    Keith Thompson, Oct 30, 2008
    #4
  5. Lynn

    WANG Cong Guest

    Peter Nilsson wrote:

    > CBFalconer <> wrote:
    >>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.
    WANG Cong, Oct 30, 2008
    #5
  6. WANG Cong <> writes:

    > Peter Nilsson wrote:
    >
    >> CBFalconer <> wrote:
    >>>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.

    --
    Ben.
    Ben Bacarisse, Oct 30, 2008
    #6
  7. WANG Cong <> writes:
    > Peter Nilsson wrote:
    >
    >> CBFalconer <> wrote:
    >>>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.


    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_Keith) <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"
    Keith Thompson, Oct 30, 2008
    #7
  8. Lynn

    Paul Hsieh Guest

    On Oct 29, 8:35 pm, Lynn <> wrote:
    > 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.

    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
    Paul Hsieh, Oct 30, 2008
    #8
  9. Keith Thompson <> writes:
    [...]
    > 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_Keith) <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"
    Keith Thompson, Oct 30, 2008
    #9
  10. Paul Hsieh <> writes:
    > On Oct 29, 8:35 pm, Lynn <> wrote:
    >> 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.


    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.

    --
    Keith Thompson (The_Other_Keith) <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"
    Keith Thompson, Oct 30, 2008
    #10
  11. (blargg) writes:

    > In article <>, Ben Bacarisse
    > <> wrote:

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

    --
    Ben.
    Ben Bacarisse, Oct 31, 2008
    #11
  12. Lynn

    Phil Carmody Guest

    Ben Bacarisse <> writes:
    > (blargg) writes:
    >
    >> In article <>, Ben Bacarisse
    >> <> wrote:

    > <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
    Phil Carmody, Oct 31, 2008
    #12
  13. Phil Carmody <> writes:

    > Ben Bacarisse <> writes:
    >> (blargg) writes:
    >>
    >>> In article <>, Ben Bacarisse
    >>> <> wrote:

    >> <snip>
    >>>> Curiously, it is possible to turn a function call into a

    .... statement (corrected so I can snip some more) without adding a ;

    >>> 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){}


    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.

    --
    Ben.
    Ben Bacarisse, Oct 31, 2008
    #13
    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. Graeme

    srand() question

    Graeme, Jul 29, 2003, in forum: C++
    Replies:
    6
    Views:
    703
    Karl Heinz Buchegger
    Jul 29, 2003
  2. August1
    Replies:
    4
    Views:
    7,723
    August1
    Dec 8, 2003
  3. David Shaw

    Why isn't srand working?

    David Shaw, Mar 3, 2004, in forum: C++
    Replies:
    14
    Views:
    669
    CBFalconer
    Mar 5, 2004
  4. August1
    Replies:
    0
    Views:
    7,503
    August1
    May 16, 2004
  5. Arijit Das

    srand versus srandom - srand with random() safe?

    Arijit Das, Oct 17, 2011, in forum: C Programming
    Replies:
    12
    Views:
    3,004
    Nick Keighley
    Oct 18, 2011
Loading...

Share This Page