Liste des Groupes | Revenir à cl c |
On 04/12/2024 22:58, Keith Thompson wrote:Bart <bc@freeuk.com> writes:>OK, if it's so simple, explain it to me.I'll pretend that was a sincere question.
Let's put it this way: if somebody asked me what the rule was, I
wouldn't be able to tell them.
You seem to be under the impression that a closing brace should>
either always or never be followed by a semicolon. I don't know
where you got that idea.
Braces ("{", "}") are used in different contexts with different
meanings. They're generally used for some kind of grouping (of
statements, declarations, initializers), but the distinct uses are
distinct, and there's no particular reason for them to follow the
same rules.
Apparently the first line here needs a semicolon after }, the secondYes. The first is a declaration, and a declaration requires a
doesn't:
>
int X[1] = {0};
void Y() {}
semicolon. I suppose the language could have a special-case rule
that if a declaration happens to end with a "}", the semicolon is
not required, but that would be silly.
The second is a function definition (and can only appear at file
scope). Function definitions do not require or allow a semicolon
after the closing "}". Why should they?
Consistency? I posted a bit of Algol68 the other day; each function
definition needed a semicolon, to separate it from the next.
>Similarly here:Because that's what the grammar says.
>
if (x) y;
if (x) {}
>
Why?
>
"Because that's what the grammar says" isn't a valid answer.
Not all statements require a closing semicolon. In particular,
compound statements do not, likely because the closing
"}" unambiguously marks the end of the statement. Sure, the
language could have been specified to require a semicolon, but why?
Consistency again? But then you'd get this: if () {}; else {};. It
would be incompatible with how it works now.
So overall it's just messy. It works for BEGIN/END when semicolon is a
separator, rather than a terminator. So you write END ELSE not END;
ELSE.
>
But because C uses ";" as a terminator, it's made a rod for its own back.
(I'll note that languages that use "begin"/"end" rather than "{"/"}">
often require a semicolon after the "end".)
That's my A68 example.
>And you can add a semicolon after a compound statement if you like>
(it's a null statement),
But not here I guess: if () {}; else ...
Of course you know all this.>
I'm aware of how messy and conistent it is. I've said that languages
that use ";" as a separator probably fair better, but are still not
ideal because the last statement of a block is now an annoying special
case.
Meanwhile, if I compile this:
>
void F(){}; void G(){};
>
My compilers report a syntax error. But gcc passes it by default. So I
take what the grammar says more seriously than gcc!
>
IS ";" between function definitions legal or not? If not, then fail
the program.
>Please don't say the label is only defined to be a prefix to anotherThe label is only defined to be a prefix to another statement.
statement. I asking why it was done like that.
It was simple to define it that way, and not particularly
inconvenient to add a semicolon if you happen to want a label at
the end of a block. I'd be surprised if this rule has ever actually
caused you any inconvenience.
I said that my generated code has to use ":;" after each label; it
looks weird.
But you'll be delighted to know that C23 changed the grammar for a>
compound statement, so a label can appear before any of a statement,
a declaration, or the closing "}". So now you have exactly what you
want. (Just kidding; you'll still find a way to be angry about it.)
No, that's amazing. Presumably, some people must complained about it
(maybe it actually caused some inconvenience!), and it eventually got
fixed.
>
Of course, if it was just me, then it would be pointless ranting. 'How
hard is to add the semicolon?'
>
Well, 'How hard is it to delete the semicolon' from my above example?
Les messages affichés proviennent d'usenet.