C printf()
In this page:
printf("format string");
printf("text %specifier", variable_name);
What is printf()?
printf ("print formatted") is the standard C library function for writing text and variable values to the screen -- it's part of stdio.h and is the primary way a C program communicates anything back to the person running it.
Example: What is printf()?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("Hello from printf");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing Variables
Format specifiers like %d or %s inside a printf string act as placeholders that get replaced, in order, by the values of the arguments you pass after the format string -- mismatching the specifier and the variable's type produces garbage output.
Example: Printing Variables
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Print formatted text to the screen
printf("%d", age);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Printing Multiple Values
When printf is called with multiple format specifiers, each one consumes the next argument in the list in left-to-right order, so printf("%d and %d", a, b) prints a's value first and b's second, matching their position.
Example: Printing Multiple Values
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 1, b = 2;
// Print formatted text to the screen
printf("%d and %d", a, b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Using Newlines in Output
Because printf never adds a line break on its own, consecutive printf calls print immediately next to each other on the same line unless you explicitly include \n wherever you want output to move to a new line.
Example: Using Newlines in Output
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("First\n");
// Print formatted text to the screen
printf("Second");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Formatting Floating-Point Precision
Writing %.2f instead of %f rounds a floating-point value to exactly 2 decimal places when printed -- essential for displaying things like currency cleanly instead of C's default of six digits after the decimal point.
Example: Formatting Floating-Point Precision
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare price and set it to 19.9999
double price = 19.9999;
// Print formatted text to the screen
printf("%.2f", price);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using the wrong format specifier such as
%dfor afloat, which prints garbage. - Giving
printffewer arguments than format specifiers, which is undefined behavior. - Passing a user-supplied string as the format, such as
printf(input);, which is a security risk.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: