I use scanf in a c program to read an int from STDIN:
scanf("%d", &n);
when I compile the c program with optimization enabled I get some warnings:
gcc main.c -lm -lpthread -O2 -o main
main.c: In function ‘main’:
main.c:45: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
main.c:50: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
but when I remove the optimization options, why don't I get those warnings?
gcc main.c -lm -lpthread -o main
P.S: I'm not using -Wall or something similar.
The warning message for each controllable warning includes the option that controls the warning. That option can then be used with -Werror= and -Wno-error= as described above. (Printing of the option in the warning message can be disabled using the -fno-diagnostics-show-option flag.)
Turning on optimization flags makes the compiler attempt to improve the performance and/or code size at the expense of compilation time and possibly the ability to debug the program. The compiler performs optimization based on the knowledge it has of the program.
You can make all warnings being treated as such using -Wno-error. You can make specific warnings being treated as such by using -Wno-error=<warning name> where <warning name> is the name of the warning you don't want treated as an error. If you want to entirely disable all warnings, use -w (not recommended).
GCC 4.3+ now has -Q --help=warnings , and you can even specify --help=warnings,C to just print out the C related warnings.
Changing optimizer settings changes how much (and how) the compiler analyzes your code.
Some program flow analysis is not done when optimization is not enabled (or not set high enough), so the related warnings are not issued.
You'll see that frequently for "unused variable" warnings - these require analysis of the code beyond what is necessary to simply compile it, so you'll ususally only get them with optimization enabled.
(And you really should be compiling with -Wall
.)
-Wunused-result
is enabled by default: Because you'll actively need to decorate a function with __attribute__ ((warn_unused_result))
to trigger the warning, false positives only occur when it's used too liberally.
Even without passing additional flags, gcc should produce a warning. However, as Mat explained, the compiler doesn't do the necessary control flow analysis without increasing optimization levels.
Fix your code or silence the warning by adding -Wno-unused-result
. Casting the return value to void
will probably do as well.
To silence the warning in code, you'll need to assign the return value to a dummy variable, which you then can cast to void
to avoid a new warning about the unused variable. It's also possible to substitute a C99 compound literal for the explicitly declared variable (tested with gcc 4.5.3).
This is indeed not optimal - I really expected the void
-cast I originally porposed to work...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With