๐ข C Format Specifiers โ Printing and Reading Data in C
๐งฒ Introduction โ What Are Format Specifiers in C?
Format specifiers in C are placeholders used with printf()
and scanf()
functions to represent the type of data being printed or read. They tell the compiler what kind of variable is involvedโwhether it’s an integer, float, character, or stringโand how to format it during input/output operations.
๐ฏ In this guide, youโll learn:
- The complete list of format specifiers for all data types
- How format specifiers work with
printf()
andscanf()
- How to control formatting width and precision
- Common mistakes and tips for cleaner I/O handling
๐ท๏ธ Syntax of Format Specifiers
Format specifiers start with a percent symbol %
followed by a character that denotes the data type.
โ Example:
int age = 25;
printf("Age: %d", age);
The %d
tells printf()
to expect an int
value and print it accordingly.
๐ Common Format Specifiers in C
Specifier | Data Type | Description |
---|---|---|
%d / %i | int | Signed integer (decimal) |
%u | unsigned int | Unsigned integer |
%f | float , double | Decimal floating point |
%.2f | float | Float with 2 decimal places |
%lf | double | Long float for scanf() |
%c | char | Single character |
%s | char[] (string) | String of characters |
%x / %X | int | Hexadecimal (lower/uppercase) |
%o | int | Octal number |
%p | pointer | Memory address |
%% | โ | Prints a percent symbol % |
๐งช Examples Using printf()
and scanf()
๐น Printing Multiple Variables
int age = 30;
float height = 5.9;
char grade = 'A';
printf("Age: %d, Height: %.1f, Grade: %c\n", age, height, grade);
Output:
Age: 30, Height: 5.9, Grade: A
๐น Reading Input with scanf()
int score;
scanf("%d", &score);
Use %lf
for reading double
and %s
for strings (no spaces):
double pi;
char name[20];
scanf("%lf %s", &pi, name);
๐ Width & Precision Control in printf()
Format | Description |
---|---|
%5d | Print integer with width 5 |
%05d | Pad with zeros (e.g., 00042 ) |
%.2f | Display 2 decimal places for float |
%10s | Right-align string in 10-character width |
%-10s | Left-align string in 10-character width |
โ Example:
printf("%05d\n", 42); // 00042
printf("%10s\n", "C"); // C
printf("%-10s\n", "C"); // C
โ ๏ธ Common Mistakes
Mistake | Problem | Solution |
---|---|---|
Using wrong specifier | Compiler warning or incorrect output | Match data type to format specifier |
Forgetting & in scanf() | Input not stored or causes crash | Always use & (except for strings) |
%lf in printf() | Displays incorrectly | Use %f for both float and double in printf() |
๐ Format Specifier Summary Table
Type | printf() Specifier | scanf() Specifier |
---|---|---|
int | %d / %i | %d / %i |
unsigned int | %u | %u |
float | %f | %f |
double | %f | %lf |
char | %c | %c |
string | %s | %s |
pointer | %p | N/A |
๐ Summary โ Recap & Next Steps
Format specifiers help ensure that data is displayed and read correctly based on its type. Theyโre essential for writing interactive and formatted C programs that work across different environments.
๐ Key Takeaways:
- Use format specifiers with
printf()
andscanf()
to handle various data types %d
,%f
,%s
,%c
,%u
,%x
, and%p
are common placeholders- Use width and precision modifiers for clean formatting
- Always match the variable type with the correct specifier
โ๏ธ Real-World Relevance:
Format specifiers are used in logging systems, debug output, data processing, and command-line interfaces, where precision and structured formatting are vital.
โ Frequently Asked Questions (FAQ)
โ What is a format specifier in C?
โ
A format specifier is a symbol starting with %
that tells C how to read or print a variable of a specific data type.
โ Can I use %lf
with printf()
?
โ
Technically yes, but %lf
is treated the same as %f
in printf()
because of default argument promotions. %lf
is only required in scanf()
for reading a double
.
โ How do I print a percent sign using printf()
?
โ
Use %%
:
printf("Success: 100%%");
Output:
Success: 100%
โ What does %05d
mean?
โ It means print an integer using a width of 5 characters, padding with zeros:
printf("%05d", 7); // Output: 00007
โ Why do I need &
in scanf()
?
โ
scanf()
requires the address of the variable to store the input value. You must use &var
(except for strings, which are arrays and already decay to pointers).
Share Now :