Course Content

  • printfandscanf

Course Content

printf() and scanf() Functions

Hello everyone my name is Atharva and we are continuing this C programming course. (pause 6 sec)

In this video, we will be seeing two specific functions, which are called printf function and scanf function, printf we have seen in last few practical videos, we know that this printf function helps us in bringing out the output, which means the output screen, the black colour screen that comes below, which we also call as console, on that how our output can be printed.

So, that print is done by printf function, apart from that, the scanf function is used in our program to take the input, our user or the person who will be using our program.

If we want to consider any value from that person, in our program, and there is a specific operation that we have to conduct, that we can do with the help of scanf.

We will see and understand what are the functionalities of printf and scanf.

This printf and scanf is used for output and input respectively, which means, printf is used for bringing out the output and scanf is used to put the input.

We can see here as an example, we have used the printf function in which there are two round brackets and in the round brackets, whichever line we are writing in blue colour, it will be printed on our output screen.

So, this is saying “enter a number.”

So, to our user or who is compiling our program, we have printed through him, that enter a number.

Now where will he enter the number, where will he type the number?

So, all those things are a part of input and that is done by scanf.

What does scanf do? It makes a path for the user to give the input, and brings the cursor there for the user, so that the user can provide us any value or input, so that considering the program the further activities can be done.

Going ahead we'll see how the printf function syntax is written.

The printf syntax has two parts.

First part is format string, which will always be in double quotes, which you can see here in example in blue colour.

After that, separating it with a comma, there will be an argument list.

Now, what is the argument list? What is the format string? Even this we will understand slowly and we will also explore practically how it works.

So, this first part in which there are double quotes, we have seen it earlier as well, that if in double quotes we have to write a line, that line will be seen in the output.

So, yes, the blue part that is visible in these double quotes, in that we have to write that part which we have to show in our output, to show it in the output, suppose we have to print the inside value of the variable.

Since it's a variable, its value won't be fixed, we will keep changing it in the program like we have seen the addition program, in that the addition of a and b happened and the value that came out of it, the sum of it.

We had saved it under the result variable, if you remember.

So, that result variable’s value if we want to print on the output screen, so how will we do that.

Since in the double quotes whichever line we write, it gets printed and value is such a thing which is stored in the variable, we don't know what will be its exact value after the solution.

So extracting that value from inside that variable, is the work of our printf function and there are few things that we will pass here as an argument list.

Which means whichever variable we're using, its value we are printing that variable’s name, will come in our argument list.

Like we had to print a result, so in the argument list after the comma, we wrote the name of a result variable, and the format string that is there, how will you know that, we have to print this variable.

There are few format specifiers which we will see in the coming slides, how they work.

So, we'll define that thing over there.

After that, our compiler will get to know that the value of this variable has to be extracted from here.

So, we will go ahead and see the next slide, about format specifiers.

Now, first of all, without seeing you understand this, that percent C, percent S, this is used for which things, which means percent C, you can see that it can be used for a single character.

It means if you want to print a character on your screen, which is stored in the variable, so this percent C specifier that is there or we will call it as sign.

That we will have to use.

In the same way if we have to print a string or a sentence which is stored in a variable, for that we will use percent S, percent HI, we will use for a short data type, in the same way there is percent HU, percent LF and even percent N, in the same way there are more format specifiers, which are percent D, if you want to print any integer value which is stored in the variable, you will utilise percent D.

Now, where will you do it and how will you do it, that we will see in a practical way.

For now, you just understand that if you want to print integer, you have to understand percent D, if you want to print floating point you have to write percent F, if you want to print a character then percent C and after that if you just want to print percent symbol, as it is if the percent is used it has some other meaning, which means it is telling the meaning of format specifier, but you want to print that percent sign as it is, then you do percent percent.

Now you just read it once, see it once, as and when slowly you will make the programs, type them, see them and use them practically, slowly these things will get clear in your mind, okay.


So, we saw the printf function, after printf we saw format specifiers.

After this we will quickly see what the scanf function is doing.

Scanf function is used to input the data, it reads the input data from the console, the console, the black screen which was coming below, where we were giving the output, from there we will even take the input.

How will we take it? With the help of scanf.

Scanf has the same format.

In the first part, there will be a format string in which double quotes will come, and after the comma, there will be an argument list where you will have to put the variable names in which you have to store the values.

So, in the bracket those values will be input, and which all variables will be there in the argument list, in that, those respective values will be stored.

Now we will go on to our Visual Studio code.

