Output Formatting Using Printf Function in C

Here we will discuss about the output formatting function printf in the C language. What are different options available in the printf() function to format the output?

In C, there are many predefined library functions are available for input and output operations. These functions are collectively called standard input-output library functions, and all these functions are available in stdio.h header file. The “stdio.h” is an abbreviation of the standard input-output header file. So, in C language, any program which uses input or output operations must contain the statement #include<stdio.h>

The printf function in the C programming language is used for output formatting. It is used to display information required by the user and also prints the value of the variables. It formats the output, like the width of the output, the sign of the output e.t.c We will learn those formatting using printf() C.

 #include<stdio.h>
 int main()
 {
   printf("Hi ");
   printf("Hello 2025");
   return 0;
 }

Output:-

Hi Hello 2025

The below image shows the modifiers used by printf() function in C. We will discuss all modifiers one by one.

printf c 1

Conversion Code in printf C

To insert data into the stream, we use a conversion specification that contains a start token (%), a conversion code, and up to four optional modifiers. There are approximately 30 different conversion codes following % indicates the type of variable to be displayed as well as the format in which the value should be displayed.

The following table represents some regularly used conversion codes:

Conversion code Usual
variable type
Display
%c char Single character
%d or %i int Singed decimal integer
%u int Unsigned decimal integer
%o int Unsigned octal value
%x or %X int Unsigned hex value
%f float or
double
Signed floating point
%e or %E float or
double
Exponential format
%g or %G float or
double
Use %f or %e, whichever is shorter
%s Array of
char
Sequence of characters (string)
%p pointer Address stored in pointer
%% none No corresponding argument is converted,
it prints only a %
%n pointer of
int
The corresponding argument is a pointer to an integer
into which the number of characters displayed is placed.

Program to demonstrate conversion code or characters

 #include<stdio.h>
 int main()
 {
   char ch = 'a';
   int n1 = -10;
   unsigned int n2 = 200;
   int n3 = 50;
   float n4 = 5.5;
   float n5 = 3000.253215;
   char ch2[10] = "Know";

   printf("ch = %c\n",ch);
   printf("n1 = %d\n",n1);
   printf("n2 = %u\n",n2);
   printf("Octal value of n3 = %o\n",n3);
   printf("Hexadecimal value of n3 = %x\n",n3);
   printf("n4 = %f\n",n4);
   printf("n5 = %e\n",n5);
   printf("n5 = %g\n",n5);
   printf("ch2 = %s\n",ch2);

   return 0;
 }

Output:-

ch = a
n1 = -10
n2 = 200
Octal value of n3 = 62
Hexadecimal value of n3 = 32
n4 = 5.500000
n5 = 3.000253e+03
n5 = 3000.25
ch2 = Know

If the programmer uses a conversion code with the wrong type of variable, then some strange thing will be seen on the screen and the error often propagates to other items in the printf() list.

 #include<stdio.h>
 int main()
 {
   char ch[20] = "KnowProgram";
   printf("ch = %d\n",ch);
   return 0;
 }

Output on Run-1:-

ch = -1594754816

Output on Run-2:-

ch = -1487609632

Every time it gives different output. Here, ch is a variable of string but we use %d which is generally used for the int data type. So, we get garbage value.

Size Modifier in printf C

The size modifier is used to modify the data type specified by the conversion code. There are four different sizes modifiers there, those are h, l, ll, and L. The h is used with integer to indicate a short integer value. The l is used to indicate a long integer value, and the ll is used to indicate long long integer value. The L is used with floating-point numbers to indicate a long double value.

Size modifier Used for
h Short int
l Long int
ll Long long int
L Long double
 #include<stdio.h>
 int main()
 {
   short int n1 = 10;
   long int n2 = 100000;
   long long int n3 = 5060626500325;
   long double n4 = 556626.36595;
   
   printf("n1 = %hd\n",n1);
   printf("n2 = %ld\n",n2);
   printf("n3 = %lld\n",n3);
   printf("n4 = %Lf\n",n4);

   return 0;
 }

Output:-

