Saturday, October 10, 2026

Format Specifiers and Their Usage in C

C Programming: Format Specifiers and Their Usage

In C programming, format specifiers are used with functions such as printf() and scanf() to specify the type of data being displayed or accepted as input.

Common Format Specifiers

Format specifier

Data type

Example value

Usage

%d

int

25

Integer numbers

%i

int

25

Integer input/output

%f

float

12.5

Floating-point numbers

%lf

double

12.567

Double input using scanf()

%c

char

'A'

Single character

%s

char array

"Hello"

String

%u

unsigned int

25

Non-negative integer

%ld

long int

123456L

Long integer

%lld

long long int

123456789LL

Very large integer

%x

Unsigned integer

255

Hexadecimal in lowercase

%X

Unsigned integer

255

Hexadecimal in uppercase

%p

Pointer

Address

Memory address


In printf(), use %f to display a double value. In scanf(), use %lf to accept a double value.

C Program Demonstrating Format Specifiers

#include <stdio.h>

int main()
{
    int age = 18;
    float marks = 85.5f;
    double salary = 25000.75;
    char grade = 'A';
    char name[] = "Jiya";

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    printf("Marks: %f\n", marks);
    printf("Salary: %f\n", salary);
    printf("Grade: %c\n", grade);
    printf("Percentage: %.2f%%\n", marks);

    return 0;
}

Output

Name: Jiya
Age: 18
Marks: 85.500000
Salary: 25000.750000
Grade: A
Percentage: 85.50%

Explanation
  • %s displays the student's name.
  • %d displays an integer.
  • %f displays a floating-point value.
  • %c displays a single character.
  • %.2f displays a floating-point number with exactly two digits after the decimal point.
  • %% displays the % symbol.
  • \n moves the output to the next line.

Format Specifiers in scanf()

The scanf() function accepts input from the keyboard. The format specifier must match the variable's data type.

#include <stdio.h>

int main()
{
    int age;
    float marks;
    double salary;
    char grade;
    char name[30];

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Enter your marks: ");
    scanf("%f", &marks);

    printf("Enter your salary: ");
    scanf("%lf", &salary);

    printf("Enter your grade: ");
    scanf(" %c", &grade);

    printf("Enter your name: ");
    scanf("%29s", name);

    printf("\n--- Student Details ---\n");
    printf("Age: %d\n", age);
    printf("Marks: %.2f\n", marks);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);
    printf("Name: %s\n", name);

    return 0;
}

Why do we use & in scanf()?

The & operator provides the address of a variable so that scanf() can store the input in that variable. For a character array such as name, use name without &.
The space before %c in scanf(" %c", &grade) skips leftover whitespace, such as a newline from the previous input.

Width and Precision in printf()

Format specifiers can control how values are displayed.

Format

Example statement

Output

%d

printf("%d", 25);

25

%5d

printf("%5d", 25);

25

%05d

printf("%05d", 25);

00025

%f

printf("%f", 3.14159);

3.141590

%.2f

printf("%.2f", 3.14159);

3.14

%.3f

printf("%.3f", 3.14159);

3.142

%-5d

printf("%-5d", 25);

25


Usage hint:
  • Width: Minimum character spaces for displaying a value.
  • Precision: Number of digits after the decimal point.
  • Minus (-): Left-aligns the value.
  • Zero (0): Pads the value with leading zeros.