The order of block declaration

  • Thread starter lovecreatesbea...
  • Start date
L

lovecreatesbea...

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

santosh

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

lovecreatesbea...

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?
 
K

Keith Thompson

santosh said:
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".)
 
A

Army1987

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

CJ

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

Keith Thompson

CJ said:
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>).
 
B

Bill Leary

Keith Thompson said:
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
 
K

Keith Thompson

Bill Leary said:
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".
 
B

Bill Leary

Keith Thompson said:
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
 
C

CJ

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top