n1 = 10
n2 = 100000
n3 = 5060626500325
n4 = 556626.365950

Note:- size modifier h is optional. If we use short data type in our program then we can use either %d or %hd. Generally, most of the time we use %d instead of %hd. You will hardly find uses of %hd. But other size modifiers l, ll and, L are compulsory, if we don’t use them then we will get reduced value or unexpected result.

 #include<stdio.h>
 int main()
 {
   short int n1 = 10;
   long double n2 = 5566265466565.36595;
   printf("n1 = %d\n",n1);
   printf("n2 = %f\n",n2);
   return 0;
 }

Output:-

n1 = 10
n2 = 0.000000

In the above program, the variable n1 is of short int data type so we can use either %d or %hd. The variable n2 is of long double data type, it is compulsory to use %Lf but we didn’t use size modifier L with %f so, we get an unexpected result.

Width Modifier in printf C

The width modifier is used to specify the minimum number of positions that the output will take. If the user does not mention any width then the output will take just enough positions required for the output data.

 #include<stdio.h>
 int main()
 {
   int num1 = 10;
   float num2 = 9.23;
   
   printf("num1 = %d\n",num1);
   printf("num1 = %5d\n",num1);
   printf("num1 = %10d\n",num1);
   
   printf("num2 = %f\n",num2);
   printf("num2 = %12f\n",num2);
   
   return 0;
 }
 Output:-
 num1 = 10
 num1 =    10
 num1 =         10
 num2 = 9.230000
 num2 =     9.230000
printf c 2

The first line of printf() width is not mentioned so 10 is displayed as it is. In the second line, we use %5d so five empty positions are created, and then from the last position digit 10 is filled, and the remaining positions are filled with spaces. In the third line %10d is used so ten empty positions are created and from the last position, value 10 is filled.

By default floating points use six positions after the decimal point, if there is no number present after the decimal point then fill it will zeros. Due to this, num2=9.23 is displayed as 9.230000; Here we don’t mention any width. In the next line, we use %12f so it uses 12 positions and filled it from left to right.

 #include<stdio.h>
 int main()
 {
   int num1 = 100;
   float num2 = 9.23;
   
   printf("1. num1 = %1d\n",num1);
   printf("2. num1 = %2d\n",num1);
   printf("3. num1 = %3d\n",num1);

   printf("\n"); // new line
   
   printf("1. num2 = %1f\n",num2);
   printf("2. num2 = %5f\n",num2);
   
   return 0;
 }

Output:-

1. num1 = 100
2. num1 = 100
3. num1 = 100

1. num2 = 9.230000
2. num2 = 9.230000

If we use a width modifier with less than the required width then it doesn’t affect output. The variable num1 requires 3 positions but we used %1d, %2, and %3d so, it doesn’t change output because these are less than or equal to the required space. Similarly, num2 = 9.23 requires 8 spaces but if we use %1f or %5f then it doesn’t change the output.

The Precision Modifier in printf C

To specifies width after the decimal point, the precision modifier is used. From the previous point (width modifier) we know that %6f says, the number is to be at least six characters wide. Similarly, %.2f specifies two characters after the decimal point, it doesn’t affect the width before the decimal point.

 #include<stdio.h>
 int main()
 {
   float num = 9.2356;
   
   printf("num = %.1f\n",num);
   printf("num = %.3f\n",num);
   printf("num = %.5f\n",num);
   
   printf("num = %3.1f\n",num);
   printf("num = %5.2f\n",num);
   printf("num = %10.3f\n",num);
   
   return 0;
 }
Output:-
num = 9.2
num = 9.236
num = 9.23560
num = 9.2
num =  9.24
num =      9.236

In the above program, %.1f print only one digit after the decimal point. Similarly, %.3f print only 3 digits after the decimal point, and %.5f print five digits after the decimal point so one zero is added to the end of the value. Notice that the original number was 9.2356 but %.3f print 9.236 (due to mathematics).

%d Print as a decimal integer
%5d Print as a decimal integer,
at least 5 characters wide
%f Print as a floating point
%5f Print as a floating point,
at least 5 characters wide
%.3f Print as a floating point,
3 characters after the decimal point
%5.3f Print as floating-point,
at least 5 wide and 3 after decimal point

