Legal C or bug in gcc

M

Micah Cowan

Richard said:
CBFalconer said:


If that is true, the following code will compile, and will *not* print
"Hmmm". Otherwise, the compilation will fail.

...."presence of [an immediately] following 'else'."...

Howzat?
 
R

Richard Tobin

The semicolon brings the if to an end.
[/QUOTE]
In Pascal. Not in C. In C the definitive thing is the presence of
a following 'else'.

In C, the definitive thing is matching the grammar, which in this case
means that you can't just look for an else, a semicolon, or a closing
brace. You have to look for the end of the "then" part, then look to
see if there's an else immediately following.

-- Richard
 
C

CBFalconer

Richard said:
CBFalconer said:

If that is true, the following code will compile, and will *not* print
"Hmmm". Otherwise, the compilation will fail.

#include <stdio.h>

int main(void)
{
int a = 0;
if(1 == 1)
a = 1;
printf("Hmmm\n");
else
NULL;
return 0;
}

That has a following printf, not a following else. As you knew
anyhow. However, in Pascal:

IF true THEN a := 1;
ELSE (* anything *)

will bring up large compiler screams at the ELSE. Remove the semi
and all is well.
 
R

Richard Heathfield

CBFalconer said:
That has a following printf, not a following else.

Er, that else sure looks like a following else to me.

Here's another counter-example:

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

According to you, that 'if' has no end (because there is no following
'else' present).
 
R

Richard Heathfield

Mark McIntyre said:
No, the else is following the printf.

Yes, and the printf is following the assignment, and the assignment is
following the 'if'.

You know what I mean, and I know what you mean. The point is that the
original claim was not only ambiguous but also incorrect. An 'if' is *not*
terminated by an 'else', but by the statement that follows it and,
*optionally*, an 'else'.

The grammar is:

selection-statement:
if ( expression ) statement
if ( expression ) statement else statement
 
M

Mark McIntyre

Richard said:
Mark McIntyre said:


Yes, and the printf is following the assignment, and the assignment is
following the 'if'.

You know what I mean, and I know what you mean.

Then why continue to argue? You made your (valid) point in the first
post, the rest has IMHO been posturing.
 
A

Antoninus Twink

Then why continue to argue? You made your (valid) point in the first
post, the rest has IMHO been posturing.

Miaow!

Even Psycho Mackintyre has finally realized that HeathField goes in for
posturing in a big way.
 
C

CBFalconer

Richard said:
CBFalconer said:

Er, that else sure looks like a following else to me.
Here's another counter-example:

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

According to you, that 'if' has no end (because there is no following
'else' present).

I said 'definitive thing', not 'end'. And was talking about the
presence/absence of the following 'else'. I don't think I have
been sloppy this time. Also don't count line endings as
significant in either language. They're all white space.
 
R

Richard Heathfield

CBFalconer said:
Richard Heathfield wrote:


I said 'definitive thing', not 'end'.

Look at the context - the thing to which you were replying was: "The
semicolon brings the if to an end." You said that this was true in Pascal
but not in C, and /then/ said: "In C the definitive thing is the presence
of a following 'else'." The obvious way to read this is that you were
claiming the presence of the following 'else' is the definitive thing that
marks the end of the 'if'.

If you didn't mean that, fine, we're arguing over nothing, so let's stop.
 

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,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top