0

I have this issue where gcc using the Werror argument, comes up with the error 'ignoring return value of scanf'. Now, I checked the forums and I did do stuff such as putting it in an if statement which seemed to work fine, but the thing is, I do not get the error if I were to compile it at my University.

#include <stdio.h>
#include <stdlib.h>

#define EXAMPLE1 5

int main (int argc, char* argv[]) {
int example;


   scanf ("%d", &example);
   if (example <= EXAMPLE1) {
       printf ("Woohoo\n");
   }   else {
       printf ("Oohoow\n");

   }

   return EXIT_SUCCESS;

}

So this code for example would compile fine using gcc -Wall -Werror -O -o namehere namehere.c at my uni, but if I were to use it at home, the aforementioned error comes up. My uni is using gcc 4.9.2. I tried it at home on gcc 4.8.4, 4.8.5, 4.9.3.

Brandon Duong
  • 11
  • 1
  • 1
  • 1
    What is your question? – autistic Mar 17 '16 at 11:24
  • Apparently `-Wunused-result` is glitchy and doesn't always work. [Might have something to do with optimization](http://stackoverflow.com/questions/17275116/ignoring-return-value-of-int-scanfconst-char-declared-with-attribute). I can't reproduce it though, so I'd dismiss this as a GCC bug. – Lundin Mar 17 '16 at 12:15

3 Answers3

1

Ignoring the return value of scanf() is not good because it means you are not checking for possible errors in input. Though some compilers or settings don't give warnings for that, you shouldn't ignore return values of scanf().

Try this:

#include <stdio.h>
#include <stdlib.h>

#define EXAMPLE1 5

int main (int argc, char* argv[]) {

   int example;

   if (scanf ("%d", &example) != 1) {
       printf ("Read error\n");
   }
   else if (example <= EXAMPLE1) {
       printf ("Woohoo\n");
   }   else {
       printf ("Oohoow\n");

   }

   return 0;

}
Marievi
  • 4,951
  • 1
  • 16
  • 33
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

You are not getting an error, the compiler is so friendly to give you a warning. And correctly! Upon erroneous input example will have a random value.

Always check that the return value of scanf matches the number of arguments you want to read.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
  • Ah I see. So if I have ("%d %d", &one, &two) for example, I should make sure that there are two arguments? – Brandon Duong Mar 17 '16 at 11:07
  • @BrandonDuong: Not passing the arguments (in number **and type** expected by the format string is undefined bhaviour already. You should also follow the warning of your compiler and check that all arguments have been set. Please read a C book and the man-page of `scanf` if still unsure! – too honest for this site Mar 17 '16 at 11:36
0
scanf ("%d", &example);

This tells scanf to read a sequence of decimal digit characters (possibly with a prefixed negative sign) up to the first non-decimal character, and convert that sequence into an int value that gets stored into example.

What if the user enters something that is entirely non-decimal, or if stdin is closed? The scanf call would fail, indicating such a failure using a corresponding return value... but what would the value of example be in such a situation?

You need to check the return value to avoid using example when it contains garbage. If you try to use that variable, the behaviour of your program may be erratic. This might include segfaults.

Your compiler is within its rights to refuse to compile this code, because using an uninitialised (or otherwise indeterminate) variable is undefined behaviour.

In summary, it's well within your best interests to always check the return value because:

  • Some compilers might refuse to compile code that doesn't have such checks, as you've noticed.
  • Those that do compile your code will allow your program to behave erratically, using garbage when the user enters garbage or in some cases, possibly segfaulting due to use of a trap representation.
autistic
  • 1
  • 3
  • 35
  • 80