Formatted Input/Output scanf()/printf()

For Complete YouTube Video: Click Here

In this class, we will try to understand Formatted Input/Output scanf()/printf().

Before understanding the word formatted we will understand Input and Output.

Understanding Input/Output scanf()/printf()

Any programming language should provide a facility to the user to input and output the values to the end user.

C Programming language with no exception is providing these functionalities by printf() and scanf() functions.

For instance, the programmer want to take the values from the user with the use scanf() function user can provide the inputs.

Similarly, information can be displayed on the screen by printf() function.

In this class, we will have basic understanding on printf() function.

The image below basic program which we discussed in our first class.

Introduction to C Programming

In the above program the fourth line illustrates the use of printf() function.

printf() Function

We will try to understand the use of printf() from the paragraph given below.

” The printf function is designed to display the contents of a string, known as the format string, with values possibly inserted at specified points in the string. The format string is followed by any values that are to be inserted into the string during printing. The format string may contain both ordinary characters and conversion specifiers which begin with % character. “

For the matter of understanding we will use two printf() functions instead of the one used in the above program.

The image below is the modified version of the above program.

Modified main program
Modified First Program

The above paragraph states that The printf function is designed to display the contents of a string, known as the format string.

Consider the first printf() function from the above program.

printf(“The values of the integer a and b are 10 and 20”);

From the above printf() function the content in the double quotes is called format string.

The printf() function is designed to display that content.

printf() Function with Conversion Specifiers

A part from that the printf() function inserts the values at specified points in the format string.

For instance, if you want to display the content stored in the variable a, b, and c, those values can be displayed by the conversion specifiers.

Consider the second printf() as shown below.

printf(“The sum of the %d and %d is %d”, a, b, c);

The above function is using special characters like %d called conversion specifiers.

A Conversion Specifier is a place holder representing the a value to be filled in during printing.

The place holders of the conversion specifiers is filled with the values followed by the format string.

In the above function the three %d’s are filled with the values of a, b, and c.

The final output of the above printf() function is as shown below.

The sum of the 10 and 20 is 30.

The format string may contain both ordinary characters and conversion specifiers which begin with % character.