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

C Format Specifiers

Format specifiers are the little placeholders like %d and %f that tell C what kind of value goes in a spot, like labeled blanks in a form. Each one matches a type.
Syntax
c
printf("%d", integer_value);
printf("%f", float_value);
printf("%c", char_value);
printf("%s", string_value);

What are Format Specifiers?

A format specifier is a placeholder inside printf or scanf's format string that tells the function what data type to expect at that position -- getting it wrong doesn't always crash your program, but it will corrupt or misread the value.

Example: What are Format Specifiers?

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

Specifiers for Integers (%d and %i)

%d and %i both format a signed base-10 integer and are interchangeable in printf, though %i is slightly more common in scanf conventions -- either way, the value must actually be an int-sized integer, not a float or a long.

Example: Specifiers for Integers (%d and %i)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	// Print formatted text to the screen
	printf("%d %i", a, a);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Specifiers for Decimals (%f and %lf)

%f prints or reads a float, while %lf is required specifically when reading a double with scanf (printf accepts either interchangeably for doubles due to how C promotes float arguments) -- mixing these up with scanf will corrupt your data.

Example: Specifiers for Decimals (%f and %lf)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	double price;
	sscanf("19.99", "%lf", &price);
	// Print formatted text to the screen
	printf("%f", price);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Specifiers for Text (%c and %s)

%c reads or prints exactly one character, while %s handles a whole string (a null-terminated array of characters) -- using %c where you meant %s will only capture the first letter and leave the rest of the input unread.

Example: Specifiers for Text (%c and %s)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare letter and set it to 'A'
	char letter = 'A';
	char word[] = "Hello";
	// Print formatted text to the screen
	printf("%c %s", letter, word);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Specifiers for Addresses (%p)

%p formats a pointer's value as its raw memory address, typically shown in hexadecimal -- useful mainly for debugging, to confirm where a variable actually lives in memory or that two pointers reference the same location.

Example: Specifiers for Addresses (%p)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%p", (void*)&x);
	// 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 %d for a float or double, which prints wrong values.
  2. Using %f with scanf for a double instead of %lf.
  3. Printing a long with %d instead of %ld.
🔒

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.