In C, Format Specifiers are the operators used in conjunction with the printf() and scanf() functions to tell the compiler what type of data is being processed. They act as placeholders or "type-safety" bridges between the machine's binary data and the human-readable text.
Format specifiers start with the percent symbol (%) followed by a character that denotes the data type.
When you use a specifier like %d, the compiler doesn't just "see" a number; it looks at the internal memory representation. For instance, an int usually occupies 4 bytes. The %d specifier tells the printf function: "Take the next 4 bytes from the argument list and interpret them as a signed decimal integer."
Without format specifiers, the standard I/O functions would not know how many bytes to read or whether to treat those bytes as a whole number, a decimal, or a character.
| Specifier | Data Type | Description |
| %d or %i | int | Signed decimal integer. |
| %u | unsigned int | Unsigned decimal integer. |
| %f | float | Decimal floating-point (standard precision). |
| %lf | double | "Long Float" – double-precision floating-point. |
| %c | char | A single character. |
| %s | char[] (String) | A sequence of characters until a null terminator. |
| %p | void* | Memory address (Pointer) in hexadecimal format. |
| %x / %X | int | Unsigned hexadecimal integer (lower/upper case). |
| %o | int | Octal (base-8) representation. |
| %e / %E | float/double | Scientific notation (e.g., $1.2e+02$). |
| %% | None | Prints a literal % character. |
C allows you to control the visual layout of your data using modifiers between the % and the specifier character.
Field Width (%5d): Reserves at least 5 spaces for the number. If the number is shorter, it adds leading spaces (right-aligned).
Precision (%.2f): Controls the number of digits after the decimal point. Very common for currency or scientific data.
Left Alignment (%-10s): The minus sign forces the output to be left-aligned within the specified width.
#include <stdio.h> int main() { int id = 101; char grade = 'A'; float percentage = 92.4567; double pi = 3.1415926535; char name[] = "Alice"; printf("--- Student Report ---\n"); // Using width and precision printf("Name : %-10s\n", name); // Left-aligned, 10 width printf("ID : %05d\n", id); // Padded with zeros (00101) printf("Grade : %c\n", grade); // Float precision control printf("Percentage : %.2f%%\n", percentage); // Rounds to 92.46 // Scientific and Hexadecimal printf("Pi (Long) : %.10lf\n", pi); printf("ID in Hex : %x\n", id); return 0; }
Mismatching Types: Passing a float to a %d specifier will result in "garbage values" because the function interprets the floating-point bit pattern as an integer.
Using %f for double in scanf: While printf often lets you use %f for both, scanf strictly requires %lf for a double variable and %f for a float.
Forgetting %%: If you want to print "100%", writing printf("100%"); will cause an error or unexpected behavior. You must write printf("100%%");
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION