MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

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 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.

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.

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.

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.

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.

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

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.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box