Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - error: ignoring return value of scanf?

Tags:

c

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?

like image 670
Cnerb Avatar asked Apr 06 '12 13:04

Cnerb


1 Answers

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
like image 52
Basile Starynkevitch Avatar answered Oct 02 '22 20:10

Basile Starynkevitch