Parameters
The printf function accepts a variable number of arguments, depending on the format string. Here are the parameters of printf:
-
format (required): A string that contains the text to be printed, along with optional format specifiers & escape sequences. This parameter is mandatory & must always be provided.
- ... (optional): Additional arguments that correspond to the format specifiers in the format string. The number & type of these arguments must match the placeholders specified in the format string.
Example
printf("My name is %s and I am %d years old.", "Rahul", 25);
In this case, the format string contains two format specifiers: %s for a string & %d for an integer. The additional arguments "Rahul" & 25 are passed to match these format specifiers.
Note : It's important to ensure that the number & type of arguments match the format specifiers in the format string. If there is a mismatch, it can lead to undefined behavior or incorrect output.
Return Value
The printf function returns an integer value that represents the number of characters printed to the console. If the printing is successful, the return value is non-negative. However, if an error occurs during the printing process, printf returns a negative value.
Here are a few important points to note about the return value of printf:
-
If the printing is successful, the return value is the total number of characters written, excluding the null terminator.
-
If an output error occurs, a negative value is returned. The specific value depends on the implementation & the nature of the error.
- It's generally a good practice to check the return value of printf to handle any potential errors gracefully.
Example
int charactersWritten = printf("Hello, world!");
if (charactersWritten < 0) {
// Handle the error
// ...
}
In this case, we store the return value of printf in the variable charactersWritten. We can then check if the value is negative to detect any errors that may have occurred during the printing process.
Example of printf
C
#include <stdio.h>
int main() {
char name[] = "Rinki";
int age = 25;
printf("My name is %s and I am %d years old.\n", name, age);
return 0;
}

You can also try this code with Online C Compiler
Run Code
In this example:
-
We include the stdio.h header file, which contains the declaration of the printf function.
-
Inside the main function, we declare two variables: name (a character array) & age (an integer).
-
We use the printf function to print a formatted string. The format string contains two format specifiers: %s for a string & %d for an integer.
-
We pass the name & age variables as additional arguments to printf, which correspond to the format specifiers in the order they appear.
- The \n escape sequence is used to add a newline character at the end of the printed string.
When we run this program, the output will be:
My name is Rinki and I am 25 years old.
The printf function replaces the format specifiers with the actual values of the variables passed as arguments, resulting in the formatted output.
Formatting in C printf
One of the powerful features of printf is its ability to format the output using format specifiers.
Format specifiers are placeholders that begin with a percent sign (%) & specify how to format & print the corresponding argument.
Here are some commonly used format specifiers in C:
-
%d or %i: Prints an integer value.
-
%f: Prints a floating-point value.
-
%c: Prints a character.
-
%s: Prints a string.
-
%p: Prints a pointer address.
- %x or %X: Prints an integer value in hexadecimal format.
In addition to these basic format specifiers, printf allows you to control the width, precision, & alignment of the printed output. You can use additional characters & modifiers within the format specifier to achieve the desired formatting.
For example:
int value = 42;
printf("%10d\n", value); // Prints " 42"
printf("%.2f\n", 3.14159); // Prints "3.14"
printf("%-10s\n", "Hello"); // Prints "Hello "
In the first example, %10d specifies a minimum width of 10 characters for the integer value.
If the value is shorter than 10 characters, it will be padded with spaces on the left.
In the second example, %.2f specifies a precision of 2 decimal places for the floating-point value. The printed output will be rounded to 2 decimal places.
In the third example, %-10s specifies a minimum width of 10 characters for the string, with left alignment. The string will be printed left-aligned, & any remaining space will be padded with spaces on the right.
Syntax of Format Specifier
The general syntax of a format specifier in C printf is as follows:
%[flags][width][.precision][length]specifier
Here's what each part of the format specifier represents:
-
%: The percent sign marks the beginning of a format specifier.
-
flags (optional): One or more characters that modify the output format, such as - for left alignment or 0 for zero-padding.
-
width (optional): A number that specifies the minimum width of the output. If the printed value is shorter than the specified width, it will be padded with spaces or zeros.
-
.precision (optional): A number that specifies the precision for floating-point values or the maximum number of characters to print for strings.
-
length (optional): A modifier that specifies the size of the argument, such as l for long or h for short.
- specifier: A character that represents the type of the argument to be printed, such as d for integer, f for floating-point, or s for string.
Here are a few examples to illustrate the usage of format specifiers:
printf("%d\n", 42); // Prints "42"
printf("%10.2f\n", 3.14159); // Prints " 3.14"
printf("%-10s\n", "Hello"); // Prints "Hello "
printf("%*d\n", 5, 42); // Prints " 42"
printf("%ld\n", 1234567890); // Prints "1234567890"
In the first example, %d is used to print an integer value.
-
In the second example, %10.2f specifies a minimum width of 10 characters & a precision of 2 decimal places for a floating-point value.
-
In the third example, %-10s specifies a minimum width of 10 characters & left alignment for a string.
-
In the fourth example, %*d uses the * flag to specify the width as an argument, allowing dynamic width specification.
- In the fifth example, %ld is used to print a long integer value, with the l length modifier.
Examples of printf() in C
Now, let's explore a few more examples of using printf in C to reinforce our understanding.
Example 1: Printing variables of different types
C
#include <stdio.h>
int main () {
int age = 25;
float height = 5.7;
char grade = 'A';
const char* message = "Hello, world!";
printf("Age: %d\n", age);
printf("Height: %.2f\n", height);
printf("Grade: %c\n", grade);
printf("Message: %s\n", message);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Output:
Age: 25
Height: 5.70
Grade: A
Message: Hello, world!
In this example, we declare variables of different types (int, float, char, & const char*) & use the appropriate format specifiers to print their values.
Example 2: Printing formatted table
C
printf("%-10s %-10s %-10s\n", "Name", "Age", "City");
printf("%-10s %-10d %-10s\n", "Akash", 25, "New York");
printf("%-10s %-10d %-10s\n", "Harsh", 30, "London");
printf("%-10s %-10d %-10s\n", "Pallavi", 35, "Paris");

You can also try this code with Online C Compiler
Run Code
Output
Name Age City
Akash 25 New York
Harsh 30 London
Pallavi 35 Paris
In this example, we use printf to create a formatted table. The %-10s format specifier is used to left-align the strings within a width of 10 characters. The %-10d format specifier is used for integers. By aligning the format specifiers, we can create a visually appealing table.
Frequently Asked Questions
Can printf handle other types besides strings and numbers?
Yes, printf can also handle characters with %c, and even memory addresses with %p, among others.
What happens if the format specifier does not match the data type?
Misaligning format specifiers and data types can lead to runtime errors or incorrect output. It's essential to ensure they match correctly.
Is it possible to print a percentage sign using printf?
Yes, to print a percentage sign (%), you double it in the format string: %% will output a single %.
Conclusion
In this article, we have learned about the printf function in C, which is used to print formatted output to the console. We discussed it’s syntax, parameters, & return value of printf. We also explained the concept of format specifiers & how they can be used to control the appearance of the printed output. With the help of different examples, we saw how printf can be used to print variables of different types, create formatted tables, & more.
You can refer to our guided paths on the Code 360. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.