K&R2 variable definition

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

    #1

    K&R2 variable definition

    Hello everybody,
    I cannot find where in the K&R2 it is stated that variables must be
    defined at the beginning
    of a block and not within other lines.
    In Section 1.2 I read:
    In C, all variables must be declared before they are used, usually at
    the beginning of the
    function before any executable statements.
    USUALLY does not mean ABSOLUTELY, correct?
    I know that in C99 it is possible, but I'm interested in C89.
    Can anyone tell me something about?
    Thank you!
    Claus

  • Zara

    #2
    Re: K&R2 variable definition

    On 26 Oct 2006 06:42:38 -0700, "Clausfor" <clausfor@gmail .comwrote:
    >Hello everybody,
    >I cannot find where in the K&R2 it is stated that variables must be
    >defined at the beginning
    >of a block and not within other lines.
    >In Section 1.2 I read:
    In C, all variables must be declared before they are used, usually at
    >the beginning of the
    function before any executable statements.
    >USUALLY does not mean ABSOLUTELY, correct?
    >I know that in C99 it is possible, but I'm interested in C89.
    >Can anyone tell me something about?
    >Thank you!
    Claus
    Take a look at page 25:

    "A funtion definition has this form:

    return-type function-name (parameter declarations, if any)
    {
    declarations
    statements
    }
    "

    That's it, and it's clear

    Zara

    Comment

    • Richard Tobin

      #3
      Re: K&amp;R2 variable definition

      In article <1161870158.830 511.229950@m73g 2000cwd.googleg roups.com>,
      Clausfor <clausfor@gmail .comwrote:
      In C, all variables must be declared before they are used, usually at
      >the beginning of the
      function before any executable statements.
      >USUALLY does not mean ABSOLUTELY, correct?
      You can declare variable in inner blocks:

      int foo(void)
      {
      int a;
      ...
      {
      int b;
      ...
      }
      }

      -- Richard

      Comment

      • Clausfor

        #4
        Re: K&amp;R2 variable definition

        Yes... clear... thank you!
        Claus

        Comment

        • Chris Dollin

          #5
          Re: K&amp;R2 variable definition

          Clausfor wrote:
          Hello everybody,
          I cannot find where in the K&R2 it is stated that variables must be
          defined at the beginning
          of a block and not within other lines.
          Try the grammar.

          (Can't cite page & name, as my copy of K&R2 is at home.)

          --
          Chris "back home once again ...." Dollin
          "Reaching out for mirrors hidden in the web." - Renaissance, /Running Hard/

          Comment

          • Richard Heathfield

            #6
            Re: K&amp;R2 variable definition

            Clausfor said:
            Hello everybody,
            I cannot find where in the K&R2 it is stated that variables must be
            defined at the beginning
            of a block and not within other lines.
            You're looking for an explicit prohibition, but you won't find one. What you
            have instead is absence of permission. See page 236, grammar for
            compound-statement, which shows that declarations *may* occur at the
            beginning of a compound statement. Now look for other places in the grammar
            where declarations *may* occur, and look at... well, at what isn't there.

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at above domain (but drop the www, obviously)

            Comment

            Working...