The problem is : When i enter any number the result of printf statement is always 1
This is the code : code in picture
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
printf("%d\n", scanf("%d", &i));
return 0;
}
The problem is : When i enter any number the result of printf statement is always 1
This is the code : code in picture
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
printf("%d\n", scanf("%d", &i));
return 0;
}
The answer is 1
because you are not printing the number that you have scanned.
Instead, you print the number returned by scanf
, which is the number of items that it has successfully processed. In your case, that number is 1
, because you read one item from the input.
In order to fix this problem, you need to move scanf
onto a separate line, check its return value to be 1
, and then print the value of i
:
if (scanf("%d",&i) == 1) {
printf("%d\n", i);
} else {
printf("Invalid input\n");
}
You are printing the return value of scanf
not the item. If one number is read, that value will be 1
, from scanf
s man page:
RETURN VALUE
The
fscanf
function returns the value of the macroEOF
if an input failure occurs before the first conversion (if any) has completed. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.
Instead, read the number and then print it out with printf("%d", i);
Always double check what functions return and remember that return values usually try to tell you if the function was successful/failed/etc.
Here's what you want (slightly more elaborate than what you probably need, to illustrate):
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
if( scanf( "%d",&i) != 1) {
fprintf(stderr,"Oh noes! an error-wrong number of things read\n");
exit(1);
} // else: we read something into i
printf("%d\n",i);
return 0;
}
All of the answers here are pointing you in right direction, "check the return value of scanf" .
Generalising out further, you should be aware that most all of the functions in the standard C library would have return values that should be checked at runtime.
So you do this as described in many of the other answers here.
But it's useful to know that the answer to your question was probably at your fingertips on your computer without having to even to go online.
At the command line enter the following to get the documentation on scanf ...
man -k scanf
You should then get a list of man pages that relate to the topic one of which will describe the full operation of the function including how the return value works.
try the same code with two scanf items you will get 2 as your output instead of 1 . Since you are reading only 1 input its returning 1 as your output . Unlike dynamic languages like python your code need solid input beforehand.
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
printf("%d\n", scanf("%d%d", &i,&i));
return 0;
}
OUTPUT : 2 (everytime)
Hope it helps.
scanf
will return the number of read elements. The same will happen with printf
. For example, if you write:
int number = printf("abcd\n");
printf("%d", number);
It will return the number of elements read.