What is printf() in C
The printf() function is included in the C standard library and is widely adopted in a program to display output on the console. This function accepts any type of input provided inside the closed brackets. However, the users must specify the type of output using the format specifiers. Without using any format specifiers, the printf() function will fail to generate an output on the console.
The following is the syntax to use printf() function in C programming.
What are the Parameters of printf() Function
The printf() function accepts the following arguments.
- Format: A pointer to a null-terminated string written to the file stream. It is made up of characters and an extra format specifier that starts with %.
- Additional arguments: Other arguments describing data to be printed. They appear in the format specifier’s order.
What Does a Format Specifier Include
The parts of the format specifier are given as:
- A leading sign %.
- One or more than one flags modifying conversion behavior (optional).
- If no sign is there, a space is inserted to the initiative of the result.
- The optional * or integer number is used to define the minimum width field.
- To define precision, an optional field that includes a. followed by a * or integers or nothing.
- A length modifier that is optional and defines the size of an argument.
- The conversion format specifier.
For more understanding look at the example of the printf() function in C given below:
The above code defines variables for a character, floating-point numbers, and an integer. It then uses printf() function to display the multiplication of the floating-point numbers, set the width of the character, and show the octal equivalent of the integer.
The most common format specifiers with printf() function are:
- %d or %i for printing integers
- %f for printing floating-point numbers
- %c for printing a single character
- %s for printing a string
Conclusion
The printf() is used in C to write the formatted strings. It is defined inside the <stdio.h> header file. In the above guide, we described the syntax, arguments, and working of printf() function along with an example. This tutorial also discussed about the format specifier.