In C language, printf() function is used to print formatted output to the standard output stdout (which is generally the console screen).
#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", args...);
Parameter:
- formatted_string: It is a string that specifies the data to be printed. It may also contain a format specifier as a placeholder to print the value of any variable or value.
- args...: These are the variable/values corresponding to the format specifier.
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 symbols used in printf() and scanf() to specify the type of data being input or output.
- It ensures the correct interpretation of variable values during input/output operations.
- Different specifiers are used for different data types, like %d for integers, %f for floats, %c for characters, and %s for strings.
In addition to working as placeholders, format specifiers can also contain a few more instructions to manipulate how the data is displayed in the output.
Examples of printf() in C
The below examples demonstrate the use of printf() in our C program for different purposes.
Print a Variable
#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 this 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 a Literal
#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 the Output
We can right align the output using the width specifier with positive value.
#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 the Output with Specified Width
If we pass negative width, the minimum width will be the absolute value of the width, but the text will be left aligned.
#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 Zeroes to Integer
The precision sub-specifier adds leading zeroes to the integer.
#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 Point in Float
For floating point values, precision limits the number of digits to be printed after decimal points.
#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 Number of Characters in a String
For strings, precision limits the 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.