In C, the printf() function is used to print formatted output to the standard output stream (stdout), which is usually the console.
- It can print text, variables, expressions and formatted values.
- Format specifiers control how different types of values are displayed.
#include <stdio.h>
int main() {
// Using printf to print the text "Hi!"
printf("Hi!");
return 0;
}
Output
Hi!
Explanation: In this program, the printf function print the text "Hi!" on the console screen.
Syntax of printf
The printf() function is defined inside <stdio.h> header file.
printf("format_string", arguments...);
Parameter:
- format_string: A string that specifies the text and formatting of the output. It can contain format specifiers as placeholders for values.
- arguments: Values or variables corresponding to the format specifiers in the format string.
Return Value: Returns the number of characters printed after successful execution. If an error occurs, a negative value is returned.
Format Specifier in printf
Format specifiers are special sequences beginning with % that specify how an argument should be formatted and displayed. Some commonly used format specifiers are:
| Specifier | Type |
|---|---|
%d | int |
%f | double |
%c | char |
%s | String |
%x | Unsigned integer in hexadecimal |
%p | Pointer |
Format specifiers can also be combined with width, precision, flags and other formatting options to control the appearance of the output.
Examples of printf()
The following examples demonstrate different ways to use printf() for formatted output.
Print Variables and Expressions
#include <stdio.h>
int main() {
int a = 99;
int b = 1;
// Printing Variables and
// value of expressions
printf("Sum of %d and %d is %d", a, b,
a + b);
return 0;
}
Output
Sum of 99 and 1 is 100
Explanation: In the above program, the format specifier %d is used to print integers variables using printf. Here, it prints the values of a, b, and the result of a + b.
Print Literal Values
#include <stdio.h>
int main() {
// Printing Variables
// and value of expressions
printf("Sum of %d and %d is %d", 99, 1,
99 + 1);
return 0;
}
Output
Sum of 99 and 1 is 100
Explanation: The format specifier %d is used to print integer literals. It substitutes 99, 1, and the result of 99 + 1 into the string.
Right-Align Output Using Width
A positive field width can be used to right-align the output.
#include <stdio.h>
int main() {
char s[] = "Welcome to GfG!";
// Printing right aligned string of width 40
printf("%40s", s);
return 0;
}
Output
Welcome to GfG!
Explanation: The format specifier %40s prints the string s right-aligned with a minimum width of 40 characters. If the string is shorter than 40 characters, it is padded with spaces on the left.
Left-Align Output Using Width
The - flag left-aligns the output within the specified field width.
#include <stdio.h>
int main() {
char s[] = "Welcome to GfG!";
// Printing left aligned string of width 50
printf("%-50s", s);
printf("Geeks");
return 0;
}
Output
Welcome to GfG! Geeks
Explanation: The format specifier %-50s prints the string s left-aligned with a minimum width of 50 characters. The remaining spaces are padded with blanks, followed by printing Geeks.
Add Leading Zeros to an Integer
The precision for an integer can be used to specify the minimum number of digits. Leading zeros are added when necessary.
#include <stdio.h>
int main() {
int n = 2451;
// Precision for integral data
printf("%.10d\n", n);
return 0;
}
Output
0000002451
Explanation: The format specifier %.10d ensures the integer n is printed with a precision of 10 digits. If n has fewer digits, it is left-padded with zeros to meet the required precision.
Limit Digits After the Decimal Point
For floating-point output, precision specifies the number of digits printed after the decimal point when using %f.
#include <stdio.h>
int main() {
float f = 2.451;
// Precision for float data
printf("%.2f", f);
return 0;
}
Output
2.45
Explanation: The format specifier %.2f ensures the floating-point number f is printed with 2 digits after the decimal point. It rounds the value if necessary.
Limit the Number of Characters in a String
For %s, precision specifies the maximum number of characters to be printed.
#include <stdio.h>
int main() {
char s[] = "Welcome to GfG!";
// Print with 3 decimal places
printf("%.7s", s);
return 0;
}
Output
Welcome
Explanation: The format specifier %.7s prints only the first 7 characters of the string s, truncating the rest if it exceeds this length.