To summarize, the internal buffer created by printf is used to build the output string. The character or value is then copied to the output string as printf iterates over each character in the user string. Printf only stops at “%,” which indicates that there is a conversion argument. The types of arguments are char, int, long, float, double, or string. This is done, and the character is added to the output. If the parameter is a string, a string copy is performed. Finally, the Printf writes the full buffer to the stdout file when it finally reaches the end of the user string.”
Format
The printf() function’s syntax is as beneath. The string provided to the function is denoted here by “format.” The “…” shows that there may be more arguments after it.
Example # 01: Using the printf() Function to Print Text in the C Programming Language
Let us look into a very basic scenario to display a string with the help of the printf() function. Here we don’t need to add any format specifier as anything written between the quotation marks will be displayed at stdout, as shown in the picture below.
This is the most basic and simplified use of the printf() function, in which we can write anything between the inverted commas despite worrying about the length of the text.
Example # 02: Using the printf() Function to Print an Integer Variable in the C Programming Language
In this example, we will see how to display the integer with the printf() function. We will also use the scanf() function, which is used to read character, string, and numeric data from the input device. An integer variable is declared first with no value assigned to it. Then printf() command is written to display the message “enter a number:”. Then scanf() function is used with a format specifier “%d” for integer to assign the value to the location or address of variable “n” from the keyboard or any input device as the “&” operator is used as a prefix to the variable. It also adds a new life by default, unlike printf() after execution.
In the next line printf() function is written, which will display everything inside the quotations. Remember that the format specifier “%d” will be replaced by the value stored in the variable “n” at stdout. The output will look like “enter a number:”, then the user will type in the desired number, which will be stored in the address of variable “n”. Then “The number is:111” will be displayed at stdout.
Now let us look at how to use the printf() function with the float data type. Everything will be the same, except the format specifier used in the case of float will be “%f”, which will display the float value of the variable.
In the case of double data type, the format specifier used with printf() will be “%lf”, which will display the numeric value as a double at the output.
In the case of character data type, the format specifier used will be “%c”, which will display the character value at the output, as shown below.
Example # 03: Using the printf() Function to Print an Integer and Float Variable in the C Programming Language
Now let us see how to use printf() and different format specifiers for additional arguments of different data types in a single printf() and scanf() functions. 2 variables are declared of different types; integer “a” and float “b”. In the next line, a text is displayed through the printf() function. After that, the scanf() function reads the values from the keyboard and keeps them in the addresses of their variables. The format operators must be in the order in which the variables or their addresses are written. In the next line, the values are displayed by using the printf() function.
Example # 04: Using the printf() Function to Display an Integer Variable Along With its ASCII Value by Using Different Format Specifiers in the C Programming Language
This is another example of multiple arguments used in the printf() function. First, an integer variable is declared with the name “h”. Then a for loop is formed that will run five times. In the printf() command, ASCII values are displayed along with their corresponding character. Note that “%d” is used to display the numeric value, and “%c” is used to display the character value of the same variable. Another thing to note here is that “\n” is used here to go to the next line.
As we can see from the result above, the ASCII value of various characters was shown on the output screen once the code was compiled. Even though we only defined a string variable, the change in the format specifiers in the printf() method’s parameter gave the integer variable a different output in the form of Alphabet against the ASCII representation defined universally.
Conclusion
In conclusion, printing output is one of the typical tasks in every application. In this article, we learned about one of the ways to display the output through the printf() function in the C programming language. The implantation of several examples related to the printf() function was also focused on in this article. These examples will be helpful to you in understanding the usage of the printf() function in the C language, along with the different format specifiers for different purposes and data types, depending on what you want to display in the output.