Why does optimizing with the -O(1/2/3) option interfere with scanf()?

LinieKiste

I am trying to compile a C program using gcc with optimization enabled.

gcc [name] -lm

works fine, but as soon as I add -O3 it gives me this warning:

warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]

What am I missing here?

klutt

Why does this warning depend on the optimization level? [from comment section]

The main reason is that there are certain analysis enabled by different optimization levels.

The absolute best way is to do as the compiler tells you. Not checking the return value of various functions is something that is the source of many bugs. Just do something like this:

if(scanf("%d%d", &x, &y) != 2) { /* Handle error * }

and the warning will disappear.

If you instead decide that you know better than the compiler and simply want it to shut up, then you can also do as the warning tells you, even if it's not so obvious how it is done. Use this code:

#pragma GCC diagnostic warning "-Wunused-result"

That method can be used for a lot of warnings. In this particular case, it's however more conventional to cast the result:

(void)scanf( ...

but that does not always work.

Do note that all of these have their pros and cons. If you for instance for some reason are able to prove that all input from stdin will have the right form and that you never fail to read from it, then by all means, deactivate the warning. Another thing could be that you have special needs, like extreme performance and/or very limited resources, or maybe some other reasons, it could be a wise choice to do a risk analysis for it and see if the risk is acceptable.

But as a general rule, do not inactivate compiler warnings unless you are very, very certain that the thing the compiler is warning about will not cause trouble. "I don't want to see a a lot of warnings" is not a good reason for disabling warnings.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why does "#define A" interfere with "namespace A{}"?

Why does socket interfere with selenium?

Why does the NEG instruction interfere with the Carry Flag?

Why does "position: relative" interfere with "transform: scale"?

Why does remapping key interfere with FormatTime?

Why does DispatchGroup interfere with main queue?

Why does enabling undefined behaviour sanitization interfere with optimizations?

Why does a nested self-capturing function interfere with isKnownUniquelyReferenced(_:)?

why does a local PrintWriter interfere with another local PrintWriter?

Why does my Navbar interfere with my Stripe token creation?

Implementing a Web Api Controller - why does use of IDisposable interfere with routing?

Why does my firewall (iptables) interfere in my bridge (brctl)?

Does Git interfere with SVN?

Why does this code behave differently if optimizing (-O2, -O3) is used?

Why does scanf() not change line after input?

Why does scanf matches different string ends

Why does scanf function asks for an extra input?

Why does scanf() in this program fail to get input?

Why does scanf skip getting string input?

Why does scanf ask twice for input

Why does scanf expect two numbers?

Why does scanf set the first value to 0?

Why does adress-of operator work in scanf?

Why does scanf("%s", val); return null?

Why -c option does not work? (ping option)

Why does Option<&String> not coerce to Option<&str>?

Maven deploy-file goal: Why does the first execution interfere with the second one?

Does requestAnimationFrame interfere CSS transitions?

Why does use of := with fmt.Scanf in Go always return 1?