How does a scanf() inside printf() work?

printf and scanf in c
how printf and scanf works
printf function in c source code
scanf(%d)
how to use scanf and printf in c
does scanf work in c++
scanf return value
return value ignored scanf
#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 ?

@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;
}

Implementing printf and scanf in C, What is the return type of printf () and scanf () function? KEY POINTS TO REMEMBER IN C PRINTF() AND SCANF(): printf() is used to display the output and scanf() is used to read the inputs. printf() and scanf() functions are declared in “stdio.h” header file in C library. All syntax in C language including printf() and scanf() functions are case sensitive.

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);

What is the return type of printf () and scanf ()?, is the number of characters that were successfully output (to the display, or to a file or other device if stdout has been redirected). 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.

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.

C printf and scanf functions with example, if the input is reached the end and no data is read successfully. The scanf function allows you to accept input from standard in, which for us is generally the keyboard. The scanf function can do a lot of different things, but can be unreliable because it doesn’t handle human errors very well. But for simple programs it’s good enough and easy to use. The simplest application of scanf looks like this:

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;
}

What does a scanf function return?, What values do the printf() and scanf() functions return ? printf() : It returns total number of Characters Printed, Or negative value if an output error or an encoding​  Printf and scanf are the functions used most frequently in C language for input and output. These work on console stdin and stdout files respectively hence, these two works works on files and are file operators.

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.

Return values of printf() and scanf() in C/C++, Print function in C abbreviated to printf() and scan function abbreviated to scanf. Printf and scanf are most frequently used in any programs written with C  scanf and printf inside for loop IS IT GOING TO WORK | C Programming | C MCQ #13 CppNuts. Part 1: Nested printf in c || Work process of printf in c || Rahul Sir - Duration: 9:21.

How printf and scanf function works in c internally? (implementation , printf() and scanf() functions are inbuilt library functions in C programming language In C programming language, printf() function is used to print the “​character, string, float, For more information about how pointer works, please click here. Scanf for char doesn't work in loop What am I doing wrong here? I've tried this in the main, I've tried it in a function using a return statement, and here is my attempt to pass a pointer to a character (a math operator--this is supposed to be a calculator program) into a loop.

Scanf, The scanf function allows you to accept input from standard in, which for us is main() { int a, b, c; printf("Enter the first value:"); scanf("%d", &a); printf("Enter the 

Return values of printf() and scanf() in C, Understanding how printf and scanf works internally is the key to writing In the above code, the function average() can take in any number of input parameters.

Comments
  • scanf returns a value from the function: the number of items converted, as the man page will tell you. The man page should be your first stop with any function you use.
  • You did not read anything on scanf(), did you? en.cppreference.com/w/c/io/fscanf
  • What answer do you get when you type in a non-digit character?
  • The intention of my question was just to know the reason behind the result as I encountered such an objective question in a book...Thank you
  • @jishnupramod, If I were you, I would drop that book. A book should teach principles and fundamental aspects of the language. Using questions like that is what I call "smart-assery". It's not teaching.
  • The intention of my question was just to know the reason behind the result...Thank you
  • The input format spec is %d. If an integer cannot be extracted from the input, scanf returns 0 - that is what the return value is for. That is not "always 1".
  • @WeatherVane I assumed that the author of question will always input a legal %d... It's my mistake, thank you.
  • Yes, the purpose of the return value is to detect if user did not enter a value that can be converted to the type.
  • A more accurate term is "successful conversions".
  • True. I will change it.
  • The intention of my question was just to know the reason behind the result...Thank you
  • You mean "Return values", not "Outputs". What you are trying to say with the implicit-declaration-warning is unclear to me.
  • Yeah, It’s the return value !