Skip to content
StudyFame

Format Specifiers in C

When you print a variable with printf(), you must tell C what type of value it is. You do this with a small code starting with %, called a format specifier.

%d for integers, %f for decimals, %c for characters... In this lesson you will learn the complete list with examples.

What You Will Learn

  • What a format specifier is.
  • The complete list of format specifiers.
  • Printing the same value in different formats.
  • Printing a % sign.
  • Which specifiers are different in printf() and scanf().

Format Specifiers in C Explained

Exam definition: A format specifier is a sequence beginning with % that tells printf() or scanf() the type of data to print or read.

Complete list of format specifiers

SpecifierUsed forExample output
%d or %iint (signed decimal)25
%uunsigned int4000000000
%ldlong123456789
%lldlong long9000000000
%hdshort2026
%ffloat and double in printf3.140000
%lfdouble (needed in scanf)3.140000
%Lflong double3.140000
%eScientific notation3.140000e+00
%ccharA
%sStringHello
%x / %XHexadecimal (small / capital letters)1a / 1A
%oOctal32
%pMemory address (pointer)0x7ffe5c1a2b3c
%zuResult of sizeof4
%%Prints a % sign%

Memory trick: The letter tells the type: d = decimal integer, f = float, c = character, s = string, x = hex, o = octal, p = pointer, u = unsigned. Add l for long.

%f vs %lf

  • In printf(), both float and double print with %f (%lf also works).
  • In scanf(), you must use %f for float and %lf for double.
  • %f prints 6 digits after the decimal point by default. Use %.2f for 2 digits.

Syntax: Using a Format Specifier

printf("text %specifier text", value);
printf("Age: %d, Grade: %c\n", age, grade);   /* one specifier per value, in order */

The values are matched to the specifiers from left to right. The first specifier gets the first value, and so on.

Example 1: One Value, Many Formats

#include <stdio.h>

int main(void)
{
    int n = 65;
    float pi = 3.14159f;

    printf("As integer    : %d\n", n);
    printf("As character  : %c\n", n);
    printf("As hexadecimal: %x\n", n);
    printf("As octal      : %o\n", n);
    printf("Float default : %f\n", pi);
    printf("Float 2 digits: %.2f\n", pi);
    printf("Scientific    : %e\n", pi);
    printf("Discount      : 50%%\n");

    return 0;
}

Output

As integer    : 65
As character  : A
As hexadecimal: 41
As octal      : 101
Float default : 3.141590
Float 2 digits: 3.14
Scientific    : 3.141590e+00
Discount      : 50%

Code Explanation

  1. %d prints 65 as a normal number.
  2. %c prints the character whose ASCII code is 65, which is A.
  3. %x prints 65 in hexadecimal: 4×16 + 1 = 65, so 41.
  4. %o prints 65 in octal: 1×64 + 0×8 + 1 = 65, so 101.
  5. %f prints 6 digits after the point; %.2f rounds to 2 digits.
  6. %e prints 3.14159 as 3.141590 × 10⁰.
  7. %% prints one % sign.

How Format Specifiers Work

  • printf() reads the text from left to right and copies it to the screen.
  • When it finds a %, it takes the next value from the list and converts it according to the letter after %.
  • The value in memory does not change. Only how it is displayed changes, which is why 65 can be shown as 65, A, 41 or 101.
  • If the specifier and the value type don't match, the output is wrong (and it is undefined behaviour).

Example 2: Printing a Student Report

#include <stdio.h>

int main(void)
{
    char name[] = "Riya";
    int roll = 12;
    char grade = 'A';
    float percent = 91.456f;

    printf("Name: %s\n", name);
    printf("Roll: %d | Grade: %c\n", roll, grade);
    printf("Percentage: %.1f%%\n", percent);

    return 0;
}

Output

Name: Riya
Roll: 12 | Grade: A
Percentage: 91.5%

%s prints the whole string. %.1f rounds 91.456 to one decimal place: 91.5.

Important Points to Remember

  • Every format specifier starts with %.
  • %d int, %f float, %c char, %s string.
  • %lf is required for double in scanf().
  • %% prints a percent sign.
  • Values match specifiers in order, left to right.

Common Mistakes Beginners Make

MistakeWhat happensFix
printf("%d", 3.5);Garbage outputUse %f for decimals
printf("%f", 10);Prints 0.000000 or garbageUse %d, or write 10.0
printf("50%");Unpredictable outputUse 50%%
Fewer values than specifiersGarbage values printedOne value for every specifier
scanf("%f", &d); with double dWrong value storedUse %lf for double in scanf

Format Specifiers Practice Questions

  1. What is a format specifier?
  2. Write the format specifiers for int, float, char, string and double (in scanf).
  3. How do you print the % symbol with printf?
  4. What does printf("%c", 66); print?
  5. What does printf("%.3f", 2.5); print?
Show answers
  1. A code starting with % that tells printf/scanf the type of data to print or read.
  2. %d, %f, %c, %s, %lf.
  3. With %%.
  4. B (ASCII 66).
  5. 2.500

Coding Exercise

Store the number 255 in an int. Print it in decimal, hexadecimal (capital letters) and octal on separate lines.

Show solution
#include <stdio.h>

int main(void)
{
    int n = 255;
    printf("Decimal: %d\n", n);
    printf("Hex    : %X\n", n);
    printf("Octal  : %o\n", n);
    return 0;
}
Decimal: 255
Hex    : FF
Octal  : 377

Quick Quiz: Format Specifiers in C (MCQ)

  1. Which format specifier prints a string?

    A) %c   B) %s   C) %d   D) %f

  2. Which is required to read a double with scanf?

    A) %f   B) %d   C) %lf   D) %ld

  3. What does printf("%x", 255); print?

    A) 255   B) ff   C) 377   D) FF

  4. Which prints a memory address?

    A) %a   B) %m   C) %p   D) %x

  5. By default, %f prints how many digits after the decimal point?

    A) 2   B) 4   C) 6   D) 8

Show answers
  1. B: %s
  2. C: %lf
  3. B: ff (small letters with %x).
  4. C: %p
  5. C: 6.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

int%d or %i
float%f
double%f in printf, %lf in scanf
char / string%c / %s
unsigned / long long%u / %lld
hex / octal%x / %o
address%p
percent sign%%

Frequently asked questions

What is the difference between %d and %i?
In printf they are the same. In scanf, %i also reads octal and hex input (like 012 or 0x1A).
Why use %lf for double in scanf?
scanf needs to know the exact size to store: %f = 4-byte float, %lf = 8-byte double.
How do you print % in printf?
Write %%.
What happens if the format specifier is wrong?
The output is wrong or garbage; it is undefined behaviour.

Remember

  • Letter = type: decimal, float, char, string, hex, octal, pointer.
  • %.2f = 2 digits after the point.

Next Topic

Next lesson: Escape Sequences in C. You will learn special characters like \n and \t that control how text is printed.