And we will understand this practically.

I have made this code already.

Where I have made use of both printf and scanf functions.

So, one by one we will understand the lines.

The first line you can see is, our preprocessor command line, in which we are including hash and writing.

We must import the STDIO dot H file, so that we can use some of its functionalities in our program.

Now, this printf and scanf, these two types of functions or functionalities which are there, these are basically defined in STDIO dot H, which means our STDIO dot H file which is there, in that already which means our C developers which were there, who made the C, they have told that this printf function which is there, what is it’s functionality and how it has to work.

So, when you include it with the preprocessor command, to the STDIO dot H file, what happens is the compiler goes into the file, read it and the compiler understands that whenever you will use printf or scanf in the program, the way that I have used, what will be its specific meaning.

So, these things the compiler understands, when you import this file or include it.

After that our main function starts, main function means, in which we are writing our actual program.

First line if we see, here we have done variable declaration, A, B and result, we have made three variables, all three have the data type of integer.

After that, we have used printf line, we have used printf function.

In the printf function, we had seen here, in the earlier presentation.

There are double quotes, in double quotes there are few lines written, and I want that this line should be displayed or printed in our output.

Now, this program is again for our two number addition.

But there is a small twist here.

Like in a previous program, you can see here.

What we were doing earlier, in the program we were defining the number, in num1 there will be 4, in num2 there will be 7.

So, this we were defining earlier itself.

These numbers we were not taking from the users, we were not taking the input from him, but in this case where our scanf and printf is used.

In this case, what we want is our user, the person who is using our program, he should give us the input so that, we can the values of his input and do the sum and print and give him the result.

So, here in the printf line, we have written one line that “please enter the numbers”.

So, I want that this should be printed on my screen, it will get printed ‘as it is.’

Now we will come to another line which is saying scanf…

Scanf is used for input.

So, how will input happen? Like we had seen in the previous slides, our format specifiers which are there, in the way if we want to input the integer or output it, in both ways we have to use percent D, and if you want to utilise the float value, we have to use percent F.

So, in that way if we see here, we have input A and B from our users.


And we know that A and B are our integers.

What we will do is the percent D sign which was there, the format specifier which we learned in the last slide, we will write it here.

So, you must be understanding in scanf, the first part which is there of double quotes.

 There you write a format specifier that determines which type of input you wish to take.

And in which variable it will go, that is written after the comma.

Like in A, the first one will go, in B the second one will go.

Since I have to insert two values, this percent D sign, I will write it twice.

Which means the first percent D will go in my A and the second percent D will be used for B.

So, whenever our user will put 2 values, the first value will always go in A, as it is written first and the second value will go in B.

So, here how much ever time percent D, percent F, percent C would be written, that many variables we will have to write before the comma so that correspondingly… The first percent D, it will go in A and the second percent D will go for B.

If this B was the floating-point number instead of D, we would have written F.

In this way our program would work.

Since both are integers, for both we have put percent D and percent D.

Now you can see here that we have also put an ‘and’, before both of them.

We call it “ampersand” (pronunciation: am puh sand)

We will see why it is used.

In the last video we saw, our variables are representational names.

The real values have been stored in our hard disk at some location.

How will we get the address of that location? That we get by putting the ampersand (pronunciation: am puh sand) sign.

Now what is happening now is, whenever you write percent D here, the compiler gets to know that some value needs to be inserted.

Now once the value is inserted, where will it get stored? It will get stored, in the location or address of a variable, that we have made in our hard disk, ampersand a would be providing that.

That location we will tell the compiler, and the value that has come inside from percent D in our program, with the help of ampersand A, it will be in a variable.

In the same way we will use the other percent D as well and with the help of ampersand B, our value will be stored in the B variable.

 So, basically ampersand is telling us the location address and on that address the value is going and getting stored.

And the representation name in our program is A and B respectively.


Now, we have taken the input from scanf.

In the program, we will see practically how it will work.

After that, what we did is the value that had come inside A and B, its addition we have got saved in the result…

Again, the result is variable, we have made it on the top, we have saved it in the result.

Now this result variable, that is made, I have to print it again, only then our user will know the 2 numbers that I had put, what is its summission or sum of it.

So, what we will do is, we will again write this line in double quotes, which I want as it is.

I am writing in it, “the sum of the number is, after that we have put colon, and after that again, percent D is used.”

You must be understanding that, it is a format specifier and as soon as you see percent D, you would understand that here some integer value will come.

