Format Specifiers in C

Format Specifiers in C



What is Format Specifier ?

The format specifier is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). The format specifier always starts with a '%' sign.

Here is a list of format specifiers with examples :-


Integer Format Specifier ( %d, %u ) :

Where the variable stands for both negative and positive values, that values are called signed integer value and %d is used to take input and print output of signed integer value.

Example :  
//Usage of %d

#include<stdio.>
int main()
{
    int T = 9 ;
    printf("%d\n", T);
    return 0;
}
Output :
35

%u : 
It is used to accept and print only the positive integers means, the unsigned integer.

Example :  
//usage of %u
  
#include<stdio.h>
int main()
{
    int S;
    printf("enter a negative value to convert into it's positive equivalent value :\n");
    scanf("%u", &S);
    printf("the converted equivalent positive value is : %u\n", S);
    return 0;
}

Output :
enter a negative value to convert into it's posive equivalent value :
-91
the converted equivalent positive value is : 4294967205
 

Float Format Specifiers ( %f, %.2f, %e, %E ) :

It's used to print decimal number values (float datatypes). By default, it prints upto 6 digit after the '.'.
%.2f provides the precision of up to two decimal values.

Example :  

//Usage of %f or %.2f

#include<stdio.>
int main () 
{
    float anik = 117.211;
    printf ("%f\n", anik);
    printf ("%.2f\n", anik);
    return 0;
}

Output :
117.210999
117.21

%e/ and %E :
It is also known as Exponent and is used for scientific notation of floats.

Example :  
//Usage of %e and %E

#include<stdio.>
int main()
{
    float P; 
    printf("enter a number to check the scientific notation of a number  :\n");
    scanf("%f", &P);    //taking input with float format specifier.
    printf("the converted Hexadecimal value is : %e\n", P); 
    printf("the converted Hexadecimal value is : %E\n", P); 
    return 0;
}

Output :
enter a number to convert into its Hexadecimal value :               
117.56                                                               
the converted Hexadecimal value is : 1.175600e+02                    
the converted Hexadecimal value is : 1.175600E+02
 

Character Format Specifier ( %c ) :

It is used to take input and print the output of characters.

Example :  
//Uses of %c
#include<stdio.>
int main () 
{ 
    char mizu;
    printf("Enter the character: \n");
    scanf("%c", &mizu);
    printf("The entered character is: \n");
    printf("%c\n", mizu); 
    return 0; 
}

Output :
Enter the character:                                                 
Z                                                                    
The entered character is:                                            
Z

Octal Format Specifier ( %o ) :

It is used to print Octal representation of a given number and that octal integers always start with a 0 value. Taking input with %o won't convert the number into octal value.

Example :  
//Usage of %o

#include<stdio.>
int main()
{
    int A;
    printf("enter a number to convert into its Octal value :\n");
    scanf("%d", &A);	//taking input with integer format specifier.
    printf("the converted Octal number is : %o\n", A);
    return 0;
}

Output :
enter a number to convert into its Octal value :                     
47                                                                   
the converted Octal number is : 57
 
Hexadecimal Format Specifier ( %x and %X ):
%x and %X are Hexadecimal format specifiers. It is used to print hexadecimal representation of a given number. %x will print the hexadecimal representation in lower case such as a, b, c etc.
But %X will print the hexadecimal representation in upper case such as A, B, C etc.

Example :  
//Usage of %x and %X

#include<stdio.>
int main()
{
    int P;
    printf("enter a number to convert into its Hexadecimal value :\n");
    scanf("%d", &P);    //taking input with integer format specifier.
    printf("the converted Hexadecimal value is : %x\n", P); //will print in lower case.
    printf("the converted Hexadecimal value is : %X\n", P); //will pring in upper case.
    return 0;
}

Output :
enter a number to convert into its Hexadecimal value :               
14                                                                   
the converted Hexadecimal value is : e                               
the converted Hexadecimal value is : E

Pointer Format Specifier ( %p ) :

It's used to print the memory address of a pointer or a variable in hexadecimal form. As it will print the address so taking input with "%d" will not work because "%d" will try to format the address into a number.

Example :  
//Usage of %p

#include<stdio.>
int main()
{
    float P; 
    printf("enter a number to print the memory of a variable or pointer  :\n");
    scanf("%f", &P);;
    printf("The Memory Address of a: %p\n",(void*)&P);
    return 0;
}

Output :
enter a number to print the memory of a variable or pointer  :       
117.56                                                               
The Memory Address of a: 0x7ffcbc65c11c
 

String Format Specifier ( %s ) :

It's used to take the input and print the output of strings.

Example :  
//Usage of %s

#include<stdio.h>
#include <string.h>
int main()
{
    char anime[50];
    printf("Input the string you wish to print : \n");
    scanf("%s",anime);  //taking input with string format specifier.
    printf("'User Given' String is : %s\n", anime);  
  	return 0;
}

Output :
Input the string you wish to print :                                 
Eruditors                                                            
'User Given' String is : Eruditors
 
%g : 
It is used to print decimal floating-point values and uses the fixed precision. the value after the decimal in input would be exactly the same as the value in the output.
  
%ld or %l or %li :
It's used to print the long-signed integer value.
 

Points to be noted :

1. A period (.) :
Separates the field width and provides precision.
2. A minus symbol (-) :
It provides the left alignment.

~ A number after the '%' to specify the minimum width to be printed. If the characters are less than the size of width, then the remaining space is filled with space. And if it is greater than it printed as it is without truncation.