C printf, scanf

C printf scanf sprintf sscanf:
These C functions are used in for taking input from user and display output to the user. Lets see how to use these keywords.

  • There is no keyword available in C for doing input/output.
  • All I/O in C is done using standard library functions.
  • C provide us some in build function like printf (), scanf () etc.
  • These functions are predefined in library “stdio.h”.
  • To include functions printf and scanf in program we have to include “stdio.h” file.
  • printf() and scanf() are different from Printf() and Scanf() as C is case sensitive language.
  • printf( ) outputs the values to the screen.
  • Whereas, scanf( ) receives them from the keyboard.
  • scanf() is used to read data from the keyboard.
  • All characters in printf() and scanf() functions must be in lower case.

The general form of printf( )

<format string> can contain, %f for printing real values, %d for printing integer values, %c for printing character values. This format string may also contain any other characters. When the printf ( ) is executed these characters are printed as they are.

printf() examines the format string from left to right. So it continuously prints characters that it encounters.  When any format string encounters it picks up the first variable in the list of variables and prints its value in the specified format.

The general form of scanf( )

In the scanf, the ampersand (&) before the variables is a must. & is an ‘Address of’ operator. It gives the location number used by the variable in memory. A blank, a tab or a new line must separate the values supplied to scanf( ). Do not include these escape sequences in the format string. All the format specifications that we have learned in printf( ) function are applicable to scanf( ) function as well.

Let’s see the example –


In above program, during compilation, when %d is met compiler picked up variable ‘c’ and print its value.  Similarly, when an escape sequence (newline, tab characters) is met it takes the appropriate action.  This process continues until the end of the format string is not reached.

sprintf and sprintf functions

sprintf( ) and printf() function works similar except one small difference.

printf() sends the output to the screen whereas sprintf() function writes the output to an array of characters.
Let’s see an example to understand difference-

In above program The printf() prints values of a, ch, and b on screen. Whereas sprintf() stores these values in the character array str.
Using sprintf( ) doesn’t get displayed on the screen. So by using printf() you can print string str on screen.

sscanf() allows us to read characters from a string and to convert and store them in C variables according to specified formats. The usage of sscanf( ) is same as scanf( ), except that the first argument is the string from which reading is to take place.

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.