Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why missing terminating " character is a warning? [closed]

When, for example, we compile the following code:

printf("hello);

we get a warning then an error about the missing " character. In my opinion, warnings inform us about a code that can be compiled but whose behaviour may be probably different from what the developper expects. Therefore my comprehension missed up two things:

  1. Is there a complete code that can be compiled without errors while containing such a portion of code.
  2. If such a code does not exist, why this missing character situation does not give us only an error (not a warning+error).

EDIT (I am doing my best to cope with off-topic votes recommendations):
1. Desired behavior : only one error diagnostic message, there is no need for a warning for the same thing.

  1. Other related issues that do not let me accept the first answer:

2.1 Does printf_s() have the same issue? I tried to enable -c11 option with no success.

2.2 The historical reason to emit the warning does not seem to me to be plausible since why this double message was not used too in similar cases (old accepted constructions being forbidden in new c versions).

like image 660
Mahdaoui7 Avatar asked Dec 08 '25 08:12

Mahdaoui7


1 Answers

In my opinion, warnings inform us about a code that can be compiled but whose behaviour may be probably different from what the developper expects. Therefore my comprehension missed up two things:

Your opinion is irrelevant here, and neither the C standard nor the C++ standard distinguish different categories of diagnostic messages. That many compilers in fact do distinguish is an historically-based convention, albeit a widely observed one. What ultimately matters is what your compiler means by such a distinction (if indeed it makes one). On the other hand, and fortunately for you, GCC does adopt a convention similar to what you describe, as documented in its manual:

  • Errors report problems that make it impossible to compile your program. [...].

  • Warnings report other unusual conditions in your code that may indicate a problem, although compilation can (and does) proceed. [...]

(GCC 7.2 manual, section 13.9; the same or similar text appears also in earlier versions of the manual, back to at least v.4.)

Note well that the documentation frames the meaning of a warning slightly differently than you do: a GCC warning signals that compilation can proceed, but there is no assurance that it can complete successfully. If indeed it ultimately cannot then I would expect GCC, pursuant to its documentation, to also issue an error diagnostic. That is exactly what I observe with this test program, whether compiling as C or as C++:

#include <stdio.h>

int main(void) {
    printf("hello);
}

I really think you are making far too much of the fact that GCC emits a warning in addition to an error in this case. That's an implementation quirk of no particular significance.

  1. Is there a complete code that can be compiled without errors while containing such a portion of code.

It depends on exactly what you mean by that. Trivially, I could prefix the erroneous line in the above program with // to turn it into a comment, and that would make it perfectly valid C and C++ source. There are manifold other ways I could add to the given source without removing anything to make it valid -- some of them would even produce a program in which a printf() call is in fact performed.

I suppose that what you really want to know is whether there is code that would elicit the warning from GCC but not the corresponding error. To the best of my knowledge, modern GCC does not afford such code, but historically, GCC did allow it as an extension, in the form of embedded, unescaped newlines in string literals:

    printf("hello);
    Goodbye");

That behavior was already deprecated in GCC 3.2, and it was removed as early as GCC 4 (current is 7.2).

  1. If such a code does not exist, why this missing character situation does not give us only an error (not a warning+error).

We can only guess, but it seems plausible that it derives from the historical existence of the language extension described above. And again, you are making far too much of this. GCC emits two diagnostics about the same problem -- so what? The ultimate purpose of the diagnostics is to help you figure out what is or may be wrong with your code, and the diagnostics GCC emits in this case do that job just fine.

like image 172
John Bollinger Avatar answered Dec 09 '25 22:12

John Bollinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!