Subscribe Us

Printf ( ) function in C programming-C library function printf () - smartduniya

In this blog I will be talking about how to use printf () function in c programming . printf() is  a c library function . 

To  print any thing  write this function on c programming for the standard output.

The first part of basic structure of c program then you will know that the first section in c program is documentation section and it is an optional section.

 There is no any rule like if you do not document our source code then it will not work, but since it is considered to be good practice to have source code documented, lets have this section included.

Now the second step is an inclusion of header file but the question stands here that, 

What header file do ?

In the last post  I mentioned that the definition of input output function is stored in < stdio.h > named header file.< Stdio.h > holds the definition of all input output function .

So let's include it here in the link section and I have already told you in my last tutorial about how to include header file. So lets have this header file included here in the link section.

printf() function

Now that we have included < stdio.h >  header file, we have got the license to use printf () function.

Now let's directly jump to the main()  function section  skipping definition and global sections as there is no use of those sections in this program. This is the basic template of main function.

printf () function in c 

Now let's proceed to the syntax of printf()  function. So this is the syntax for printf () function and since I don't want this syntax to be compiled, I'm goanna comment it and I have already taught you in my post named "commenting  In C" that how to comment the statements in programming.

Now let us assume that we want to display "We are learning C programming."

So, lets follow this printf () syntax and try to display " We are learning C programming." on the output screen.

" We are learning C programming “ and we must not forget to end this printf ()  statement with a semicolon like

I told you in the first part of basic structure of c program .Whatever we write here in between this opening and closing curly braces must end with a semicolon.

 So if you do not end each of those statements that you write in between opening and closing curly brace with a semicolon and if you run your code then compiler is going to show a compilation error.

So we must end each of those statements that we write here with a semicolon. Now let's save this and this is our terminal, this is where we are going to compile our code. So lets compile our code. Gcc  printf.c is the command to compile the c program in DEV C++ compiler.

So let's go ahead and compile our code. Look, it is successfully compiled. if there had been any syntactical error, it would have shown some compilation error here, but as it is not showing any compilation error it means that it is successfully compiled. Now let's run our program, this is the command to run program in ubuntu So this is a compiler.

 So let's go a head and run our program. Look, it's displaying " We are learning C programming." and both of these statements are  appearing in the same line, but what if I want "we are student " to appear here in the next line right below " We are learning C programming "

code for printf function

So, lets go back to our code and make some changes here. I will insert a backward slash followed by  “ \n “.

 This is called backslash character and everything that follows backslash ( \n ) character will appear in the next line.

 

 

Now "We are student and We are learning C programming." will appear in the next line. Lets go, first lets first save this source code . Yeah guys  there is still a lot to talk about printf () function. This is not all about printf () function.

printf c programming

Since this post is made keeping beginners in mind, I will talk more about printf () function after the explanation of data type and variable, and now that you have learned how to display output to the standard output stream, your job is to display .your introduction .

If you have any query drop it in the comment box.  



Also Learn This :

>>Scanf() Function in C

>> C progarm to find absolute value

>> C program to find Square Root of Any Number

>> C program to find leap-year

>> Make a profit loss Calculator

>>How to write Comment in C

Post a Comment

0 Comments