Why will it come? Because percent D means the integer value.

If it was percent F, they would say floating point value.

Since it is percent D, it is an integer value.

And how are we cross checking it? This result we had made in the integer, in integer data type.

So, it will remain the integer.

So, in this way, I first printed a line from the printf on my screen, which was saying, please enter the numbers.

After that I used scanf, and with the help of scanf, with its help, I considered both those numbers in my program, with the help of these two format specifiers.


My result has been generated.

How? I got A plus B, and I saved it in it.

At the end, I wrote a printf statement, in which the line that was supposed to come, as it is, “the sum of the number is”, after that I wrote percent D, the percent D will be replaced with the result.

And what is the value stored in result A plus B.


And where is A plus B coming from, it is coming from scanf, in which A plus B both are given by the user.

So, you will understand the flow here.

First of all please enter the numbers that got printed, after that user saved both A plus B.

We added A plus B and got it stored in the result, and after that using percent D printed the value of the result.

 So, this was the program of our addition, where we are taking the number from the user and doing the addition.

Now, you will see white dot here, which means that it is not saved.

I will save it with Ctrl plus S, and I will right click and run the code.

Now it is telling me “please enter the numbers.”

I will enter 10.

To put the second number I will have to press space.

So, the compiler knows that the second number has started.

Now I will put 23 here.

I have put 10 space 23, these are the two numbers.

And after putting both those numbers, I will press the enter key.

You can see that as soon as we pressed enter, our third line, “the sum of the number is”, got printed.

And where we had to write percent D, it got switched with the result.

And in the result the value stored was 33.

Which means 10 plus 23, 33.

Here it was 10 and 23, adding both in the result 33 got stored.

And with the help of percent D, we got 33 printed here.

In the same way, we can do a lot of things with the printf function, we will quickly see, once, what else we can do.

What I will do is I will make 2 more variables, which will be named multiple...

We have made one more variable, multiple, and I will subtract...

Which means, what I am doing is, with these 2 numbers I will multiply as well as subtract, and I will get them printed as well.

So, I am typing here again MULTIPLE is equal to A multiplied by B, semicolon, after that, subtract is equal to A minus B, semicolon, now I have defined both multiple and subtract.

Now I even want to print it.

So, how will I print it… or I will do one thing, I will write this in one line, and I will even tell you what backslash N is.

Now we will first of all copy this, then put a space here, and paste it here.

Now you can see here, what I have done, the sum of the number is, will come in the first line.

After that will come multiple of the number is… and in the third one… it will come subtraction of the number is…

Since here was the result, I will put a comma in front of it, and write multiple, then I will put a comma again, and write, subtract.

Now this has become too long.

So, I will reduce its size, so that you can see it.



So, you can see the printf statement… What is this printf statement doing? First of all, the sum of the number is, then we have put percent D.

What is this percent D saying? It will print the result.

After that, again the multiple of the number is, after that again percent D has come, that will print the multiple.

Then we have said the subtraction of the number is percent D, so this will print our subtraction.

I have put all these three things in one line.

Now we will run it after saving, then we will check how it works.

I will right click, I will go on the run code, it will ask me 2 numbers.

I wrote here 20 and 10.

When I entered both, you can see that their sum comes as 30, the sum of the numbers is 30, the multiple of the number is 200, which means 20 into 10 is 200, the subtraction of the numbers is 10, 20 minus 10 is 10.

Now you can see that it has come in one line, to bring it in different lines, one line is used, one symbol is used, which we write in this way, backslash N.

After both these lines, I will write backslash N, with which the compiler can get to know that these 3 are not to be printed in one line, they have to be printed in different lines.

I will run it again, and this time I will change the number 30 space 20…

I will press enter, you can see that it has come in different lines.

Which means please enter the number that will come in the first line, the sum of the number is 50, the multiple of the number is, the subtraction of the number is 10.

All these 3 things have come in different lines.

Why? Because we have used this backslash N symbol.

Which means print on the next line.

So, in this way we can use printf, scanf.

And we can utilise the input and output easily and do the operations from the program.

So, all these things are very useful, when we are making big programs.

And slowly as and when we practice, all these things will stay in our subconscious mind.

So, that’s it for this video.

If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your queries.

In the next video, we will discuss what are these constants, how can we define constants in our program and by using constants, which are all things we can do.

Till that time, thank you so much.

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
Apple App Store
598K+ Downloads
App Download Section Circle 1
4.57 Avg. Ratings
App Download Section Circle 2
15K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere