For those who like quoting chapter and verse...

K

Kenny McCormack

Given the assumption that anywhere (with two exceptions that I know of; see
next} that you have stuff inside curly braces, you can remove the curly
braces if there is one one "thing" inside them, ...

The exceptions that I know of: 1) A function definition must start with "{".
2) A switch statement must have "{" and "}", even if it has only one case.

Anyway, given this assumption, why won't this program compile (see below):

$ cat x.c
int main(void) {
int i = 1;
if (i) int j;
return 0;
}
$ gcc x.c
x.c: In function ?main?:
x.c:3:8: error: expected expression before ?int?
$

Note that the above *will* compile if I encloe "int j;" in curlies.
 
A

Anand Hariharan

On Feb 26, 3:37 pm, (e-mail address removed) (Kenny McCormack)
wrote:

At the outset, IANAL, so I do not belong to the target audience (as
indicated in your subject). That said, I suspect that those can
quote chapter and verse in this group have most likely kill-filed
you that they may not have seen your post at all.

Given the assumption that anywhere (with two exceptions that I know of; see
next} that you have stuff inside curly braces, you can remove the curly
braces if there is one one "thing" inside them, ...

The exceptions that I know of: 1) A function definition must start with "{".
2) A switch statement must have "{" and "}", even if it has only one case..

Anyway, given this assumption, why won't this program compile (see below):

$ cat x.c
int main(void) {
int i = 1;
if (i) int j;
return 0;}

$ gcc x.c
x.c: In function ?main?:
x.c:3:8: error: expected expression before ?int?
$

Note that the above *will* compile if I encloe "int j;" in curlies.

One may have to get down to the grammar and see what the productions
say.

It is apparent that while a declaration such as -

int i = 0, j = 0;

- is allowed outside of any function, a statement such as -

i = j;

- is not allowed outside any function. So, there must be something in
the grammar that distinguishes a declaration from a statement.

I suspect the grammar might say that the statement following an if
condition cannot be composed of just a declaration alone, but must
either be a block or a statement.

- Anand
 
Ö

Öö Tiib

int main(void) {
int i = 1;
if (i) int j;
return 0;
}

It is C++, where declaration of variable may
have side effects. Your code is out of topic here being C++.

In C it is illegal (at least in C99 it was):

(6.8) statement
It can be
expression-statement
compound-statement
and several others none is declaration.

(6.8.2) compound statement
it is
{ declarations and statements in mix }

(6.8.3) expression-statement
optional expression and semicolon.
expression is disallowed to be declaration.

(6.8.4) selection-statement
it is
if (expression) statement
but is not such thing as
if (expression) declaration

So no way your code should work in C99. Also it is pointless in C anyway.
 
S

Shao Miller

Given the assumption that anywhere (with two exceptions that I know of; see
next} that you have stuff inside curly braces, you can remove the curly
braces if there is one one "thing" inside them, ...

The exceptions that I know of: 1) A function definition must start with "{".

It starts like a function declaration, but must have parentheses, then
has a compound statement, yes.
2) A switch statement must have "{" and "}", even if it has only one case.

You are mistaken about this. 'switch' doesn't require them.
Anyway, given this assumption, why won't this program compile (see below):

$ cat x.c
int main(void) {
int i = 1;
if (i) int j;
return 0;
}
$ gcc x.c
x.c: In function ?main?:
x.c:3:8: error: expected expression before ?int?
$

Note that the above *will* compile if I encloe "int j;" in curlies.

'if' requires a statement, where you have a declaration. A compound
statement can have declarations and statements inside.
 
J

James Kuyper

On Feb 26, 3:37 pm, (e-mail address removed) (Kenny McCormack)
wrote:

At the outset, IANAL, so I do not belong to the target audience (as
indicated in your subject). That said, I suspect that those can
quote chapter and verse in this group have most likely kill-filed
you that they may not have seen your post at all.
Correct.

His assumption is incorrect when the curly braces don't begin and
terminate a compound statement. For example, you cannot drop the curly
braces in the following:

int array[1] = {1};

Even when the curly brackets do mark the beginning and end of a compound
statement, his assumption still can fail if the only "thing" inside a
compound statement is a declaration. That's because removing the curly
brackets would replace a statement with with a declaration. Most
locations where compound statements are allowed are NOT locations where
declarations are allowed (see the grammar summary in A.2.3), and that's
what went wrong in his example code below. The only exceptions are
within another compound statement (6.8.2p1), and in function definitions
(as he notes below).

No, there's no such requirement. Neither of the following switch
statements is a syntax error, nor do they violate any constraints.

switch(i++) j = 3;
switch(j) case 5: i = 4;

Those statements are functionally equivalent to:

i++;
if(j==5) i = 4;

which is a much better way to write such code - but there's nothing
wrong with those switch() statements, as far as the C standard is
concerned, just because they are don't have any curly brackets.

If the compound statement controlled by a switch() contained only a
single declaration, removing the curly brackets would be a syntax error,
for the same reason as for the if() statement below .

Given that assumption, the program should compile. The problem is with
the assumption, which is false.

In particular, the grammar rules for if statements say:

if ( expression ) statement
or
if ( expression ) statement else statement


"int j;" is a declaration, so it it doesn't match either form.

{int j;} is a compound statement, and therefore a statement (6.8p1), so
it matches the first grammar rule for if statements.
One may have to get down to the grammar and see what the productions
say.

Definitely. Talking about "stuff" and "things" is just not sufficiently
precise to resolve questions like this.
It is apparent that while a declaration such as -

int i = 0, j = 0;

- is allowed outside of any function, a statement such as -

i = j;

- is not allowed outside any function. So, there must be something in
the grammar that distinguishes a declaration from a statement.

Lots of things, mostly in section 6.8.
I suspect the grammar might say that the statement following an if
condition cannot be composed of just a declaration alone, but must
either be a block or a statement.

Actually, no - it's simpler than that - a compound statement is a
statement (6.8p1), so "statement" is sufficient.

All compound statements are blocks (6.8.2), but so are selection
statements and their associated sub statements (6.8.4p3), iteration
statements and the bodies of their loops (6.8.5p5), even if none of
those things is a compound statement.

Therefore, it actually works the other way around: in the statement

if(i) j = 3;

the sub-statement "j=3" is a block, because of it's position in the if
statement.
 
A

army1987

No, there's no such requirement. Neither of the following switch
statements is a syntax error, nor do they violate any constraints.

switch(i++) j = 3;
switch(j) case 5: i = 4;

Those statements are functionally equivalent to:

i++;
if(j==5) i = 4;

which is a much better way to write such code - but there's nothing
wrong with those switch() statements, as far as the C standard is
concerned, just because they are don't have any curly brackets.

Interesting. I might use that should I ever decide to participate in the
IOCCC.
 
S

Shao Miller

Interesting. I might use that should I ever decide to participate in the
IOCCC.

Yeah. Suppose you wish to hook 'return' statements:

#define return \
switch (1) \
while (1) \
if (0) { \
default: \
do_something(); \
} \
else \
return

This 'switch' will invoke the 'default' case, then 'do_something()',
then continue the 'while', which then invokes the 'if', whose condition
is false, so then invokes the 'else', which performs the 'return'.
(Note the trailing space after the final 'return'.)
 
J

James Kuyper

....
brackets would replace a statement with with a declaration. Most
locations where compound statements are allowed are NOT locations where
declarations are allowed (see the grammar summary in A.2.3), and that's
what went wrong in his example code below. The only exceptions are
within another compound statement (6.8.2p1), and in function definitions
(as he notes below).

I just re-read that, and realized that I got a little confused while
editing it. Function definitions are not exceptions to my assertion that
"Most locations where compound statements are allowed are NOT locations
where declarations are allowed". It is precisely because of that fact
that they ARE exceptions to his assumption.
 
N

Nick Keighley

On Feb 26, 3:37 pm, (e-mail address removed) (Kenny McCormack)
wrote:

At the outset, IANAL, so I do not belong to the target audience (as
indicated in your subject).  That said, I suspect that those can
quote chapter and verse in this group have most likely kill-filed
you that they may not have seen your post at all.

Everything I've seen recently looks perfectly sensible. Perhaps its
time to pop him out of the kill files for a while.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top