Written by - Vanshika Yadav

Format Specifier in C


The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output.
The format string always starts with a '%' character.

Format specifierType
%d or %iIt's used to print the integer value(positive n negative both)
%oIt’s used to print the octal value
%xIt’s is used to print the hexadecimal integer value and it always starts with a 0x value.in this case, alphabetical characters are printed in small letters like a,b,c, etc.
%XIt’s is used to print the hexadecimal integer value. but %X prints the alphabetical characters in uppercase like A, B, C, etc.
%fIt’s is used for print the decimal floating-point values.
%sIt’s used to print the strings.
%cIt’s used to print the unsigned character.
%ldIt is used to print the long-signed integer value.

Let's see with the code example

%d

#include<stdio.h>
int main(){
    int a =10;
    printf("Value of a is:%d\n",a ); //Value of a is 10
    return 0;
}

%o

#include<stdio.h>
int main(){
    int a =66;
    printf("%o\n", a); //102
    printf("%d\n", a); //66
    return 0;
}

%X or %x

#include<stdio.h>
int main(){
    int a =0xfc;
    printf("%x\n", a); //fc
    printf("%X\n", a); //FC
    printf("%d\n", a); //252
    return 0;
}

%f

#include<stdio.h>
int main(){
    float a = 3.14;
    printf("%f", a); //3.140000
    return 0;
}

%E

#include<stdio.h>
int main(){
    float a=3;
    printf("%E", a); //3.000000E+00
    return 0;
}

%c

#include<stdio.h>
int main(){
    char a='C';
    printf("%c", a); //C
    return 0;
}

%s

#include<stdio.h>
int main(){
    printf("%s", "prosequence.tech"); //prosequence.tech
    return 0;
}