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
%d | Integer value |
%i | Integer value |
%f | Float value |
%c | Character value |
%s | String value |
%p | For Pointers |
%ld/%li | Long value |
%lf | Double value |
%Lf | Long double value |
%llu | Unsigned long long value |
%o | For Octal Value |
%x | Hexadecimal value |
%u | Unsigned Integer Value |
%e | For 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);
}