Flag Modifier in C

The flag modifier allows one or more print modifications to be specified. The flag can be any one of the characters from the below table.

Flag Meaning
Left justify the display
+ Display positive or negative signs of value
Space Display space if there is no sign
0 Pad with leading zeros
# Use the alternative form of the specifier

Pad with leading zeros

 // pad with leading zeros
 #include<stdio.h>
 int main()
 {
   printf("1. Number = %05.2f\n",3.23);
   printf("2. Number = %09.3f\n",1.1);
   printf("3. Number = %010.5f",2.9);
   return 0;
 }

Output:-

1. Number = 03.23
2. Number = 00001.100
3. Number = 0002.90000

Display sign of the value

 // display sign of the value
 #include<stdio.h>
 int main()
 {
   printf("1. Number = %d\n",-5);
   printf("2. Number = %+.2f\n",-1.1);
   printf("3. Number = %+.3f\n",5.5);
   printf("4. Number = %+5.2f\n",3.23);
   return 0;
 }

Output:-

1. Number = -5
2. Number = -1.10
3. Number = +5.500
4. Number = +3.23

The specifier %-10d will display an int left-justified in a ten-character space. The specifier %+5d will display an int using the next five-character locations and will add a ‘+’ or ‘-’ sign to the value.

Left justify the display

 // left justify the display
 #include<stdio.h>
 int main()
 {
   // without flag (-)
   printf("Without - flag:\n");
   printf("1. Number = %+6.1f\n",-5.9);
   printf("2. Number = %+10.3f\n",2.2);
   
   // with flag
   printf("With - flag:\n");
   printf("1. Number = %-+6.1f\n",-5.9);
   printf("2. Number = %-+10.3f\n",2.2);
   return 0;
 }

Output:-

Without – flag:
1. Number = -5.9
2. Number = +2.200
With – flag:
1. Number = -5.9
2. Number = +2.200

 #include<stdio.h>
 int main()
 {
   // with flag 0
   printf("Number = %+09.2f\n",2.2);

   // with flag -
   printf("Number = %-+9.2f\n",2.2);

   return 0;
 }

Output:-

Number = +00002.20
Number = +2.20

printf c 3

In the first statement, a float is printed with a precision of 2 and a width of 9. Because the flag 0 is used so the extra four positions that need to be filled are occupied by zeros instead of spaces. In the second statement, the minus sign causes the value to be left-justified, spaces are added to the right instead of left, and the positive sign causes the sign of the number to be printed with the number.

Flag # With the Format Specifier

Among the flags, the only complexity is in the use of the # modifier. What this modifier does depends on the type of format specifier, that is, the conversion code it is used with.

Flag with format specifier Action
%#o Adds a leading 0 to the octal number printed
%#x or X Adds a leading 0x or 0X to the hex number printed
%#f or e Ensures that the decimal point is printed
%#g or G Display trailing zeros in g or G type conversion and ensures the decimal point is printed in floating-point number, even though it is a whole number.
 #include<stdio.h>
 int main()
 {
   printf("%#o \n",9);
   printf("%#o \n",16);
   printf("%#x \n",17);
   printf("%#x \n",100);
   printf("%#f \n",9.235);
   printf("%#g \n",9.235);
   return 0;
 }

Output:-

011
020
0x11
0x64
9.235000
9.23500

Modifiers with a String using printf in C

When a string is printed using the %s specifier, then all the characters stored in the array up to the first null will be printed. If a width specifier is used, the string will be right-justified within the space. If a precision specifier is included, only that number of characters will be printed.

 #include<stdio.h>
 int main()
 {
   printf("%s\n","program");
   printf("%3s\n","program");
   printf("%12s\n","program");
   printf("%-12s\n","program");
   printf("%12.3s\n","program");
   return 0;
 }
 Output:-
 program
 program
      program
 program     
          pro
width modifier in printf in C

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Test Your Knowledge:-

Leave a Comment

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