C Tutorial C Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C - Format Specifiers



The format specifiers helps the compiler to understand the data types and formats in the input or output stream. It is used with scanf() function while taking input and printf() function while printing the output. Here is the list of format specifiers available C language:

Format Specifier

SpecifierDescriptionSupported Data Types
%cCharacterchar
unsigned char
%hiSigned Integershort
%huUnsigned Integerunsigned short
%i or %dSigned Integershort
unsigned short
int
long
%uUnsigned Integerunsigned int
unsigned long
%f or %FFloating pointfloat
%l or %li or %ldSigned Integerlong
%luUnsigned Integerunsigned int
unsigned long
%lfFloating pointdouble
%LfFloating pointlong double
%sString of characterschar *
%e or %EScientific notation for float valuesfloat
double
%g or %GSimilar as %e or %Efloat
double
%lli or %lldSigned Integerlong long
%lluUnsigned Integerunsigned long long
%pAddress of pointervoid *
%oHexadecimal representation of Integershort
unsigned short
int
unsigned int
long
%x or %XHexadecimal representation of Unsigned Integershort
unsigned short
int
unsigned int
long
%nPrints nothing
%% Prints % character

Example:

The example below shows how to use different format specifier in C.

#include <stdio.h>
int factorial(int x); 

int main (){
  int i = 10;
  printf("%i\n", i);
  printf("%d\n", i);

  float j = 10.5;
  printf("%f\n", j);
  printf("%.3f\n", j); //specify precision
  printf("%e\n", j);   //prints in scientific notation

  int k = 250;
  printf("%o\n", k);   //prints in octal format
  printf("%x\n", k);   //prints in hexadecimal format

  int* p = &k;
  printf("%p\n", p);   //prints pointer
  
  return 0;
}

The output of the above code will be:

10
10
10.500000
10.500
1.050000e+01
372
fa
0x7ffe8fffaa84

5