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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Generating random number in a range in C
- Why strict aliasing is required in C ?
- Modulus of two float or double numbers
- Interesting Facts about Macros and Preprocessors in C
- Variables and Keywords in C
- Measure execution time with high precision in C/C++
- Why strcpy and strncpy are not safe to use?
- Applications of Pointers in C/C++
- Communication between two process using signals in C
- Pre-increment and Post-increment in C/C++