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.
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("Aは %d is.\n", a);
printf("Bは %d is.\n", b);
printf("Cは %d is.\n", c);
return 0;
}
The output of this program is as follows:
Aは 10000 is.
Bは 500 is.
Cは 3 is.
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.
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("Aは %5d is.¥n", a);
printf("Bは %5d is.¥n", b);
printf("Cは %5d is.¥n", c);
return 0;
}
The output of this program is as follows:
Aは 10000 is.
Bは 500 is.
Cは 3 is.
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 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.
#include <stdio.h>
int main(void)
{
int a = 10000, b = 500, c = 3;
printf("Aは %05d is.\n", a);
printf("Bは %05d is.\n", b);
printf("Cは %05d is.\n", c);
return 0;
}
The output of this program is as follows:
Aは 10000 is.
Bは 00500 is.
Cは 00003 is.
I feel like the display has a sort of computer-like quality.
"For displaying real numbers, you can specify both the total number of digits and the number of digits after the decimal point."
"%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.
#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:
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.
詳しくは
Words and symbols のOutput format specifiersを参照してください。