Follow The New Tutorial updates on WhatsApp, Telegram or YouTube.

C Language Format Specifier

The Format specifier is a string used in the formatted input and output functions. Some examples are %c, %d, %f, etc.

Format specifiers define the type of data to be printed on standard output.

Using “Format Specifier” the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function. 

Format Specifier determines the format of the input and output. It always starts with a ‘%’ character.

Most used format specifiers

Format SpecifierDescription
%dInteger Format Specifier
%fFloat Format Specifier
%cCharacter Format Specifier
%sString Format Specifier
%uUnsigned Integer Format Specifier
%ldLong Int Format Specifier
Most used format specifiers

List of format specifiers

Format SpecifierType
%cCharacter
%dSigned integer
%e or %EScientific notation of floats
%fFloat values
%g or %GSimilar as %e or %E
%hiSigned integer (short)
%huUnsigned Integer (short)
%iUnsigned integer
%l or %ld or %liLong
%lfDouble
%LfLong double
%luUnsigned int or unsigned long
%lli or %lldLong long
%lluUnsigned long long
%oOctal representation
%pPointer
%sString
%uUnsigned int
%x or %XHexadecimal representation
%nPrints nothing
%%Prints % character
C Language Format Specifier

In format specifier a minus symbol (-) sign tells left alignment and a number after % specifies the minimum field width. If string is less than the width, it will be filled with spaces. A period (.) is used to separate field width and precision.

Example

#include <stdio.h> 
  
main() { 
   char ch = 'B'; 
   printf("%c\n", ch);  //printing character data
    
   //print decimal or integer data with d and i
   int x = 45, y = 90; 
   printf("%d\n", x); 
   printf("%i\n", y); 
   float f = 12.67; 
   printf("%f\n", f); //print float value 
   printf("%e\n", f); //print in scientific notation
    
   int a = 67; 
   printf("%o\n", a); //print in octal format
    
   printf("%x\n", a); //print in hex format
    
   char str[] = "Hello World"; 
   printf("%s\n", str); 
    
   printf("%20s\n", str); //shift to the right 20 characters including the string
   printf("%-20s\n", str); //left align
   printf("%20.5s\n", str); //shift to the right 20 characters including the string, and print string up to 5 character 
   printf("%-20.5s\n", str); //left align and print string up to 5 character 
}
C Language Format Specifier Example – Output