Lesson 12 of 60 – Format Specifiers in C
20%

Format Specifiers in C

Format specifiers are special symbols used with functions such as printf() and scanf() to specify the type of data being displayed or read. They usually begin with the % symbol.

Note: The format specifier should match the type expected by the corresponding formatted input or output function.

1. What is a Format Specifier?

A format specifier is a special sequence beginning with % that tells a formatted I/O function how to interpret an argument.

printf("%d", age);

Here, %d tells printf() to format the corresponding argument as an integer.

2. Why Use Format Specifiers?

Format specifiers are useful because they tell formatted input and output functions what type of data is being processed.

  • Display integer values.
  • Display floating-point values.
  • Display characters.
  • Read values using scanf().
  • Control the appearance of formatted output.

3. %d for int

The %d conversion specifier is commonly used with an int value.

int age = 20;

printf("%d", age);

Output:

20

4. %i for int

The %i conversion specifier can also be used with an integer argument in printf().

int number = 100;

printf("%i", number);

In scanf(), %i has additional behavior: the input base can be detected from prefixes such as 0 or 0x.

5. %u for Unsigned Integer

The %u conversion specifier is used for an unsigned integer.

unsigned int count = 50;

printf("%u", count);

6. %f for Floating-Point Output

The %f conversion specifier is used to format a double argument in printf(). A float argument is automatically promoted to double when passed to printf().

float price = 99.50f;

printf("%f", price);

7. %lf in scanf()

When using scanf(), %lf is used to read a double.

double salary;

scanf("%lf", &salary);

The & provides the address where the input value should be stored.

8. %f in scanf()

In scanf(), %f is used to read a float.

float price;

scanf("%f", &price);

9. %c for Character

The %c conversion specifier is used for a single character.

char grade = 'A';

printf("%c", grade);

Output:

A

10. %s for String

The %s conversion specifier is used to print a null-terminated string.

char name[] = "Rahul";

printf("%s", name);

Output:

Rahul

11. %x for Hexadecimal

The %x conversion specifier displays an unsigned integer in lowercase hexadecimal notation.

int number = 255;

printf("%x", number);

Output:

ff

12. %X for Uppercase Hexadecimal

The %X conversion specifier displays an unsigned integer in uppercase hexadecimal notation.

int number = 255;

printf("%X", number);

Output:

FF

13. %o for Octal

The %o conversion specifier displays an unsigned integer in octal notation.

int number = 64;

printf("%o", number);

Output:

100

14. %e for Scientific Notation

The %e conversion specifier displays a floating-point value using scientific notation with a lowercase e.

double number = 12345.67;

printf("%e", number);

15. %E for Scientific Notation

The %E conversion specifier is similar to %e, but uses an uppercase E in the exponent.

double number = 12345.67;

printf("%E", number);

16. %g for General Floating-Point Format

The %g conversion specifier chooses between normal floating-point notation and scientific notation, using the more appropriate representation for the value and precision.

double number = 123.456;

printf("%g", number);

17. %% for Percent Sign

The sequence %% is used in printf() to display a percent sign.

printf("Success: 90%%");

Output:

Success: 90%

18. Width in printf()

A field width can be used to specify a minimum number of character positions for formatted output.

printf("%5d", 25);

The value is printed in a field with a minimum width of 5 characters.

19. Precision for Floating-Point Output

Precision can control the number of digits displayed after the decimal point when using printf() with %f.

double price = 99.5678;

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

Output:

99.57

20. Left Alignment

The - flag can left-align a value within the specified field width.

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

This places the value toward the left side of a field of at least 10 characters.

21. Zero Padding

The 0 flag can be used to pad certain numeric output with zeros.

printf("%05d", 25);

Output:

00025

22. Long Integer with %ld

The l length modifier can be used with d to format a long int.

long int population = 1000000L;

printf("%ld", population);

23. Long Long Integer with %lld

The ll length modifier can be used with d for a long long int.

long long int number = 9000000000LL;

printf("%lld", number);

24. Unsigned Long with %lu

The %lu conversion specification can be used to format an unsigned long value.

unsigned long population = 1000000UL;

printf("%lu", population);

25. size_t and %zu

The %zu conversion specification is used with a size_t value, such as the result of sizeof.

printf("%zu", sizeof(int));

This is the standard way to print a size_t value.

26. Format Specifiers with scanf()

The scanf() function also uses format specifiers to determine what kind of input should be read.

int age;

scanf("%d", &age);

The address operator & is commonly used with scalar variables so that scanf() can store the input value.

27. Character Input with scanf()

char grade;

scanf(" %c", &grade);

The space before %c tells scanf() to skip leading whitespace characters such as a leftover newline.

28. String Input with scanf()

For a character array, %s can read a word until whitespace is encountered.

char name[20];

scanf("%19s", name);

The width limit helps prevent input from exceeding the array's capacity.

29. Common Format Specifiers

Specifier Common Use
%d int
%u unsigned int
%f Floating-point output / float input
%lf double input with scanf()
%c Character
%s String
%x Hexadecimal
%o Octal
%zu size_t

30. Complete Format Specifier Example

#include <stdio.h>

int main()
{
    int age = 20;
    unsigned int students = 50;
    float percentage = 85.5f;
    double salary = 25000.75;
    char grade = 'A';
    char name[] = "Rahul";

    printf("Name = %s\n", name);
    printf("Age = %d\n", age);
    printf("Students = %u\n", students);
    printf("Percentage = %.1f\n", percentage);
    printf("Salary = %.2f\n", salary);
    printf("Grade = %c\n", grade);
    printf("Hex = %x\n", age);

    return 0;
}

This example demonstrates several commonly used format specifiers with different types of data.

📌 Key Points

  • Format specifiers generally begin with the % symbol.
  • %d is commonly used for int values.
  • %u is used for unsigned integers.
  • %f is used for floating-point output.
  • %lf is used to read a double with scanf().
  • %c is used for characters.
  • %s is used for strings.
  • %x and %X are used for hexadecimal output.
  • %o is used for octal output.
  • %zu is used for size_t values.
  • Width and precision can be used to control formatted output.
  • The format specification must be compatible with the corresponding argument type.

🧠 Quick Quiz

Question: Which format specifier is commonly used with printf() to display an integer of type int?