13

I'm only a few days into C programming, so I am not quite sure what's wrong with this code:

#include <stdio.h>

int main(int argc, char * argv[]) {
    int sides;
    printf("Please enter length of three sides :\n");
    scanf("%d", &sides);
    return 0;
}

The error message I receive is as follows:

ignoring return value of scanf

What am I doing wrong here, and what can I do to fix it?

Lavaman65
  • 863
  • 1
  • 12
  • 22
Cnerb
  • 171
  • 1
  • 2
  • 4
  • What do you intend to do? You haven't used the return value of `scanf` at all in this code! What do you intend to do with it? – vsz Apr 06 '12 at 13:04
  • While it is not strictly an error to ignore the return value of `scanf` and your compiler is just doing more hand-holding that you might expect, you should really *consider* it a programming (or at least a thinking) error to ignore the return value of any I/O operation. All I/O can fail, and a program that does not deal with that is essentially incorrect. – Kerrek SB Apr 06 '12 at 13:27
  • Possible duplicate of [Ignoring return values in C](http://stackoverflow.com/questions/11888594/ignoring-return-values-in-c) – Ciro Santilli OurBigBook.com Jul 19 '16 at 11:48

4 Answers4

10

You might code

if (scanf("%d", &sides) >0) {
    printf("you want %d sides.\n", sides);
} 
else printf("You did not enter any number.\n");

The scanf function (please follow the link) has several roles

  1. it is expecting some input and could modify the variables you passed by address to it
  2. it is returning the number of successfully input items
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • using an if statement works, I've done it before, but I don't encounter the need for an if statement on the uni computers... So perhaps its some setting on my home computer? ALSO thank you so for the link - it should prove handy – Cnerb Apr 06 '12 at 13:10
  • okay i got it working by calling it int sides = scanf("%d", &sides). thanks – Cnerb Apr 06 '12 at 13:44
  • 1
    No, coding `sides = scanf("%d", &sides);` is wrong. You should not use twice the same `sides` variable like this. – Basile Starynkevitch Nov 11 '13 at 18:27
7

It's a warning that stops your compiler from performing it's task (too strict settings). Check the return value of the scanf() function for errors and the warning should disappear.

Return Value

On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned.

Alexander
  • 8,117
  • 1
  • 35
  • 46
  • This is a warning btw and I don't have this problem using the computers at uni – Cnerb Apr 06 '12 at 13:07
  • There are several levels of strictness, one of them treats warnings as errors. I figured you meant that by `the error`. – Alexander Apr 06 '12 at 13:08
  • sorry for not being specific, it is a warning that's being treated as an error – Cnerb Apr 06 '12 at 13:13
  • If you tell us your ide/compiler, we can provide you with information how to turn it off. – Alexander Apr 06 '12 at 13:20
  • well i'm using gedit to write the programs and a standard xterm on ubuntu to compile/run – Cnerb Apr 06 '12 at 13:23
  • It's the `-Werror` flag, as @Thiruvalluvar pointed out. If you want to continue using it, you have to check the return values of functions. – Alexander Apr 06 '12 at 13:46
1

scanf returns the number of "items", i.e. values passed both in the format string (a single item is e.g. %d, %c and so on), and in the subsequent arguments to scanf, for example, to read two integers separated by comma and space, you would use:

int x, y;
int items = scanf("%d, %d", &x, &y);
assert(items == 2);

I've already spoiled what my suggestion will be above - instead of adding unused variables, if you just want to read it, add an assertion:

#include <assert.h>
/* ... */
assert(scanf("%d", &sides) > 0);
/* ... */

Unfortunately, assert(scanf("%d", &sides)); is not enough, because of EOF (this will return -1). It would be really elegant.

I think this is the way to go, if you don't want to continue your program with an uninitialized variable (sides) in this case.

Alternatively, you can capture scanf's result to a variable, and handle it gracefully like in the other answers.

Tomasz Gandor
  • 8,235
  • 2
  • 60
  • 55
0

You don't capture the return value of scanf in a variable. It's a count (as an integer) of how many characters were read, so if that's important to you, then it may be good to capture it.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • 4
    And it _is_ important to you, believe me. But it's a count of how many items were successfully scanned, _not_ characters. – paxdiablo Apr 06 '12 at 13:06
  • Something as innocuous as `int count = scanf("%d", &sides);` is enough to stop the warning. Although I will admit that on gcc 4.6, I'm not getting the same warning. – Makoto Apr 06 '12 at 13:08