Liste des Groupes | Revenir à cl c |
On 27/02/2025 14:05, Ar Rakin wrote:Ah, I see. Thanks for explaining.Lawrence D'Oliveiro <ldo@nz.invalid> writes:He is thinking of situations like this (ignore the stupidity of the example) :
>On Wed, 26 Feb 2025 13:31:51 -0800, Keith Thompson wrote:>
>One interesting feature of Perl is that the braces are required.>
And yet it doesn’t have C-style macros, which to me are the most obvious
way one can get into trouble by omitting needed braces.
What do you mean? Sorry I'm stupid sometimes.
#define message(x) printf("Message: "); printf(x); printf("\n")
void foo(int y) {
message("Starting foo... ");
if (y < 0)
message("Y is negative");
message("Done foo.")
}
Reading "foo", you'd think that it would print "Message: Y is negative\n" if y is negative. But in fact only the first statement in the macro is covered by the conditional, and "Y is negative\n" will always be printed. You can easily imagine how things could get a whole lot worse from such mixups.
There are many ways to avoid this, including putting a "do { ... } while (0)" wrapper around the statements in the macro, using an inline function instead of a macro, writing macro names in all-caps as a warning (I strongly dislike that convention), and always using braces with conditionals even if they appear to cover just one statement.
I am not sure if there is a basis for judging the "most obvious" problem that can occur from omitting braces in conditionals, but this is certainly a strong contender for the title.
Another case that occurs is confusion about matching up else's with if's in nested conditionals without braces.
Les messages affichés proviennent d'usenet.