DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 9 of 64
←PreviousPrevNextNext→

C Format Specifiers

What Are Format Specifiers?

In C, `printf` and `scanf` use format specifiers to determine how data is displayed or read.

They act as placeholders inside strings and must match the type (and, for integers/floats, the correct length) of the value being printed or read.

ℹ️ Note: In `printf`, `float` arguments are promoted to `double` (default argument promotions). In `scanf`, types must match exactly (e.g., `%f` → `float*`, `%lf` → `double*`).
SpecifierTypical UseExample (printf)Notes
%d / %isigned int (decimal)int x=5; printf("%d", x);For `scanf`, use "%d" to read int.
%uunsigned int (decimal)unsigned u=42; printf("%u", u);
%x / %Xunsigned int (hex)unsigned val=3735928559u; printf("%x", val);Lower/upper hex.
%ounsigned int (octal)unsigned v=511; printf("%o", v);
%ld / %lldlong / long long (decimal)long long n=123; printf("%lld", n);Match length with the type.
%fdouble (fixed-point)double pi=3.14; printf("%f", pi);In `printf`, floats are promoted to double.
%e / %Edouble (scientific)printf("%e", 3.14);
%g / %Gdouble (compact)printf("%g", 3.1400);Chooses %f or %e form.
%.2fdouble with precisionprintf("%.2f", 3.14159);Precision controls decimals (print only).
%cchar (character)char grade='A'; printf("%c", grade);For `scanf`, consider a leading space: " %c" to skip whitespace.
%schar* (C string)char name[]="John"; printf("%s", name);For `scanf`, limit width: e.g., "%99s".
%ppointer addressint *p=&x; printf("%p", (void*)p);Cast to `(void*)` for portability.
%zusize_t (unsigned)printf("%zu", sizeof(int));Use `%td` for ptrdiff_t (signed).
%%literal %printf("100%%");Prints a percent sign.

Controlling Output Format

• Precision can be set for floating-point values using `%.nf`. Example: `%.3f` prints 3 decimals.

• Width can be specified for alignment. Example: `%5d` reserves 5 spaces for an integer.

• Flags: `-` (left-justify), `+` (always show sign), `0` (zero pad), space (leading space for positive), `#` (alternate form for `%o/%x/%e/%f/%g`).

Example
#include <stdio.h>

int main(void) {
    double pi = 3.14159;
    printf("Default: %f\n", pi);
    printf("2 decimals: %.2f\n", pi);
    printf("Width 10: %10.2f\n", pi);
    printf("Zero-pad: %010.2f\n", pi);
    printf("Sign: %+10.2f\n", pi);
    return 0;
}
Output
Default: 3.141590
2 decimals: 3.14
Width 10:       3.14
Zero-pad: 0000003.14
Sign:      +3.14

scanf vs printf (Important Differences)

• In `scanf`, `%f` reads into a `float*`, **`%lf` reads into a `double*`**. In `printf`, `%f` always prints a `double`.

• Always pass the address of the variable to `scanf` (e.g., `&x`).

• For `%c`, leading whitespace in the input may be consumed by a prior read; use a leading space in the format string (e.g., " %c") to skip whitespace.

• Limit `%s` to avoid buffer overflows, e.g., `char name[100]; scanf("%99s", name);`

Example
#include <stdio.h>

int main(void) {
    int a; double d; char c; char name[100];
    /* Sample input: 7 3.5 Y Alice */
    if (scanf("%d %lf %c %99s", &a, &d, &c, name) != 4) return 1;
    printf("a=%d, d=%.3f, c=%c, name=%s\n", a, d, c, name);
    return 0;
}
Output
a=7, d=3.500, c=Y, name=Alice

Length Modifiers (Cheat Sheet)

ModifierUse WithMeansExample
h%d/%u/%xshort / unsigned shortshort s; scanf("%hd", &s);
l%d/%u/%x/%olong / unsigned longlong L; printf("%ld", L);
ll%d/%u/%x/%olong long / unsigned long longlong long q; printf("%lld", q);
z%usize_tsize_t n; printf("%zu", n);
t%dptrdiff_t (signed)ptrdiff_t d; printf("%td", d);
L%f/%e/%glong double (printf/scanf)long double x; printf("%Lf", x);

Quick Examples

Example
#include <stdio.h>

int main(void) {
    int n = 42; unsigned u = 42; double x = 1234.567; char ch = 'A';
    printf("int: %d, unsigned: %u\n", n, u);
    printf("hex: 0x%X, octal: %o\n", u, u);
    printf("fixed: %.2f, scientific: %.2E, compact: %g\n", x, x, x);
    printf("char: %c, string: %s\n", ch, "Hello");
    printf("size: %zu bytes\n", sizeof n);
    return 0;
}
Output
int: 42, unsigned: 42
hex: 0x2A, octal: 52
fixed: 1234.57, scientific: 1.234567E+03, compact: 1234.57
char: A, string: Hello
size: 4 bytes

Best Practices

1. Match specifiers exactly to the type and length (use length modifiers).

2. For `scanf`, always pass addresses (and limit `%s` input width).

3. Use `(void*)` with `%p` in `printf` for portability.

4. Prefer `%.nf` to control floating-point output precision; avoid comparing floats directly for equality elsewhere.

5. Validate `scanf` return values to ensure inputs were read successfully.

Test your knowledge: C Format Specifiers
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C BasicsTopic 9 of 64
←PreviousPrevNextNext→