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?
printf() - printf() returns the number of characters successfully written on the output. It is used to simply print data in the output. scanf() - It returns the number of data items that have been entered successfully.
scanf() 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.
This is correct, because, scanf() returns number of successfully matched and converted elements. Considering proper input in your case, every time your input passes the conversion, so you get to see the value 1.
The printf() function will return the number of characters printed.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With