Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignoring return value (C Programming) [duplicate]

Tags:

c

I just started learning C programming a few days back and I'm trying out some problems from the Kattis (open.kattis.com) website. I came up with this problem along the way where I don't really understand what it means.

//two stones

#include <stdio.h>

int main()
{ 
int n,x=2;

printf("The number of stones placed on the ground is ");
scanf("%d",&n);

if (n%x != 0)
{
    printf("The winner of the game is Alice! \n");
}
else
{
    printf("The winner of the game is Bob! \n");
}
return 0;
}

This appeared >>

warning: ignoring return value of scanf, declared with attribute warn_unused_result [-Wunused-result] regarding scanf("%d",&n);

Can anyone explain what's wrong with this and how to rectify this problem? Thanks.

like image 476
jingsen Avatar asked Sep 07 '18 14:09

jingsen


2 Answers

scanf has a return value that indicates success:

C Standard; §7.19.6.4.3:

The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

If you have a format string in your call to scanf that has one format specifier, then you can check that scanf succeeded in receiving an input of that type from the stdin by comparing its return value to 1.

Your compiler is warning you about this not specifically because scanf returns a value, but because it's important to inspect the result of scanf. A standard-compliant implementation of printf, for example, will also return a value (§7.19.6.3.3), but it's not critical to the soundness of your program that you inspect it.

like image 166
Govind Parmar Avatar answered Sep 25 '22 20:09

Govind Parmar


You are ignoring the return value of the scanf call.
That is what the compiler warns about and was told to treat as an error.
Please understand that there are many subtle mistakes possible to be done with scanf() and not caring about the success, which is indicated by the return value.

To hide the problem which the compiler kindly notifies you about, I recommend to first try the "obvious" straight forward approach

int IreallyWantToIgnoreTheImportantInfo;
/* ... */
IreallyWantToIgnoreTheImportantInfo = scanf("%d",&n);

This will however only move the problem somewhere else and the valid reason about ignoring the scanf() return value will then probably (or maybe "hopefully") turn into a "variable set but never used" warning.

The proper way to really solve the problem here is to USE the return value. You could e.g. make a loop, which attempts reading user input (giving an explanation and removing unscanned attempts) until the return value indicates success.

That would probably make much better code, at least much more robust.

If you really really want to ignore, without instead ignoring a variable which contains the info, then try

(void) scanf("%d",&n); /* I really really do not care ... */

But, please take that as completly helpfuly as I mean it, that is not a good idea.

like image 42
Yunnosch Avatar answered Sep 23 '22 20:09

Yunnosch