Number formatting
Integer alignment
We've used the printf function frequently until now, but there's still functionality that hasn't been explained.
The `printf` function allows you to specify the number of digits to be displayed for characters or numbers.
The following program displays the output as is, without specifying the number of digits as before.
The output of this program is as follows.
As you can see from the results, the numbers are displayed as they are.
It's misaligned with the other numbers.
Displaying several numbers with many digits becomes very difficult to read.
To improve readability of the results, please pad the beginning of numbers with spaces and align them at the end.
To right-align numbers when using the printf function, you can do the following.
Placing a number between the output conversion specifier and the variable will pad the output with spaces to match that number of digits.
The following program is an example formatted with 5 digits for readability.
The output of this program is as follows.
As you can see from the result, spaces have been added to improve readability based on the number of digits.
If the number of digits in the number exceeds the specified number of digits, it will be adjusted to match the number of digits in the number.
Therefore, if you specify the expected maximum number of digits, the numbers will be aligned and displayed accordingly.
Also, when displaying negative values, the minus sign is treated as a single digit.
If there's a possibility of displaying negative values, specify one digit larger.
The `printf` function allows you to specify the number of digits to be displayed for characters or numbers.
The following program displays the output as is, without specifying the number of digits as before.
Source code
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("Ais %d.\n", a);
printf("Bis %d.\n", b);
printf("Cis %d.\n", c);
return 0;
}
The output of this program is as follows.
Results
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.
It's misaligned with the other numbers.
Displaying several numbers with many digits becomes very difficult to read.
To improve readability of the results, please pad the beginning of numbers with spaces and align them at the end.
To right-align numbers when using the printf function, you can do the following.
Align numbers by their trailing ends.
%d digits
Placing a number between the output conversion specifier and the variable will pad the output with spaces to match that number of digits.
The following program is an example formatted with 5 digits for readability.
Source code
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("Ais %5d.¥n", a);
printf("Bis %5d.¥n", b);
printf("Cis %5d.¥n", c);
return 0;
}
The output of this program is as follows.
Results
A is 10000.
B is 500.
C is 3.
B is 500.
C is 3.
As you can see from the result, spaces have been added to improve readability based on the number of digits.
If the number of digits in the number exceeds the specified number of digits, it will be adjusted to match the number of digits in the number.
Therefore, if you specify the expected maximum number of digits, the numbers will be aligned and displayed accordingly.
Also, when displaying negative values, the minus sign is treated as a single digit.
If there's a possibility of displaying negative values, specify one digit larger.
Computational representation
The printf function allows for the display of numbers padded with zeros, a common feature in computing.
Leading zeros will cause numbers to be displayed with leading zeros instead of spaces.
The following program is an example modified to pad with leading zeros.
The output of this program is as follows.
I feel like the display has a sort of computer-like quality.
Leading zeros will cause numbers to be displayed with leading zeros instead of spaces.
The following program is an example modified to pad with leading zeros.
Source code
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("Ais %05d.\n", a);
printf("Bis %05d.\n", b);
printf("Cis %05d.\n", c);
return 0;
}
The output of this program is as follows.
Results
A is 10000.
B is 00500.
C is 00003.
B is 00500.
C is 00003.
I feel like the display has a sort of computer-like quality.
Decimal alignment of real numbers
For displaying real numbers, you can specify both the total number of digits and the number of digits after the decimal point.
In the previous section, we explained how to declare variables using the following notation.
For example, in %6.2f, the integer part is interpreted as having 3 digits, the decimal point takes 1 digit, and the fractional part has 2 digits, resulting in a total of 6 digits.
The following program demonstrates how to format real numbers.
The output of this program is as follows.
The following line is included for readability of the digits.
It appears the display is six digits total, with one digit for the decimal point and two digits for the fractional part.
If you set the number of decimal places to 0, the decimal point will not be displayed.
You can also specify other options.
For details, refer to the output conversion specifiers in Words and Symbols.
Numeric alignment (for real numbers)
%total_digits.decimal_digitsf
In the previous section, we explained how to declare variables using the following notation.
For example, in %6.2f, the integer part is interpreted as having 3 digits, the decimal point takes 1 digit, and the fractional part has 2 digits, resulting in a total of 6 digits.
The following program demonstrates how to format real numbers.
Source code
#include <stdio.h>
int main(void)
{
double pi = 3.14159;
printf("%6.2f\n",pi);
printf("123456\n");
return 0;
}
The output of this program is as follows.
Results
3.14
123456
123456
The following line is included for readability of the digits.
It appears the display is six digits total, with one digit for the decimal point and two digits for the fractional part.
If you set the number of decimal places to 0, the decimal point will not be displayed.
You can also specify other options.
For details, refer to the output conversion specifiers in Words and Symbols.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




