Format specifiers in C

The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.

The format specifier in printf() and scanf() are mostly the same but there is some difference which we will see.

printf(char *format, arg1, arg2, …)

This function prints the character on standard output and returns the number of character printed the format is a string starting with % and ends with conversion character (like c, i, f, d, etc.).
Between both, there can be elements governing the printing format. Below is its description



  1. A minus(-) sign tells left alignment.
  2. A number after % specifies the minimum field width to be printed if the characters are less than the size of width the remaining space is filled with space and if it is greater than it printed as it is without truncation.
  3. A period( . ) symbol seperate field width with the precision.

Precision tells the maximum number of digits in integer, characters in string and number of digits after decimal part in floating value.

Lets see these..
Character format specifier : %c

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    char ch = 'A';
    printf("%c\n", ch);
    return 0;
}

chevron_right


Output:

A

Integer format specifier : %d, %i

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int x = 45, y = 90;
    printf("%d\n", x);
    printf("%i\n", x);
    return 0;
}

chevron_right


Output:

45
45

Double format specifier : %f, %e or %E

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

12.670000
1.267000e+01

Unsigned Octal number for integer : %o

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

103

Unsigned Hexadecimal for integer : %x, %X


filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int a = 15;
    printf("%x\n", a);
    return 0;
}

chevron_right


Output:

f

String printing : %s

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    char a[] = "geeksforgeeks";
    printf("%s\n", a);
    return 0;
}

chevron_right


Output:

geeksforgeeks

More formatting

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    char str[] = "geeksforgeeks";
    printf("%20s\n", str);
    printf("%-20s\n", str);
    printf("%20.5s\n", str);
    printf("%-20.5s\n", str);
    return 0;
}

chevron_right


Output:

       geeksforgeeks
geeksforgeeks       
               geeks
geeks

scanf(char *format, arg1, arg2, …)

This function take input using standard input (keyboard) and store it in variable accordingly. It returns the number of items successfully read. Formal parameter arg1, agr2, .. must be a pointer

decimal integer : %d

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int a = 0;
    scanf("%d", &a); // input is 45
    printf("%d\n", a);
    return 0;
}

chevron_right


Integer may be octal or in hexadecimal : %i

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    int a = 0;
    scanf("%i", &a); // input is 017 (octal of 15 )
    printf("%d\n", a);
    scanf("%i", &a); // input is 0xf (hexadecimal of 15 )
    printf("%d\n", a);
    return 0;
}

chevron_right


Floating data type : %f, %e(double), %lf(long double)

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    float a = 0.0;
    scanf("%f", &a); // input is 45.65
    printf("%f\n", a);
    return 0;
}

chevron_right


Output:

0.000000

String input : %s

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    char str[20];
    scanf("%s", str); // input is geeksforgeeks
    printf("%s\n", str);
    return 0;
}

chevron_right


Output:

P@

Character input : %c

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <stdio.h>
int main()
{
    char ch;
    scanf("%c", &ch); // input is A
    printf("%c\n", ch);
    return 0;
}

chevron_right


Many other format specifiers are also there
1.%u for an unsigned integer.
2.%lld for long long int.
3.%o octal integer without leading zero
4.%x hexadecimal integer without 0x before the number.



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mandarautade, Code_Mech



Article Tags :
Practice Tags :


9


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.