# Formatting the print using printf in C

May 30, 2017 53247

This post is about the formatted printing in C. The printf function of C can do a lot more than just printing the values of variables. We can also format our printing with the printf function. We will first see some of the format specifiers and special characters and then start the examples of formatted printing. So, let’s have start from the beginning and see how we can format our printing with the printf function.

## Format Specifiers

Let’s have a look at some of the important format specifiers.

 %d a decimal (base 10) number %c a single character %s string of characters %f a floating-point number %o a number in octal (base 8) %u an unsigned decimal (base 10) number %x a number in hexadecimal (base 16)

## Special Characters

Some of the important special characters are:

 \ escape the next character \n prints a new line \t tab \v vertical tab \b backspace \r carriage return
#include <stdio.h>

int main()
{
printf("Hello\tat BlogsDope\n\vLet's print slash \\.\nUse \\b to move back\bprint now.\nCool! Let's move back\rPrint again.\n");
return 0;
}


Output

Hello    at BlogsDope

Let's print slash \. Use \b to move bacprint now. Print again.move back

Here, we first printed a tab space after “Hello” with ‘\t’ and then a vertical tab using ‘\v’ after “BlogsDope\n”. We use ‘\\’ to print a slash (\).

‘\b’ moved one character backward and thus reached to ‘k’ of ‘back’ and started printing from there and hence printed “bacprint now”. ‘\r’ started the printing from the beginning of the line and thus reached to the ‘C’ of “Cool! move back” and started printing “Print again”. So, it printed ‘P’, ‘r’, ‘i’, ‘n’, ‘t’, ‘ ’, ‘a’, ‘g’, ‘a’,  ‘i’ and ‘n’ in the place of ‘C’, ‘o’, ‘o’, ‘l’, ‘!’, ‘ ’, ‘L’, ‘e’, ‘t’, ‘’’ and ‘s’ respectively and then started printing the rest of the characters (“move back”).

## Precision of floating-point numbers

We can control the number of the decimal digits to be printed in C. We use  “%.n” before “f” in the %f to print a floating number with a precision on ‘n’. Let’s see an example to get it.

#include <stdio.h>

int main()
{
float a = 123.123456;
printf("%f\n",a);
printf("%.1f\n",a);
printf("%.2f\n",a);
return 0;
}


Output

123.123459 123.1 123.12

## Controlling the width

We can set the minimum number of width to be printed. An example will make this clear.

#include <stdio.h>

int main()
{
printf("This is %15s\n","BlogsDope");
return 0;
}


Output:

This is       BlogsDope

Here, we have reserved 15 spaces for the printing of “BlogsDope” and the “BlogsDope” got printed in 9 of these spaces and thus left the remaining spaces black.

Let’s see some more examples.

#include <stdio.h>

int main()
{
float a = 12.123;
printf("%7.3f\n",a);
printf("%2.1f\n",a);
return 0;
}


 12.123 12.1

Here, you can see that we have controlled both the precision of the decimal and the minimum space for the printing together.

Till now, we have controlled the width of the value to be printed and the whitespaces got printed before the value if the value is smaller than the specified number. We can also print these whitespaces after the value by just using minus sign (-) before specifying the width. Let’s see an example.

#include <stdio.h>

int main()
{
printf("This is %-15s.\n","BlogsDope");
return 0;
}


Output

This is BlogsDope      .

You can see that this time whitespaces got printed after “BlogsDope”.

We can also control the maximum number of characters to be printed in a string as we control the precision of the floating number. For example, printf(“%.5s”,BlogsDope) will just print the first 5 characters of “BlogsDope”. Let’s see an example.

#include <stdio.h>

int main()
{
printf("This is %.5s\n","BlogsDope");
return 0;
}


Output

This is Blogs

Here, “.%5s” printed only the first 5 characters of “BlogsDope”.

Liked the post?
Developer and founder of CodesDope.
Editor's Picks
8 COMMENTS

Please login to view or add comment(s).