Tute

Format Specifiers in C?

Format_Specifiers_in_C

C Format Specifier : -

The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a % character.

All 19 List Of Format Specifiers In C With Examples- Updated

Format specifiers in C are used for input and output purposes. Using format specifier the compiler can understand that what type of data is in input and output operation. For example, what type of data is storing in a variable using scanf or printing using printf decide by format specifiers ?

There are some elements that affect the format specifier. Below, I have mentioned elements that affect the format specifier.

1. A minus symbol (-) sign tells left alignment

2. A number after % specifies the minimum field width. If the string is less than the width, it will be  filled with spaces

3. A period (.) is used to separate field width and precision.

List of format specifiers which generally used in programming:

 

Format Specifier Type
%c Character
%d Signed integer
%e or %E Scientific notation of floats
%f Float values
%g or %G Similar as %e or %E
%hi Signed integer (short)
%hu Unsigned Integer (short)
%i integer
%l or %ld or %li Long
%lf Double
%Lf Long double
%lu Unsigned int or unsigned long
%lli or %lld Long long
%llu Unsigned long long
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character

 

Quiz01 and format specifier

Difference between %d and %i format specifier in C

When you are printing using the printf function, there is no specific difference between the %i and %d format specifiers. But both format specifiers behave differently with scanf function.

The %d format specifier takes the integer number as decimal but the %i format specifier takes the integer number as decimal, hexadecimal or octal type. it means the %i automatically identified the base of the input integer number.

Note: You must put ‘0x’ for hexadecimal number and ‘0’ for octal number while entering the input number.

#include <stdio.h>
 
int main()
{
int data1, data2, data3;
 
printf("Enter value in decimal format:");
scanf("%d",&data1);
printf("data1 = %i\n\n", data1);
 
printf("Enter value in hexadecimal format:");
scanf("%i",&data2);
printf("data2 = %i\n\n", data2);
 
printf("Enter value in octal format:");
scanf("%i",&data3);
printf("data2 = %i\n\n", data3);
 
return 0;
}

Output:

difference between %i and %d in c

Differences between %f, %e and %g format specifiers in C language

Let see an example to understand the difference between %f, %e, and %g format specifier.

#include <stdio.h>
 
int main(void)
{
double data1 = 123456.0;
 
printf("%e\n", data1);
printf("%f\n", data1);
printf("%g\n", data1);
printf("\n");
 
double data2 = 1234567.0;
 
printf("%e\n", data2);
printf("%f\n", data2);
printf("%g\n", data2);
return 0;
}

Output:

1.234560e+005
123456.000000
123456

1.234567e+006
1234567.000000
1.23457e+006

Explanation:

When using the G ( or g) conversion specifier, the double argument representing a floating-point number is converted in style f or e (or in style F or E ), depending on the value converted and the precision.

Related Topics