Why there is need of using ‘&’ in case if scanf function while not in case of printf function.
scanf("%d %d", &a, &b); printf("%d %d", a, b);
As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf(). So in order to reflect changes in the variable a and b of the main function, we need to pass addresses of them. We cannot simply pass them by value.
But in case of printf function as we are only going to print the values of the variables in output console, there are no changes going to be made in variable a and b’s values. So it is not required to send their addresses.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Nested printf (printf inside printf) in C
- Problem with scanf() when there is fgets()/gets()/scanf() after it
- Return values of printf() and scanf() in C/C++
- Cin-Cout vs Scanf-Printf
- Why to use fgets() over scanf() in C?
- What is use of %n in printf() ?
- Why "&" is not used for strings in scanf() function?
- scanf() and fscanf() in C - Simple Yet Poweful
- Inbuilt library functions for user Input | scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
- Difference between scanf() and gets() in C
- scanf("%[^\n]s", str) Vs gets(str) in C with Examples
- How to print % using printf()?
- How to change the output of printf() in main() ?
- Passing NULL to printf in C
- Execution of printf with ++ operators
- What is the difference between printf, sprintf and fprintf?
- puts() vs printf() for printing a string
- Code valid in both C and C++ but produce different output
- Do not use sizeof for array parameters
- Why strcpy and strncpy are not safe to use?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.