Format Specifiers in C With Examples

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 Specifier Description
%c Accepts and prints characters
%d Accepts and prints signed integer
%e or %E Scientific notation of floats
%f Accepts and prints float numbers
%hi Prints the signed integer
%hu Prints unsigned integer
%i Accepts and prints integer values
%l or %ld or %li Long integer values
%lf Double values
%Lf Long double values
%lu Unsigned int or unsigned long
%o Provides the octal form of representation
%s Accepts and prints String values
%u Accepts and prints unsigned int
%x or %X Provides the hexadecimal form of representation
%n Provides 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("%dn", x);
	int z = 0;
	printf("Enter the value of z:n");
	scanf("%i", &z);
	printf("%in", 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("%cn", 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("%on", 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("%fn", num);
    printf("%0.2fn", num);
    printf("%en", 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("%xn", 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("%sn", 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

By admin

Leave a Reply

%d bloggers like this: