Sample C Program Part 4

Chap. 1 : Unit 6 : Sample C Program : Part 4

In this video we will try to explore escape sequence, the facility provided by Dennis Ritchie to get the well formatted output on the screen and also how format specifiers can be used in printf and scanf functions.

Writing slash n escape sequence at the end of printf statement string is good programming practice. 

Now we will have a closer look at a very close friend of printf function….i.e. scanf function…. Scanning basically involves retrieving something who wishes to give…. Here we want program to scan data from the keyboard which is entered by the user…. So is the scan function….i.e. scanf…. It is part of stdio.h header file like that of printf. 

The second guest which appears both in printf and scanf function is %d. it is called as format specifier for integer constant. When encountered in printf function, value in the variable is printed in the integer format where percent d appears.

On the contrary, when %d is encountered in scanf function, program waits for user input and expects integer input from the keyboard. When integer constant is entered then that number scanned is stored at the address contained in corresponding variable in the variable list written after first comma. That is why & is attached as unary operator ahead of variable name and is called address operator. 

In the above example when first scanf function is executed, input from the keyboard say 10 will be stored at the address of x where as when second scanf function is executed, input from the keyboard say 20 will be stored at the address of y….

In the output printf statement we have written sum is equal to percent d, so it will print contents of the variable sum in place of %d…. in integer format, i.e. it will give output as Sum is equal to 30.

If we want output as 10 + 20 = 30 then we need to write format specifier at each location and access contents maintaining same sequence as that of sequence of format specifier…. So here if you write printf %d plus %d equal to %d then comma separated sequence should be x, y, and sum.

Access this framework with lot more stuff like C language MCQ, viva and interview preparation, Glossary, Solved Programs of C language etc. please register free on (log in Tab) Register / Login


Leave a Reply