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.
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.)