Format specifier in C

Format specifiers in C, are used for input and output data, according to data types. Format specifiers starts with % symbol and a character without space and must be placed inside double quotes. It is also called format codes or format strings.

Format specifier list in c

%dInteger value
%iInteger value
%fFloat value
%cCharacter value
%sString value
%pFor Pointers
%ld/%liLong value
%lfDouble value
%LfLong double value
%lluUnsigned long long value
%oFor Octal Value
%xHexadecimal value
%uUnsigned Integer Value
%eFor scientific notation

How to use format specifiers

Format specifiers are used to get input from users and also print output to the screen.

Format specifiers for input

Syntax

scanf("format_specfiers", variable_name);

Example

#include<stdio.h>

void main()
{
   int a;
   float f;
   char c;
   // Input Integer value
   scanf("%d", &a);
   // Input Float value
   scanf("%f", &f);
   // Input char value
   scanf("%c", &c);

}

Format specifiers for output/print

Syntax

printf("format_specfiers", variable_name);

Example

#include<stdio.h>

void main()
{
   int a = 20;
   float f = 3.14;
   char c = 'a';
   // Print Integer value
   printf("%d", a);
   // Print Float value
   printf("%f", f);
   // Print char value
   printf("%c", c);
}