The order of block declaration

Discussion in 'C Programming' started by lovecreatesbea...@gmail.com, Feb 8, 2008.

  1. Guest

    Hi!

    `declaration' appears before `statement' within local block in the
    following diagram. So, does C specify the order of block declaration
    just by this? Is there other description about the order of block
    declaration? I can't find it in n1124.pdf.

    <quote>
    (6.8.2) block-item:
    declaration
    statement
    </quote>

    Thank you for your time.
    , Feb 8, 2008
    #1
    1. Advertising

  2. santosh Guest

    wrote:

    > Hi!
    >
    > `declaration' appears before `statement' within local block in the
    > following diagram. So, does C specify the order of block declaration
    > just by this? Is there other description about the order of block
    > declaration? I can't find it in n1124.pdf.
    >
    > <quote>
    > (6.8.2) block-item:
    > declaration
    > statement
    > </quote>
    >
    > Thank you for your time.


    C99 allows mixed code and declarations. All versions of C before C99
    mandated that declarations precede code within a block.
    santosh, Feb 8, 2008
    #2
    1. Advertising

  3. Guest

    On Feb 9, 12:13 am, santosh <> wrote:
    > wrote:
    > > Hi!

    >
    > > `declaration' appears before `statement' within local block in the
    > > following diagram. So, does C specify the order of block declaration
    > > just by this? Is there other description about the order of block
    > > declaration? I can't find it in n1124.pdf.

    >
    > > <quote>
    > >   (6.8.2) block-item:
    > >     declaration
    > >     statement
    > > </quote>

    >
    > > Thank you for your time.

    >
    > C99 allows mixed code and declarations. All versions of C before C99
    > mandated that declarations precede code within a block.


    Thank you. I remember that some earlier versions don't allow
    declarations in nested block in a function, do they?
    , Feb 8, 2008
    #3
  4. santosh Guest

    wrote:

    > On Feb 9, 12:13 am, santosh <> wrote:
    >> wrote:
    >> > Hi!

    >>
    >> > `declaration' appears before `statement' within local block in the
    >> > following diagram. So, does C specify the order of block
    >> > declaration just by this? Is there other description about the
    >> > order of block declaration? I can't find it in n1124.pdf.

    >>
    >> > <quote>
    >> > (6.8.2) block-item:
    >> > declaration
    >> > statement
    >> > </quote>

    >>
    >> > Thank you for your time.

    >>
    >> C99 allows mixed code and declarations. All versions of C before C99
    >> mandated that declarations precede code within a block.

    >
    > Thank you. I remember that some earlier versions don't allow
    > declarations in nested block in a function, do they?


    As far as I know, all versions of Standard C allow declarations in
    nested blocks.
    santosh, Feb 8, 2008
    #4
  5. santosh <> writes:
    [...]
    > C99 allows mixed code and declarations. All versions of C before C99
    > mandated that declarations precede code within a block.


    That declarations precede *statements* in a block. (Declarations are
    arguably "code".)

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 8, 2008
    #5
  6. Army1987 Guest

    Keith Thompson wrote:

    > That declarations precede *statements* in a block. (Declarations are
    > arguably "code".)


    IMO they are, but, for example, gcc says "C90 doesn't allow mixed
    declaration and code".
    I don't see the point for *that* definition of "code", but...



    --
    Army1987 (Replace "NOSPAM" with "email")
    Army1987, Feb 8, 2008
    #6
  7. CJ Guest

    On Feb 8, 2:50 pm, Army1987 <> wrote:
    > Keith Thompson wrote:
    > > That declarations precede *statements* in a block. (Declarations are
    > > arguably "code".)

    >
    > IMO they are, but, for example, gcc says "C90 doesn't allow mixed
    > declaration and code".
    > I don't see the point for *that* definition of "code", but...
    >
    > --
    > Army1987 (Replace "NOSPAM" with "email")


    I think gcc is saying syntax such as "for(int ii=0;ii < 1;ii++) ;" is
    not allowed, a statement containing a declaration, "int ii."

    As for "Declarations are arguably 'code'" in-so-far registers are
    assigned values, I can agree. But CPU registers are not "hidden" from
    the c programmer. In assembler, declarations are indeed code, IMHO.
    CJ, Feb 9, 2008
    #7
  8. CJ <> writes:
    > On Feb 8, 2:50 pm, Army1987 <> wrote:
    >> Keith Thompson wrote:
    >> > That declarations precede *statements* in a block. (Declarations are
    >> > arguably "code".)

    >>
    >> IMO they are, but, for example, gcc says "C90 doesn't allow mixed
    >> declaration and code".
    >> I don't see the point for *that* definition of "code", but...

    >
    > I think gcc is saying syntax such as "for(int ii=0;ii < 1;ii++) ;" is
    > not allowed, a statement containing a declaration, "int ii."
    >
    > As for "Declarations are arguably 'code'" in-so-far registers are
    > assigned values, I can agree. But CPU registers are not "hidden" from
    > the c programmer. In assembler, declarations are indeed code, IMHO.


    I think you meant to say that CPU registers are hidden from the C
    programmer (i.e., you had an extra "not"). But I fail to see what
    this has to do with the question of whether declarations are "code".

    We're never going to settle this definitively, since there's no formal
    definition of the word "code" (at least not in the C standard). But
    clearly both declarations and statements can involve the evaluation of
    expressions, including function calls, which can result in the
    execution of statements. A declaration is executed in much the same
    sense that a statement is. (In Ada, for example, the execution of a
    declaration is called "elaboration": statements are executed,
    declarations are elaborated, and expressions are evaluated.)

    Consider:

    #include <stdio.h>
    int main(void)
    {
    /* A declaration that performs output: */
    int dummy = printf("Hello, world\n");

    /* A statement that does nothing: */
    ;

    return 0;
    }

    I'd call that declaration "code".

    It's not a big deal either way, though (<OT>but I'd like to see the
    gcc warning "ISO C90 forbids mixed declarations and code" changed to
    "ISO C90 forbids mixed declarations and statements"</OT>).

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 9, 2008
    #8
  9. Bill Leary Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Consider:
    >
    > #include <stdio.h>
    > int main(void)
    > {
    > /* A declaration that performs output: */
    > int dummy = printf("Hello, world\n");
    >
    > /* A statement that does nothing: */
    > ;
    >
    > return 0;
    > }
    >
    > I'd call that declaration "code".


    I'd always just taken that as another way C let's you be concise.

    The line
    int dummy = printf("Hello, world\n");

    (and it's like) I've been interpreting as

    int dummy;
    printf("Hello, world\n");

    for all these years. That is, I always saw it as a short hand form of a
    declaration and code.

    - Bill
    Bill Leary, Feb 10, 2008
    #9
  10. "Bill Leary" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> Consider:
    >>
    >> #include <stdio.h>
    >> int main(void)
    >> {
    >> /* A declaration that performs output: */
    >> int dummy = printf("Hello, world\n");
    >>
    >> /* A statement that does nothing: */
    >> ;
    >>
    >> return 0;
    >> }
    >>
    >> I'd call that declaration "code".

    >
    > I'd always just taken that as another way C let's you be concise.


    Yes, it's that too.

    > The line
    > int dummy = printf("Hello, world\n");
    >
    > (and it's like) I've been interpreting as
    >
    > int dummy;
    > printf("Hello, world\n");
    >
    > for all these years. That is, I always saw it as a short hand form of
    > a declaration and code.


    Actually, it's equivalent to:

    int dummy;
    dummy = printf("Hello, world\n");

    The question is what the word "code" means. I see no reason to limit
    it to C statements. The real point is that, since there seems to be
    no consensus on what the word "code" really means, it's better to use
    an unambiguous term. If you want to talk about statements, call them
    "statements".

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Feb 10, 2008
    #10
  11. Bill Leary Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "Bill Leary" <> writes:
    >> The line
    >> int dummy = printf("Hello, world\n");
    >>
    >> (and it's like) I've been interpreting as
    >>
    >> int dummy;
    >> printf("Hello, world\n");
    >>
    >> for all these years. That is, I always saw it as a short hand form of
    >> a declaration and code.

    >
    > Actually, it's equivalent to:
    >
    > int dummy;
    > dummy = printf("Hello, world\n");


    Thanks. That's what I meant. I've never been any damn good at proof
    reading my own writing.

    > The question is what the word "code" means. I see no reason to limit
    > it to C statements. The real point is that, since there seems to be
    > no consensus on what the word "code" really means, it's better to use
    > an unambiguous term. If you want to talk about statements, call them
    > "statements".


    I'll buy that. When I first wrote the above, I actually typed "...short
    hand form of a declaration and a statement." then changed it to "code" to
    conform with what I thought folks were using here. I think my first impulse
    there was the more correct one.

    - Bill
    Bill Leary, Feb 11, 2008
    #11
  12. CJ Guest

    On Feb 11, 9:04 am, "Bill Leary" <> wrote:
    > "Keith Thompson" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > "Bill Leary" <> writes:
    > >> The line
    > >> int dummy = printf("Hello, world\n");

    >
    > >> (and it's like) I've been interpreting as

    >
    > >> int dummy;
    > >> printf("Hello, world\n");

    >
    > >> for all these years. That is, I always saw it as a short hand form of
    > >> a declaration and code.

    >
    > > Actually, it's equivalent to:

    >
    > > int dummy;
    > > dummy = printf("Hello, world\n");

    >
    > Thanks. That's what I meant. I've never been any damn good at proof
    > reading my own writing.
    >
    > > The question is what the word "code" means. I see no reason to limit
    > > it to C statements. The real point is that, since there seems to be
    > > no consensus on what the word "code" really means, it's better to use
    > > an unambiguous term. If you want to talk about statements, call them
    > > "statements".

    >
    > I'll buy that. When I first wrote the above, I actually typed "...short
    > hand form of a declaration and a statement." then changed it to "code" to
    > conform with what I thought folks were using here. I think my first impulse
    > there was the more correct one.
    >
    > - Bill


    I have to agree, the declaration is "int dummy" and the code is
    "printf("Hello, world\n");". But, we deal in syntax, not semantics.

    I guess this yet another example of "six of one, half a dozen of
    another."
    cj
    CJ, Feb 11, 2008
    #12
    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. Noah
    Replies:
    5
    Views:
    956
  2. Alex Vinokur
    Replies:
    4
    Views:
    583
    Jonathan Turkanis
    Apr 5, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    507
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,227
    Richard Herring
    May 17, 2005
  5. morrell
    Replies:
    1
    Views:
    949
    roy axenov
    Oct 10, 2006
Loading...

Share This Page