Format Specifiers in C

Filed Under: C Programming
Format Specifiers In C

Format specifiers in C are used to accept and display data to the user. In programming terms, format specifiers help the compiler analyze the type of data being provided to the program.

Thus, it helps to find out the data type associated with the corresponding variable of the program.

Points to Remember:

There are basically three elements that rule the format specifiers:

  1. A period (.) – Separates field width and provides precision.
  2. A minus symbol (-) – Provides left alignment.
  3. A number after the ‘%’ to specify the minimum width of the string to be printed.

List of format specifiers in C

Format SpecifierDescription
%cAccepts and prints characters
%dAccepts and prints signed integer
%e or %EScientific notation of floats
%fAccepts and prints float numbers
%hiPrints the signed integer
%huPrints unsigned integer
%iAccepts and prints integer values
%l or %ld or %liLong integer values
%lfDouble values
%LfLong double values
%luUnsigned int or unsigned long
%oProvides the octal form of representation
%sAccepts and prints String values
%uAccepts and prints unsigned int
%x or %XProvides the hexadecimal form of representation
%nProvides a new line
%%This prints % character

Integer format specifier(%d or %i or %u)

Integer format specifiers accept and print integer values through the program.

Example:

#include <stdio.h> 
int main() 
{ 
	int x = 0; 
	printf("Enter the value of x:\n");
	scanf("%d", &x); 
	printf("%d\n", x);
	int z = 0;
	printf("Enter the value of z:\n");
	scanf("%i", &z); 
	printf("%i\n", z);
	return 0; 
} 

Output:

Enter the value of x:
10
10
Enter the value of z:
25
25

The “%d” format specifier takes the input number in a decimal format while the “%i” format specifier takes the input number into decimal or octal or hexadecimal format.


Character format specifier(%c)

Character format specifiers accept and print character values through the program.

Example:

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

Output:

Enter the character:
D
The entered character is:
D

Octal format specifier(%o)

Octal format specifiers help provide an octal representation of the given integer.

Example:

#include <stdio.h> 
int main() 
{ 
    int num = 69; 
    printf("The octal form of the input:\n");
    printf("%o\n", num); 
    
    return 0; 
}

Output:

The octal form of the input:
105

Float format specifier(%f or %e or %E)

Example:

#include <stdio.h>

int main()
{
    float num = 1.24578;
    
    printf("%f\n", num);
    
    printf("%0.2f\n", num);
    
    printf("%e\n", num);

    return 0;
}

In the above piece of code, %0.2f provides the precision of up to two decimal values.

Output:

1.245780
1.25
1.245780e+00

Hexadecimal format specifier(%x or %X)

Hexadecimal format specifiers provide the hexadecimal representation of the given integer.

Example:

#include <stdio.h>

int main()
{
    int num = 13;
    
    printf("%x\n", num);
    
    return 0;
}

Output:

d

String format specifier(%s)

These format specifiers accept and print character arrays or strings through the program.

Example:

#include <stdio.h>
int main()
{
    char in_str[] = "Engineering Discipline";

    printf("%s\n", in_str);

    return 0;
}

Output:

Engineering Discipline

Conclusion

Thus, in this article, we have understood the role of format specifiers in the C language.


References

C language documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages