C Programs Tutorials | IT Developer
IT Developer

C Programming - C Format Specifiers



Share with a Friend

C Programming - C Format Specifiers

C Format Specifiers

Format specifiers in C are used in input/output functions like printf() and scanf() to specify the type of data to be displayed or read. They provide a way to define how variables should be formatted or interpreted.

List of Format Specifiers

  1. Integer Format Specifiers
Specifier Description Example

%d

Signed decimal integer

123, -45

%i

Signed decimal integer (same as %d)

123, -45

%u

Unsigned decimal integer

123

%o

Unsigned octal

017

%x

Unsigned hexadecimal (lowercase letters)

0x1a

%X

Unsigned hexadecimal (uppercase letters)

0x1A

  1. Floating-Point Format Specifiers
Specifier Description Example

%f

Decimal floating-point number

3.14, -2.718

%e

Scientific notation (lowercase)

2.5e+3

%E

Scientific notation (uppercase)

2.5E+3

%g

Automatically selects %f or %e

3.14, 2.5e+3

%G

Automatically selects %f or %E

3.14, 2.5E+3

%a

Hexadecimal floating-point (lowercase)

-0x1.91eb8p+1

%A

Hexadecimal floating-point (uppercase)

-0X1.91EB8P+1

  1. Character and String Format Specifiers
Specifier Description Example

%c

Single character

'A'

%s

String

"Hello"

  1. Pointer Format Specifier
Specifier Description Example

%p

Memory address (pointer)

0x7ffde0c8

  1. Special Format Specifiers
Specifier Description Example

%%

Prints a literal % character

%

Modifiers for Format Specifiers

Modifiers are used to specify the width or size of the data to be read or displayed.

Length Modifiers

Modifier Description Example Specifier

h

Short integer

%hd, %hu

l

Long integer

%ld, %lu, %lf

ll

Long long integer

%lld, %llu

L

Long double

%Lf

z

Size of size_t

%zu

Width and Precision

  • Width: Minimum number of characters to print.
  • Precision: Specifies the number of digits after the decimal point for floating-point values.

Syntax:

C

%[flags][width][.precision][length]specifier

Example:

C

printf("%10d", 42);      // Prints 42 with a width of 10 (right-aligned)

printf("%.2f", 3.14159); // Prints 3.14 (2 digits after decimal)

Examples of Usage

  1. Integer

C

int num = 123;

printf("Decimal: %d\n", num);

printf("Octal: %o\n", num);

printf("Hexadecimal: %x\n", num);

  1. Floating-Point

C

float pi = 3.14159;

printf("Value of pi: %.2f\n", pi); // Prints pi up to 2 decimal places

  1. Character and String

C

char ch = 'A';

char str[] = "Hello, World!";

printf("Character: %c\n", ch);

printf("String: %s\n", str);

  1. Pointer

C

int num = 42;

printf("Memory address: %p\n", &num);

Example Program

C

#include <stdio.h>

int main() {

    int num = 42;

    float pi = 3.14159;

    char ch = 'A';

    char str[] = "Hello";

    printf("Integer: %d\n", num);

    printf("Floating-point: %.2f\n", pi);

    printf("Character: %c\n", ch);

    printf("String: %s\n", str);

    printf("Pointer address: %p\n", &num);

    return 0;

}

Output:

Integer: 42

Floating-point: 3.14

Character: A

String: Hello

Pointer address: 0x7ffeec8c

Key Points

  1. Use the appropriate specifier for the variable type to ensure proper formatting.
  2. Use precision and width modifiers to control the output appearance.
  3. Always match the data type of the variable with the corresponding specifier in printf() or scanf(). Mismatched types can lead to undefined behavior.