Format Specifiers in C

A format specifier is a special symbol used with printf() and scanf(). It tells the compiler what type of value we want to print or take as input. Format specifiers always start with the % symbol.

This page covers the most commonly used format specifiers. These are enough for most beginner programs, exam answers and daily practice.

Most Used Format Specifiers in C

Format Specifier Used For Data Type Example
%d Integer value int int age = 20;
%f Decimal value float float marks = 88.5;
%.2f Decimal value with fixed digits after point float or double printf("%.2f", marks);
%lf Double input in scanf() double scanf("%lf", &price);
%c Single character char char grade = 'A';
%s String or character array char[] char name[] = "Ravi";

Need More Format Specifiers?

Some format specifiers are useful but not used in every beginner program, such as %u, %ld, %lld, %x, %o, %p and %zu.

Study them separately here: Less Used Format Specifiers in C.

1. %d - Integer Format Specifier

%d is used to print or read integer values.

#include <stdio.h>

int main() {
    int age = 20;
    printf("Age = %d", age);
    return 0;
}

Output:

Age = 20

Explanation:

  • age is an integer variable.
  • %d tells printf() to print an integer value.

2. %f - Float Format Specifier

%f is used to print decimal values. By default, it prints 6 digits after decimal.

#include <stdio.h>

int main() {
    float marks = 88.5;
    printf("Marks = %f", marks);
    return 0;
}

Output:

Marks = 88.500000

Explanation:

  • marks is a float variable.
  • %f prints decimal values.
  • It prints 6 decimal places by default.

3. %.2f - Decimal Precision

%.2f prints only 2 digits after decimal and rounds the value.

#include <stdio.h>

int main() {
    float percentage = 87.4567;
    printf("Percentage = %.2f", percentage);
    return 0;
}

Output:

Percentage = 87.46

Explanation:

  • %.2f prints 2 digits after decimal.
  • The value is rounded from 87.4567 to 87.46.
  • This is useful for marks, percentage, money and average values.

4. %lf - Double Input Format Specifier

%lf is mainly used with scanf() for taking double input. For printing double with printf(), we normally use %f.

#include <stdio.h>

int main() {
    double price = 99.75;
    printf("Price = %.2f", price);
    return 0;
}

Output:

Price = 99.75

Explanation:

  • price is a double variable.
  • In printf(), %f prints double values.
  • In scanf(), use %lf to take double input.
double price;
scanf("%lf", &price);   // Correct for double input

5. %c - Character Format Specifier

%c is used to print or read a single character.

#include <stdio.h>

int main() {
    char grade = 'A';
    printf("Grade = %c", grade);
    return 0;
}

Output:

Grade = A

Explanation:

  • grade stores one character.
  • %c prints that character.
  • Character constants are written inside single quotes.

6. %s - String Format Specifier

%s is used to print or read a string. In C, a string is stored as a character array.

#include <stdio.h>

int main() {
    char name[] = "Devanshi";
    printf("Name = %s", name);
    return 0;
}

Output:

Name = Devanshi

Explanation:

  • name is a character array.
  • %s prints the complete string until null character \0.

Combined Example of Common Format Specifiers

#include <stdio.h>

int main() {
    int rollNo = 101;
    float percentage = 88.75;
    char grade = 'A';
    char name[] = "Devanshii";

    printf("Name: %s\n", name);
    printf("Roll No: %d\n", rollNo);
    printf("Percentage: %.2f\n", percentage);
    printf("Grade: %c", grade);

    return 0;
}

Output:

Name: Devanshi

Roll No: 101

Percentage: 88.75

Grade: A

Explanation:

  • %s prints string name.
  • %d prints integer rollNo.
  • %.2f prints float value up to 2 decimal places.
  • %c prints character grade.

Very Important: %f and %lf Difference

  1. In printf(), use %f to print both float and double.
  2. In scanf(), use %f for float input.
  3. In scanf(), use %lf for double input.
  4. %.2f means print only 2 digits after decimal.

Summary:

  • Format specifiers start with %.
  • %d is used for integer values.
  • %f is used for decimal values in output.
  • %.2f controls digits after decimal.
  • %c is used for character and %s is used for string.
  • In scanf(), use %lf for double input.

Welcome to ShikshaSanchar!

ShikshaSanchar is a simple and helpful learning platform made for students who feel stressed by exams, assignments, or confusing topics. Here, you can study with clarity and confidence.

Here, learning is made simple. Notes are written in easy English, filled with clear theory, code examples, outputs, and real-life explanations — designed especially for students like you who want to understand, not just memorize.

Whether you’re from school, college, or someone learning out of curiosity — this site is for you. We’re here to help you in your exams, daily studies, and even to build a strong base for your future.

Each note on this platform is carefully prepared to suit all levels — beginner to advanced. You’ll find topics explained step by step, just like a good teacher would do in class. And the best part? You can study at your pace, anytime, anywhere.

Happy Learning! – Team ShikshaSanchar