Format Specifiers in C

What are Format Specifiers in C ?

Format Specifiers in C help the compiler in understanding the nature of the data, that is being entered by the user through scanf, or being printed by the programmer using printf. Format Specifiers also helps in printing the correct format of the data, i.e; if we want to print decimal, than we’ll use a different specifier, and if we want to print a integer then a different specifier than the previous one 

variables

List of All the Format Specifiers in C

Format SpecifierUse forSupported datatypes
        %cReads a single character.char,  unsigned char
        %dReads a decimal integer.short,  unsigned short,  int ,  long
         %iReads an integer in either decimal, octal, or hexadecimal format.short,  unsigned short,  int ,  long
         %eReads a floating -point number.float,  double
         %fReads a floating -point number.float
         %gReads a floating -point number.float,  double
         %oReads an octal number.short,  unsigned short,  int,  unsigned int,  long
         %sReads a string.char*
         %xReads a hexadecimal number.short,  unsigned short,  int,  unsigned int,  long
         %pReads a pointer.void*
        %nReceives an integer value equal to the number of characters read so far. 
        %uReads an unsigned decimal integer.unsigned int,  unsigned long
        %[ ]Scans for a set of characters. 
        %%Reads a percent sign. 
        %hiReads a signed integer(short). short
        %huReads a  un-signed integer(short).unsigned short

Use of a few Format Specifiers

Use of  %f

Run
#include  
int main()
{
    float a = 147.259;
    printf("%f\n", a);
    printf("%e\n", a);
    return 0;
}

Output

147.259003
1.472590e+02

Use of  %e

Run
#include <stdio.h> 
int main()
{
    float a = 147.259;
    printf("%f\n", a);
    printf("%e\n", a);
    return 0;
}

Output

147.259003
1.472590e+02

Use of  %x

Run
#include  <stdio.h>
int main()
{
    int a = 147.259;
    printf("%x\n", a);

    return 0;
}

Output

93

Use of  %0

Run
#include <stdio.h> 
int main()
{
    float a = 147.259;
    printf("%0\n", a);

    return 0;
}

Output

%0

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription