List of Common C Format Specifiers
Format Specifier | Description |
---|
%c | For character type |
%d | For signed integer |
%e or %E | For scientific notation of floats |
%f | For float type |
%g or %G | For float type with the current precision |
%i | For signed integer |
%ld or %li | For long integer |
%lf | For double |
%Lf | For long double |
%lu | For unsigned int or long |
%lli or %lld | For long long |
%llu | For unsigned long long |
%o | For octal representation |
%p | For pointer |
%s | For string |
%u | For unsigned int |
%x or %X | For hexadecimal representation |
%n | Prints nothing |
%% | Prints % character |
Examples of C Format Specifiers
1. Character Format Specifier - %c
The %c
format specifier is used for the char
data type in C. It works for both input and output formatting.
// C Program Example#include int main() {char c;scanf("%c", &c);printf("Entered character: %c", c);return 0;}
2. Integer Format Specifier (signed) - %d
For signed integers, the %d
format specifier can be used with input and output functions in C.
// C Program Example#include int main() {int x;scanf("%d", &x);printf("Printed using %%d: %d\n", x);return 0;}
3. Unsigned Integer Format Specifier - %u
The %u
specifier handles unsigned integers. A negative integer passed with %u
converts to its first complement.
// C Program Example#include int main() {unsigned int var;scanf("%u", &var);printf("Entered Unsigned Integer: %u", var);return 0;}
4. Floating-Point Format Specifier - %f
To handle floating-point numbers, you can use %f
. Other related specifiers include %e
and %E
for scientific notation.
// C Program Example#include int main() {float a = 12.67;printf("Using %%f: %f\n", a);return 0;}
FAQs About C Format Specifiers
Does C have a format specifier for binary numbers?
No, C doesn't natively provide a binary format specifier.
What is a formatted string?
A formatted string in C is a string that dictates how data will be displayed or retrieved, typically used in functions like printf()
or scanf()
.