Format Specifiers in C

Format specifiers in C: It can be defined as the operators which are used in the association of printf function for printing the data that is referred by an object or a variable. And if you want you can retrieve the data that are stored in the variables and can print them on the console screen by implementing these format specifiers in a printf() function.

C Format Specifiers List

Here, we are displaying the list of format specifiers in C language.

Format Specifier Description
%d Integer format specifier
%f Float format specifier
%c Character format specifier
%s String format specifier
%u Unsigned integer format specifier
%ld Long Int format specifier

Integer format Specifier (%d)

The %d format specifier is implemented for representing integer values. It is used with printf() functions for printing the integer value stored in the variable.



Syntax: printf(“%d”,<variable name>); 

Example

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

Output: A

Float Format specifier (%f)

It is implemented within the printf() function for printing the fractional or floating value stored in the variable.

Syntax: printf(“%f”, <variable name>); 

Example

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

Output: 0.000000

Character Format Specifier (%c)

It is used for printing the character stored in a variable.

Syntax: printf(“%c”,<variable name>); 

Example

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

Output: A

String format Specifier (%s)

It is used for representing string stored in a character array variable.

Syntax: printf(“%s”,<variable name>); 

Example

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

Output: freshersnow

Unsigned Integer Format Specifier (%u)

It is used for fetching values from the address of a variable having an unsigned decimal integer stored in the memory.

Syntax: printf(“%u”,<variable name>); 

Example: Unsigned Hexadecimal for integer.

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

Output: f

Long Int Format Specifier

It is implemented with printf function for printing the long integer value stored in the variables.

Syntax: printf(“%ld”,<variable name>); 

Example

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