C

How printf() Functions works in C Language?

How printf() Functions works in C Language
Arjan Solanki
Written by Arjan

How printf Function Works in C Language: When we want to create a program in Computer and get a solution to any problem based on that program, then we see that there are three components in every Computer Program, which are called Input, Process And called Output.

How printf() Functions works in C Language

Input Section

In the Input section of the program, the user who uses the program has different types of Row Data Input related to the problem. Based on these Row Data, the program provides a meaningful result by completing its further work. All data is memory alloted in this section to store different types of values inputed by the user in computer memory. Whatever Data Input user does, all the data is stored in the respective Memory Block.

For example, if there are two numbers to add a program, then three blocks of memory block are allocated in this section. Two Memory Block two numbers are for the store and the third memory block is to store the results obtained by adding those numbers.

Process Section

in this section Different types of data related to the problem are performed by performing various types of Operations and the appropriate Result is generated.For example, if there are two numbers to add a program, then connecting both numbers is done in this section.

Output Section

After performing Required Operations related to the problem related to the results that are generated, the results of doing this on display on the monitor or printing on the Printer are done in this section.

For example, when the result of connecting two numbers is obtained, the result is sent to the output devices in this section.

Only a user can see the Input and Output section, so it is important to reproduce the Input and Output section in a fairly simple manner, so that the User Program can solve its problem in a simple way.

Output Function

In “C” language, when we want to display a result on a computer’s screen, we have to use the printf () function defined in the Header File named “stdio.h”.

printf () Function

All I / O Functions in “C” language are in the header file named stdio.h. When we have to display a store value in a Massage or a Variable, we use the printf () Function.

Its syntax is as follows-

printf (“Massage Ctrl_String1 Ctrl_String2 … Ctrl_String n”, Variable1, variable2, …, variable n);

Suppose we want to create a similar program that, if run, there is a String Display on the monitor. Since we are not doing any type of input and processing in our program, this program will only have the Output Section. If we want to make Algorithm of this program, then this Algorithm will be made as follows:

Algorithm

  1. START                               [Algorithm Starts here.]
  2. PRINT “Unique Info”      [Print the message. ]
  3. END                                    [Algorithm Ends here.]

If based on this Algorithm, we would like to create a program in “C” language, then the Source Code of that program will be as follows:

Program

/* Printing Only One Statement on the screen . */
/* For Getting the I/O Services */
#include<stdio.h>
/* Main Function from where Compiler Executes Program */
main()
/* Starting of Main Function */
{
/* Prints the Message */
printf(“ Unique Info ”);
/* Ends the Program */
}

Type this program in Turbo C ++ IDE in a new file and save the file as FirstPro.c. Next, compile and run the file. To compile a file, we can also use the Ctrl + F9 Key Combination. Using this key combination will also be run by file compile and output will be printed in brijvasi.

As mentioned earlier, all Input / Output Functions “C” are in a Header File of the Library, stdio.h, so the task of taking input from Keyboard or Output on Screen is accomplished by using Stored Functions in the same Header File. . Therefore, the header file named “stdio.h” has been #include in this program.

Every program has a main () function. main () Function is a special function, because when we compile a program of “C” Language, then the compiler first searches the main function in the Source program and the compiler gets the main function. Compiler starts converting the program into Machine Language from there.

A group of all statements written between {} (Opening and Closing) and Curly Braces are called Statement Block and these statements are called Execution. Since every “Function” in the “C” language begins with an Opening Curly Brace and ends at a Closing Curly Brace, therefore, as much as any executable Instructions of any program, it is written in the main statement of the Function .

Every statement in “C” language ends with a semi-colon and

Statements written in “C” (“”) between double quote are called String.

The printf () function of “Opening and Closing” between Double Quotes is printed on the screen, as it is an Output Statement, which works on displaying a message or value on the screen. .

On running this program, we get the output as follows:

OUTPUT:

  • Unique Info

If this same program is run four times, we get the output as follows:

OUTPUT:

  • Unique InfoUnique InfoUnique InfoUnique Info

This is because, because when we run this program for the second time, then the output of the last run program also shows us again.

If we want us to run the program as many times as we do not see the last time on the Output Screen, we have to use the clarscr () Function defined in the Header File named “conio.h”.

When we use this function, the location where the Function is used, it clears the message on the Function Screen.

To compile and run the program, we use the Ctrl + F9 Key Combination. But when the program is run, the program results are terminate immediately after displaying it on the monitor and we have to use the Ctrl + F5 Key Combination to see the output.

If we want to get the Output of the Program on display before Terminate the program and then terminate, we can use the getch () Function to get this feature.

getch () Function is also defined in the header file named “conio.h”. This Function works to get a Character in Input form Keyboard. So when we use this Function in our program, our program stops till it does not have any Key Press with the User Keyboard.

In this situation, if we use this Statement as the final Statement of our program, then our program will continue to perform the Output display until the Key is pressed.

If we modify the previous program while retrieving both of these features, then we can modify this program as follows:

/* Printing Only One Statement on the screen . */
/* For Getting the I/O Services */
#include<stdio.h>
#include<conio.h>

/* Main Function from where Compiler Executes Program */
main()

/* Starting of Main Function */
{

/* Clear th Screen */
clrscr();

/* Prints the Message */
printf(“ Unique Info ”);

/* waits for the user to press a key */
getch();

/* Ends the Program */
}

OUTPUT:
Unique Info

Program Flow

When this program is run, then if the program does not have any type of typing mistake, “C” Compiler first searches the main () Function.

After getting the Function, the compiler enters the Statement Block of the main () Function and first executes the clrscr () Function. This statement clears the Output Screen.

Then the next statement of the program is printf () Function Execute, which displays “Gopal & Krishna” Message on Screen.

Lastly, the third Function getch () is Execute. This Function waits for the user to press a key and by the time the user does not press the key, he can see the output on the screen. As soon as the user presses a key from the keyboard, the program becomes terminate.

The use of the printf () function is usually used to show Output of C language programs but this requirement is not met by the only printf () function, but sprintf (), puts (), etc. in C language And there are functions that are used to meet the needs related to various types of Output Display.

I sincerely hope that I have given you full information about How printf() Functions works in C Language and I hope you have understood about How printf() Functions works in C Language. I am convinced of all the readers that you too share this information with your neighbors, relatives, your friends, so that we will be aware of our interactions and will all benefit from it. I need people’s support from you so that I can bring you even more new information.

My always try is to do that I always help my readers or readers on every side, if you have any doubt of any kind, then you can ask me uncomfortable. I will definitely try to solve those Doubts. Please tell us what you think of this article, so that we can get a chance to learn something from your thoughts and improve some.

Get new posts by email
Please follow and like us:
0

About the author

Arjan Solanki

Arjan

To seek the challenging position in Development environment that needs Innovation, creativity, dedication and enable me to continue to work in a challenging and fast paced environment, leveraging my current knowledge and fostering creativity with many learning opportunities.

2 Comments