learn through suffering C language learn through suffering 
C language

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.

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.

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.

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.

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.

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.

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

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) is
This 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.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...