Javatpoint Logo
Javatpoint Logo

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.

Data Type Conversion format string
Integer short integer %d or % i
short unsigned % u
long signed % ld
long unsigned % lu
unsigned hexadecimal % x
unsigned octal %O
Real float % f or % g
double %lf
signed character %c
unsigned char %c
string %s

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():

Example 1:


Please enter the two values:8
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.

Example 2:-

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.

Example 3:


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.

Example 4:


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.

Example 5:


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:-

  • printf() function outputs data to the standard output, i.e., to the console. In contrast, the scanf() function reads data from the standard input, i.e., input devices
  • printf() rarely uses pointer in a few cases but scanf() always uses a pointer to assign value to the given variable.
  • printf() and scanf() both have the same return type of integer. The printf() returns the number of characters it has successfully printed on the console, whereas scanf() returns 0,1 or EOF based on the format specifier provided.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA