**C language**learned by

**suffering**

**C language**learned by

**suffering**

### Numeric justification

##### Integer digit alignment

Although we have used the printf function frequently in the past, there is one function we have not yet explained.

The printf function

The following program is an example of displaying the data without specifying the number of digits, as before.

The result of executing this program is as follows

As you can see from the results, the numbers are displayed as they are.

The positional relationship with other numbers has become misaligned.

When displaying a number of numbers with more digits, it becomes very difficult to read.

To make the resulting display easier to read, put a space at the beginning of the number and align it with the end of the number.

To print a number justified by the end of the number in the printf function, do the following

If a number is placed between the output conversion specifiers, it will be displayed with a space to the number of digits.

The following program is an example of specifying five digits to make it easier to read.

The result of executing this program is as follows

As you can see in the results, spaces are inserted according to the number of digits to make it easier to read.

If the number of numerical digits is larger than the specified number of digits, the number of digits is adjusted to the number of numerical digits.

Therefore, if the maximum expected number of digits is specified, the digits will always be aligned.

Also, when displaying a negative value, the - sign is treated as a single digit, so

If there is a possibility of displaying negative values, specify one digit larger.

The printf function

**allows you to specify the number of digits of**characters or numbers to be printed.The following program is an example of displaying the data without specifying the number of digits, as before.

source code

```
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("A is %d. \n", a);
printf("B is %d. \n", b);
printf("C is %d. \n", c);
return 0;
}
```

The result of executing this program is as follows

Execution Result

A is 10000.

B is 500.

C is 3.

B is 500.

C is 3.

As you can see from the results, the numbers are displayed as they are.

The positional relationship with other numbers has become misaligned.

When displaying a number of numbers with more digits, it becomes very difficult to read.

To make the resulting display easier to read, put a space at the beginning of the number and align it with the end of the number.

To print a number justified by the end of the number in the printf function, do the following

Aligned at the end of a number

Number of % digits d

If a number is placed between the output conversion specifiers, it will be displayed with a space to the number of digits.

The following program is an example of specifying five digits to make it easier to read.

source code

```
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("A is %5d. ³", a);
printf("B is %5d. ³", b);
printf("C is %5d. ³", c);
return 0;
}
```

The result of executing this program is as follows

Execution Result

A is 10000.

B is 500.

C is 3.

B is 500.

C is 3.

As you can see in the results, spaces are inserted according to the number of digits to make it easier to read.

If the number of numerical digits is larger than the specified number of digits, the number of digits is adjusted to the number of numerical digits.

Therefore, if the maximum expected number of digits is specified, the digits will always be aligned.

Also, when displaying a negative value, the - sign is treated as a single digit, so

If there is a possibility of displaying negative values, specify one digit larger.

##### Computerized display

The printf function can also print using zeros, which are commonly seen in computers.

Prefixing a digit with a 0 causes it to be displayed with a 0 in place of a space.

The following program is an example of a program modified to include leading zeros.

The result of executing this program is as follows

Somehow, I think the display looks more computer-like.

Prefixing a digit with a 0 causes it to be displayed with a 0 in place of a space.

The following program is an example of a program modified to include leading zeros.

source code

```
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("A is %05d. \n", a);
printf("B is %05d. \n", b);
printf("C is %05d. \n", c);
return 0;
}
```

The result of executing this program is as follows

Execution Result

A is 10000.

B is 00500.

C is 00003.

B is 00500.

C is 00003.

Somehow, I think the display looks more computer-like.

##### Real number digit alignment

In the display of real numbers, the number of decimal places can be specified along with the overall number of digits.

It is important to note that the

For example, %6.2f is interpreted as six digits: three digits for the integer portion, one digit for the decimal point, and two digits for the fractional portion.

The following program is an example of displaying a real number with digits specified.

The result of executing this program is as follows

The bottom line is added to make it easier to see the number of digits.

You can see that the entire value is displayed with 6 digits, the decimal point with 1 digit, and the fractional portion with 2 digits.

Note that if the number of decimal places is set to 0, no decimal point will be displayed.

See Words and Symbols Output Conversion Specifiers for more information.

Real number justification

% whole digits . Decimal digits f

It is important to note that the

**overall digits include both decimal digits and decimal points**.For example, %6.2f is interpreted as six digits: three digits for the integer portion, one digit for the decimal point, and two digits for the fractional portion.

The following program is an example of displaying a real number with digits specified.

Source Code

```
#include <stdio.h>
int main(void)
{
double pi = 3.14159;
printf("%6.2f¥n",pi);
printf("123456¥n");
return 0;
}
```

The result of executing this program is as follows

Execution Result

3.14

123456

123456

The bottom line is added to make it easier to see the number of digits.

You can see that the entire value is displayed with 6 digits, the decimal point with 1 digit, and the fractional portion with 2 digits.

Note that if the number of decimal places is set to 0, no decimal point will be displayed.

Various other specifications can be made.

See Words and Symbols Output Conversion Specifiers for more information.

#### About this Site

The C language (bitter C), which is learned by suffering, is

This is the definitive C language introductory site.

It systematically explains the basic functions of the C language and

It is as complete as or better than any book on the market.