• Home
  • 1.16 Format Specifiers

1.16 Format Specifiers

View Categories

1.16 Format Specifiers

Format specifiers define how printf() and scanf() interpret the corresponding arguments. The specifier must match the type of the value being printed or the object receiving input.

#include <stdio.h>

int main()
{
    int number = 25;
    float temperature = 36.5f;
    double value = 3.14159;
    char letter = 'A';

    printf("Integer   : %d\n", number);
    printf("Float     : %f\n", temperature);
    printf("Double    : %f\n", value);
    printf("Character : %c\n", letter);

    return 0;
}

Example Output #

Integer   : 25
Float     : 36.500000
Double    : 3.141590
Character : A

Explanation #

The format string contains conversion specifications such as:

%d
%f
%c

Each specification determines how the corresponding argument is formatted.

For example:

printf("%d\n", number);

uses %d for an int.

printf("%c\n", letter);

uses %c for a character.

Common printf() Format Specifiers #

Specifier Expected argument type Purpose
%d int Signed decimal integer
%i int Signed decimal integer
%u unsigned int Unsigned decimal integer
%o unsigned int Octal integer
%x unsigned int Lowercase hexadecimal
%X unsigned int Uppercase hexadecimal
%c int Character
%s Pointer to char String
%f double Floating-point value
%e double Scientific notation
%g double Shorter of %f or %e representation
%p void * Pointer address
%zu size_t size_t value

For printf(), float arguments are promoted to double, so %f is used for both float and double values.

For scanf(), the corresponding format requirements are different. For example, %f expects a float *, while %lf expects a double *. These differences are covered in the dedicated scanf() lessons.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *