# Capabilities of Format Specifiers

Format specifiers give programmers various options to display content based on their needs. In this tutorial, we will learn different ways to use format specifiers for displaying content.

Format specifiers give programmers various options to display content based on their needs. In this tutorial, we will learn different ways to use format specifiers for displaying content.

Please keep in mind that the terms “format specifier” and “conversion specification” are used interchangeably and have the same meaning.

The general format for a format specifier is as follows:

`%w.pX or %-w.pX`

Here’s what each part means:

**w**: This represents the minimum field width, indicating the minimum number of characters to allocate for the data.**p**: This denotes the precision, specifying the number of digits after the decimal point (for floating-point values) or the maximum number of characters to display (for strings).**X**: This refers to the type of data to be displayed, such as decimal numbers, floating-point numbers, and so on.

The minimum field width (w) specifies the minimum number of digits to be displayed. If the number of digits is greater than w, the number will be printed as it is. However, if the number of digits is less than w, leading white spaces will be automatically added before the number to meet the minimum width requirement. This means that the number will be aligned to the left.

For example, when using `%5d`

in the printf function, it indicates that a minimum of 5 digits should be displayed on the console. If the number to be printed is 12345, it will be displayed as it is. However, if the number to be printed is 45, three white spaces will be added before 45 to fulfill the minimum field width.

You can run the following program (save the program as **field-width.c**) to observe the impact of the minimum field width:

```
#include <stdio.h>
int main()
{
int x = 12345;
int y = 45;
printf("%5d\n", x);
printf("%5d", y);
return 0;
}
```

**Output:**

```
12345
45
Process returned 0 (0x0) execution time : 0.824 s
Press any key to continue.
```

You will notice that the number 45 is aligned to the left. Three white spaces are added before the number to meet the minimum field width requirement.

Additionally, please note that the first printf function includes `\n`

at the end of the string, which adds a newline character after the string is printed. This is why the number 45 is printed on the next line.

**Quick Exercise:**Rewrite the above program

`\n`

in the first printf function, run the code and see what happened.When we place a minus sign in front of the minimum field width (w = 5), the number will be aligned to the right instead of the left.

To demonstrate this, we will add another line to the existing program, just before the **return 0** statement, as follows:

```
#include <stdio.h>
int main()
{
int x = 12345;
int y = 45;
printf("%5d\n", x);
printf("%5d\n", y);
printf("%-5d", y);
return 0;
}
```

**Output:**

```
12345
45
45
Process returned 0 (0x0) execution time : 0.562 s
Press any key to continue.
```

**Note **how the last value is presented. The last value is right justified because of the minus sign added in front of the minimum field width.

The meaning of precision varies depending on the type of format specifier (X) used. Here are the basic format specifiers for numbers and their associated meanings of precision:

**d** in %d represents decimal (or integer) values. The precision (p) in this case specifies the minimum number of digits to be displayed. If the number of digits is less than p, zeros will be added in front of the number. Otherwise, the number will be printed as it is.

To observe this behavior, you can save the following code as “field-width2.c” and run it:

```
#include <stdio.h>
int main()
{
int x = 12345;
int y = 45;
printf("%5d\n", x);
printf("%.3d\n", y);
return 0;
}
```

In the second printf function, the value of p is 3. Here, p is always followed by a dot, and it is not mandatory to specify the value of w (minimum field width). In this case, it means that a minimum of 3 digits must be printed. Since the number 45 has only two digits, a zero will be added before the number, resulting in the output as shown below:

```
12345
045
Process returned 0 (0x0) execution time : 1.394 s
Press any key to continue.
```

This format specifier represents the exponential format and is used to display data in scientific notation. The precision (p) in this case determines the number of digits to be displayed after the decimal point.

Consider the following program:

```
#include <stdio.h>
int main()
{
float x = 896.234;
printf("%.3e\n", x);
return 0;
}
```

**Output:**

```
8.962e+002
Process returned 0 (0x0) execution time : 1.880 s
Press any key to continue.
```

Also, you will observe that only three digits are printed after the decimal point because the value of p is 3.

This format specifier represents the floating-point format and is used to display decimal numbers in fixed decimal format. The precision (p) in this case specifies the number of digits to be displayed after the decimal point, similar to the exponential format.

Consider the following code:

```
#include <stdio.h>
int main()
{
float x = 896.234;
printf("%.2f\n", x);
return 0;
}
```

**Output:**

```
896.23
Process returned 0 (0x0) execution time : 0.824 s
Press any key to continue.
```

Only two digits are displayed after the decimal point because the value of p is 2.

This format specifier is used to display floating-point numbers either in floating-point format (%f) or in exponential format (%e), depending on the size of the number. If the number is too large or too small, `%g`

will display it in exponential format. The precision (p) in this case represents the maximum number of significant digits to be displayed for a more precise representation of the number.

Consider the following code:

```
#include <stdio.h>
int main()
{
float x = 896.234;
printf("%.1g\n", x);
return 0;
}
```

**Output:**

```
9e+002
Process returned 0 (0x0) execution time : 1.950 s
Press any key to continue.
```

**Can you guess why we got such a weird-looking output? **

The reason is that the format specifier allows printf to display only one significant digit, which can represent the given number, 896.234, as precisely as possible. “9e+002” is equivalent to 900, which is very close to 896.234, and that’s why it is the output. Note that the exponent part (e+002) is not significant, but it is added to represent the number more precisely.

There are different types of format specifiers, and we will explore them when discussing the data types in C.

Leave a comment

## Leave a comment