Difference between printf() and scanf() in C
Printf() and Scanf() are inbuilt library functions in C language that perform formatted input and formatted output functions. These functions are defined and declared in stdio.h header file. The 'f' in printf and scanf stands for 'formatted'. So, both the functions printf() and scanf() use codes within a format string to specify how output or input values should be formatted. We need to import the header file to use printf() and scanf():
In this article, we will discuss about the difference between printf() and scanf(), but before discussing the differences, we must know about the printf() and scanf() with its syntax and examples.
It prints anything between double quotes with format specifiers %c, %d,%f, etc., to show a value of a variable or a constant and can also have escape sequence characters like \n, \t for new-line, and horizontal tab, respectively.
It allows us to read one or multiple values entered by the user through the keyboard at the console. We can place as many format specifiers as many inputs we want with or without a format.
Format Specifier string:
In the first parameter of printf and scanf, we pass format string or format specifier string, and we specify what type of value the user is going to enter.
For example, if a user enters 20 as input, as 20 is a decimal integer value, we specify all the decimal integer values in C by using the %d format specifier. Similarly, float values are specified using %f.
Basic Code for Printf() and Scanf():
Please enter the two values:8 6 The sum = 14 sub = 2 mul = 48
In the above program, we are using the scanf() to read the input from the console and store them in variables a & b. After that, we perform arithmetic operations like addition, subtraction, and multiplication and later store the results in the three new variables (Sum, Sub, Mul). Finally, by using the printf() function, we are printing the results.
In the above program, scanf() reads two inputs from the program even though we are passing one variable because we specified two format specifiers. Always remember, In scanf()/printf() all the format specifiers that we pass should be the same, else we may get some unexpected runtime errors.
5 Execution finished...
In the above program, scanf() reads only one input from the user because we specified only one format specifier. So from this example, we conclude that none of the inputs will be taken depending on the number of format specifiers.
6 Segmentation fault
In the above program, we are passing the value of a variable to scanf() instead of the variable address. It will lead to a run-time error. In Linux, we will get a segmentation fault error.
the value of a = 10, the value of b = 20
In the above program, when printf scans the format string, it will replace the format specifiers (%d) with the actual values that we pass to printf(). i.e., the first format specifier will get replaced by the first value that we pass in printf(), and so on.
Basic Differences between Printf() and Scanf()
Few Differences between printf and scanf are:-