C Format Specifiers
In this page:
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?
// 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
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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)
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Using
%dfor afloatordouble, which prints wrong values. - Using
%fwithscanffor adoubleinstead of%lf. - Printing a
longwith%dinstead of%ld.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: