What is the output of this code if I input 25 to scanf()? I run this program, the output is 1 but I don't understand why? Can anyone explain?
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
printf("%d\n",scanf("%d",&i));
return 0;
}
scanf() returns the number of arguments successfully assigned. In your case, scanf only has one directive which has a respective argument to be assigned, so it returns 1 if the input conversion was done successfully.
1 is given now as argument to printf, which prints 1 into the output.
With the premise that the scanf() conversion was successfully,
printf("%d\n", scanf("%d",&i));
is basically equivalent to
printf("%d\n", 1);
Going straight forward
int i = 0, j = 0;
printf("%d\n", scanf("%d %d", &i, &j));
with the input of
25(enter)
50(enter)
and both conversions done successfully, would gave you the output 2 and
printf("%d\n", scanf("%d %d", &i, &j));
would basically be equivalent to
printf("%d\n", 2);
Side Note:
The return value of scanf() should always be checked by the algorithm but it is more an objective to the program to see if an input failure occurred than to diagnose it directly to the user.
These functions (scanf family) return the number of input items successfully matched and assigned.
So in this case it can return 1 if the scan was successful or 0 if not. If there is an error in the input stream it can also return EOF
Scanf is a basic function in C language.Since every function return a type of value or null,Scanf function returns numbers of inputs scanned successfully.
For Example: Scanf("%d%d") scans for 2 inputs, so it returns value 2.
Scanf("%f%d%d%d") scans for 4 inputs, so it returns value 4.
&variable_name is used to store the scanned value in the variable by using address-of operator.For scanning string, &-address-of operator can be neglible.
Example scanf("%s",variable)
Printf is a function which returns number of characters printed.It can be used with format specifer like %d,%f etc.
In the above program
printf("%d\n",scanf("%d",&i));
Scanf is first executed by scanning one integer input as %d is specified and stored in the variable of type int. Scanf returns value 1 since it is scanning only one input.
Printf function prints the value 1.Printing value does not depend upon input value.It depends only on number of inputs , scanf is scanned successfully.
you are outputting the scanf function which returns the variable numbers not the value in i.
This question already has answers here:
What does the scanf function return?
(5 answers)
Closed 3 years ago.
why is the output coming as 1 and not the address of j i tried with every alphabets still it's giving 1 as output
how is this program not giving address of j and why is it giving 1 as output i tried with every letters as input still it's giving 1?
#include "stdio.h"
int main()
{
char arr[100];
printf("%d", scanf("%s", arr));
/* Suppose that input value given
for above scanf is "jje" */
return 2;
}
shouldn't it give the address of 1st letter ie j
No. You're printing the return value of scanf. This isn't any address, but (emphasis mine):
On success, the function returns the number of items of the argument
list successfully filled. This count can match the expected number of
items or be less (even zero) due to a matching failure, a reading
error, or the reach of the end-of-file.
Since that scanf result successfully matches the %d, you get 1.
If you wanted to print that address, which is the address of arr, you could try
printf("%p", arr);
This is because you are printing the return value of scanf() which is the number of successful matches scanf makes which in your case is 1 as you're capturing a single string value.
#include<stdio.h>
int main(){
int n;
printf("%d\n",scanf("%d",&n));
return 0;
}
I wonder why the output of this program is always '1' ?!
What's actually happening here ?
I suspect you wanted to use
int n;
scanf("%d", &n);
printf("%d\n", n);
but what you wrote is equivalent to
int n;
int num = scanf("%d", &n); // num is the number of successful reads.
printf("%d\n", num);
The program is just about exactly equivalent to
#include <stdio.h>
int main() {
int n;
int r = scanf("%d", &n);
printf("%d\n", r);
return 0;
}
So if you run this program, and if you type (say) 45, then scanf will read the 45, and assign it to n, and return 1 to tell you it has done so. The program prints the value 1 returned by scanf (which is not the number read by scanf!).
Try running the program and typing "A", instead. In that case scanf should return, and your program should print, 0.
Finally, if you can, try running the program and giving it no input at all. If you're on Unix, Mac, or Linux, try typing control-D immediately after running your program (that is, without typing anything else), or run it with the input redirected:
myprog < /dev/null
On Windows, you might be able to type control-Z (perhaps followed by the Return key) to generate an end-of-file conditions. In any of these cases, scanf should return, and your program should print, -1.
#jishnu, good question and the output which you're getting is also correct as you are only reading one value from standard input (stdin).
scanf() reads the values supplied from standard input and return number of values successfully read from standard input (keyboard).
In C, printf() returns the number of characters successfully written on the output and scanf() returns number of items successfully read. Visit https://www.geeksforgeeks.org/g-fact-10/ and read more about it.
Have a look at the following 2 code samples.
Try the below code online at http://rextester.com/HNJE76121.
//clang 3.8.0
#include<stdio.h>
int main(){
int n, n2;
printf("%d\n",scanf("%d%d",&n, &n2)); //2
return 0;
}
In your code, scanf() is reading only 1 value from the keyboad (stdin), that is why output is 1.
//clang 3.8.0
#include<stdio.h>
int main(){
int n;
printf("%d\n",scanf("%d",&n)); //1
return 0;
}
In your program scanf returns 1 when successfully some value is taken by scanf into n and that is why you are always getting 1 from your printf("%d\n",scanf("%d",&n));.
You should modify your code like following
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}
In man scanf,
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.
In your program, it just match 1 input, so the function scanf will return 1, if you input an legal value that can match a %d, else it will return 0.
In your situation, you might always satisfy this requirements, so it always return 1.
scanf returns the number of successful conversion and assignments. In your case, you're only scanning for one argument, so scanf will either return a 1 on success, a 0 on a matching failure (where the first non-whitespace input character is not a decimal digit), or EOF on end-of-file or error.
When you call printf, each of its arguments is evaluated and the result is passed to the function. In this case, evaluation involves calling the scanf function. The value returned by scanf is then passed to printf.
It's essentially the same as writing
int count = scanf( "%d", &n );
printf( "%d\n", count );
For giggles, see what happens when you enter abc or type CtrlD (or CtrlZ on Windows).
Note that printf also returns a value (the number of bytes written to the stream), so you can write something ridiculous1 like
printf( "num bytes printed = %d\n", printf( "num items read = %d\n", scanf( "%d", &n ) ) );
Joke. Don't do that.
scanf returns number of successful conversions.
Try changing your scanf to as shown below....
you will notice printing 2 after inputting two valid integers.
int n1 =0;
int n2 =0;
int i = scanf("%d %d",&n1,&n2);
In C programming, scanf() takes the inputs from stdin() which is the keyboard and returns the number of successful inputs read
and printf() returns the number of characters written to stdout() which is the display monitor.
In your program scanf() is reading only one input which is getting returned and is printed by printf(). So, whatever may be the input the output is always 1 as it is reading only one input.
Here take a look at this C program:
#include<stdio.h>
int main(){
int n, n2;
printf("%d\n",scanf("%d %d",&n,&n2));
return 0;
}
It takes input or reads two integers from stdin() using scanf(), so the output will always be 2.
**Note: The output is 0 if your input is not integer, as it reads integer only.
The documentation of scanf() says the return value is:
Number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.
The code you would instead want is:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",scanf("%d",&n));
return 0;
}
If you use this code as an example,
It shows : warning: implicit declaration of function 'printf' [-Wimplicit-function declaration]
int main()
{
char b[40];
printf(" %d", scanf("%s", b));
getchar();
}
Return value : -1
Outputs of the functions like printf and scanf can be use as a parameter to another function.
scanf() is a function which is integer return type function. It returns total number of conversion characters in it. For ex if a statement is written as :
int c;
c=scanf("%d %d %d");
printf("%d",c);
Then output of this program will be 3.
This is because scanf() is integer return type function and it returns total no of conversion characters, thus being 3 conversion characters it will return 3 to c.
So in your code scanf() returns 1 to printf() and thus output of program is 1.
This is because if more than one statement are used in printf() execution orders starts from right to left.
Thus scanf() is executed first followed by printf().
#include <stdio.h>
int main()
{
int i=43;
printf("%d",printf("%d",printf("%d",printf("%d",i))));
return 0;
}
what the use of nested printf()? I used value 43 but how other values
showing in output?
Output:
43211
Consider this as a simpler version of your code:
int main(){
int i = 43;
int a = printf("%d", i);
int b = printf("%d", a);
int c = printf("%d", b);
printf("%d", c);
return 0;
}
It's clear why 43 is printed. As for the other digits, they are the return value of printf:
On success, the total number of characters written is returned.
So, what you should have been getting (considering this was your exact code) is: 43211
43 is the first print
2 is the number of digits printed before ('4' + '3')
1 is the number of digits printed before ('2')
1 is the number of digits printed before ('1')
The return value of printf is the number of printed characters.
Let's analyze what's going on.
printf("%d",printf("%d",printf("%d",printf("%d",i))));
The innermost printf("%d",i) prints obviously43 as i is 43.
The next outer printf("%d",printf("%d",i)) prints 2 because the length of the output of the previous printf is 2 (length of "43").
So up to now the output is:
432
The next outer printf will print 1 because the length of the output of the previous printf is the length of "1" which is 1.
So up to now the output is:
4321
The outer printf will print 1 again because the length of the output of the previous printf is the length of "1" which is 1.
So the final output is:
43211
You are printing return values of printf() since the first prints correctly 43.
printf() doesn't return the number of "items" output like the scanf() family of functions do for input. It returns the actual character count.
printf return an int value, so if you concatenate printf commands, the first value is the number you want to print the others are the result of printf instruction that returns in case of success the number of printed chars (even if they are carriage return or new line so not visible).
In your case:
43 the first number
3 the printf int result
and so on ...
Okay so I'm trying to do a basic program in VS. Enter a number then it gets printed out. 1 is always printed.
int main(){
printf("Enter an integer: ");
int n = scanf_s("%d", &n);
printf("%d", n);
}
You are assigning the returned value from scanf_s() to the variable n, that means that the program will print 1 in case a successful read happened.
What you should do is
int numberOfItemsMatched;
int readValue;
numberOfItemsMatched = scanf_s("%d", &readValue);
if (numberOfItemsMatched == 1)
printf("%d\n", readValue);
I hope the variable names are self explanatory, and it's always a good idea to use this kind of names.
return type of scanf is number of items read. so if scanf is succesful in reading an item, it returns one which is assigned to n here. hence the output is 1. So separate declaration of n and scanf.