37

What is the value returned by scanf when:

int g;
int p = scanf("%d", &g);      // Originally: int p = scanf("%d", g);

I know that the signature of the scanf function is:

int scanf(const char *format, ...)

What is the int value returned from this function?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rachit
  • 501
  • 1
  • 4
  • 4

5 Answers5

53

From the man page:

NAME
       scanf,  fscanf, sscanf, vscanf, vsscanf, vfscanf 

       ...

RETURN VALUE
       These functions return the number of input items  successfully  matched
       and assigned, which can be fewer than provided for, or even zero in the
       event of an early matching failure.

       The value EOF is returned if the end of input is reached before  either
       the  first  successful conversion or a matching failure occurs.  EOF is
       also returned if a read error occurs, in which case the error indicator
       for  the  stream  (see ferror(3)) is set, and errno is set indicate the
       error.

In your case, scanf() can return 0, 1 or EOF.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 5
    [`scanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html) returns the number of items successfully scanned and assigned. If the format string is `"%s %d %f %*s%n %d"`, it returns 4 if everything works. The `%*s` suppresses assignment so it isn't counted, and the `%n` returns an offset and isn't counted. If you get 0, 1, 2, or 3, something went wrong. You only get back EOF if there is no data left to read, or there's an input error (not a format error, but a 'hardware' error). With a `"%d"` format, there is only one conversion, so you will get `EOF`, `0` or `1`. – Jonathan Leffler Apr 22 '17 at 00:41
8

From scanf:

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.

gliderkite
  • 8,828
  • 6
  • 44
  • 80
1

it will return 1 as scanf returns the number of items successfully read

Taranjeet
  • 583
  • 2
  • 9
  • 20
0

Technically this is UB (Undefined behaviour).

int g;
int p=scanf("%d",g);
                 ^

You passing an unitialized integer to scanf to use it as an address to write to. From this point on, anything can happen. Most likely your app is going to crash.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 3
    The question (wrongly) was edited, so all the answers now are invalid. – S.S. Anne Sep 05 '19 at 11:19
  • I've reinstated a (corrected) version of the original code — leaving the incorrect code which this answer addresses as a comment. The edit removing the code altogether was bad! – Jonathan Leffler Feb 14 '22 at 05:33
0

Whatever you will give the in the VDU input goes to variable g and if successfully read, p equals 1.

Simon Adcock
  • 3,554
  • 3
  • 25
  • 41