Lew Pitcher said:
On November 26, 2011 06:11, in comp.lang.c, (e-mail address removed) wrote:
Lew Pitcher ha scritto:
[snip]
for (p = 0; p < P; ++p)
{
e[0] = 0;
multiply(e,10,I+X);
fputc(e[0]+'0',stdout);
if (p%5 == 4)
if (p%50 == 49)
here my compiler says:
temp.c: In function ‘main’:
temp.c:50: warning: suggest explicit braces to avoid ambiguous ‘else’
The code in question is:
if (p%5 == 4)
if (p%50 == 49)
fputs("\n\t",stdout);
else
fputc(' ',stdout);
The C standard is pretty clear about the association of <<else>> to the
appropriate <<if>>For instance, in IEC 9899-1999, section 6.8.4.1 ("The
if statement"), point 3, it says:
"An else is associated with the lexically nearest preceding if that is
allowed by the syntax."
That doesn't seem very ambiguous to me.
It's not ambiguous at all as far as the language is concerned, but it
could be ambiguous to a human reader.
I suggest that your compiler's warning level is wound up too tight. I
know that compilers are permitted to emit warnings for anything they
want, from noting potential syntax errors to baying at a full moon. That
doesn't mean that the compiler doesn't know what to do with the code, or
that the code violates the C standard in any way. It just means that the
compiler writer thought that the programmer would like to know something.
I disagree. The warning is an important one. The solution (though it's
not required by the language) is to add braces:
if (p%5 == 4) {
if (p%50 == 49) {
fputs("\n\t",stdout);
}
else {
fputc(' ',stdout);
}
}
I've worked in shops that required "clean" (no errors, no warnings)
compiles. Often the programmers took very convoluted steps to ensure
compliance (granted, this was in COBOL, so compliance became very wordy).
Certainly you can turn off the warning if you like, but I wouldn't (and
frankly I probably wouldn't see the warning in the first place when
compiling my own code).
I agree that warnings are good to see. But, taking concern when the warning
warns you that the code will do /what you intended it to do/ seems just
silly to me. And, that's the point I was trying to get across (obviously,
poorly).
Of course, the programmer has (at least) three options:
1) to confirm that the warning can be ignored (because it warns about
a /wanted/ condition).
2) to ignore or disable warnings, or
3) to change the code so that the warning goes away.
In the above example code, you seem to advocate option #3.
However, I /know/ (from your past postings) that you don't believe this in
all cases. How do I know? Well, because you regularly advocate for option
#1 or option #2 when people complain that their call to malloc() results in
a warning. You /definitely/ don't like option #3 in those cases, because in
practice it results in code that uses a cast of malloc()s return value to
eliminate the warning. You know the dangers of that sort of code fix as
much as I do.