I'm confuse with the 1 in thing line. Please help.

0 john johnny · August 25, 2015
Hi. Can i get explanation in C programming what does "1" mean in this line "if (scanf ("%d", &number) == 1)" or in this line "if (scanf("%19s", word) == 1)"

I want to understand the connection of 1 on those line. I tried to change them to 0 or 2 but program ended. So what's with 1?

Post a Reply


Oldest  Newest  Rating
+1 c student · August 25, 2015
scanf returns the number of successfully matched inputs. for example, consider the following:
int ret = scanf ("%d", &num);

if the user inputs a number, ret will 1.  if the user inputs anything that isn't a number, ret will be 0.
now consider this:
int ret = scanf ("%d%c", &num, &c);

if the user enters in a number and then a character in that order, ret will be 2.  if the user enters something that's not a number and then a character, ret will be 1.
int ret = scanf ("%s", str);

if the user enters a string, ret will be 1, otherwise it will be 0.
int ret = scanf ("%s%d%c%f", str, &num, &c, %fnum);

if the user enters in the corresponding data correctly in order, then ret will be 4.  otherwise it will be less than 4.
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator