srand

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lynn

    #1

    srand

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

    #2
    Re: srand

    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.

    Comment

    • Peter Nilsson

      #3
      Re: srand

      CBFalconer <cbfalco...@yah oo.comwrote:
      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

      Comment

      • Keith Thompson

        #4
        Re: srand

        Lynn <lowski_0409@16 3.comwrites:
        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"

        Comment

        • WANG Cong

          #5
          Re: srand

          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.

          Comment

          • Ben Bacarisse

            #6
            Re: srand

            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.

            --
            Ben.

            Comment

            • Keith Thompson

              #7
              Re: srand

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

              Comment

              • Paul Hsieh

                #8
                Re: srand

                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.

                --
                Paul Hsieh
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


                Comment

                • Keith Thompson

                  #9
                  Re: srand

                  Keith Thompson <kst-u@mib.orgwrites :
                  [...]
                  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"

                  Comment

                  • Keith Thompson

                    #10
                    Re: srand

                    Paul Hsieh <websnarf@gmail .comwrites:
                    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:

                    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_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"

                    Comment

                    • blargg

                      #11
                      Re: srand

                      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:

                      if ( func() ) { } // no semicolon needed

                      Comment

                      • Ben Bacarisse

                        #12
                        Re: srand

                        blargg.h4g@gish puppy.com (blargg) writes:
                        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
                        That's what I had in mind.

                        --
                        Ben.

                        Comment

                        • Phil Carmody

                          #13
                          Re: srand

                          Ben Bacarisse <ben.usenet@bsb .me.ukwrites:
                          blargg.h4g@gish puppy.com (blargg) writes:
                          >
                          >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

                          Working...