← Back to C Course | Chapter 2: Input & Output | Lesson 1 of 7

C printf()

printf is how a C program shows messages and values on the screen, like a loudspeaker announcing them. You put placeholders in the text and it fills them in.
Syntax
c
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()?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using the wrong format specifier such as %d for a float, which prints garbage.
  2. Giving printf fewer arguments than format specifiers, which is undefined behavior.
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.