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 Display

String and Numeric
The previous chapter explained how to display strings on the screen.
However, you may have trouble if you can only display strings on the screen.

Computers are machines that perform calculations.
However, a string is only a string of characters and cannot be used for calculation.
When performing calculations, they must be treated as numerical values.
If it is a numerical value, it can naturally be used for calculation.

Numbers and figures
In everyday life, numbers and numerals may be words that mean the same thing, but
In the computer world, there is a clear distinction.
Numeric values represent numbers and can be used for calculations.
Numbers (for human convenience) just have the same appearance as numbers.
To a computer, it does not represent a number and cannot be used in calculations.

In C, strings and numbers are clearly distinguished by the way they are written.
In C, a string is anything between enclosed in " ".
Conversely, everything is a string if it is enclosed in "".

Recall the program you created in the previous chapter.

Source Code
 #include <stdio.h>

int main(void)
{
    printf("HelloWorld");
    return 0;
}

The printf function was a function to print a string.
Therefore, a string must be passed to the printf function.
That is why HelloWorld was enclosed in "".

Strings enclosed in "" are sometimes referred to as string literals.

Keywords.
String Literals]

A character constant embedded in a program, enclosed in " ".


In C, there is no need to use a special writing style when writing numerical values.
Just write a series of numbers and they are treated as numbers.
However, even a sequence of numbers is treated as a string if it is enclosed in "".
In other words, it is treated as in the following example.

Numeric and String
1234 is a number
"1234" is a number (string)

Again, only numbers can be used in the calculation.
Numbers cannot be used in calculations.
Display numerical values
The printf function is only a function that prints a string.
Thankfully, however, it also has the ability to display numerical values.

To print numeric values with the printf function, use the output conversion specifier.
This is a type of symbol that is used embedded in a string.

Keywords.
Output conversion specifier]

When you want to convert external data into a string and display it
Symbol specifying the conversion method.


There are many different types of output conversion specifiers, but the most commonly used are
The %d specifier converts an integer value to a string.
This part of the specifier displays the result of converting the number specified at the back into a number.

Anyway, seeing is believing, so let's take a look at how it is used.
The following program displays the number 100.

Source Code
 #include <stdio.h>

int main(void)
{
    printf("%d", 100);
    return 0;
}

The result of executing this program will be as follows

Execution Result
100

Here, %d is specified in the string passed to the printf function, so
The %d part was replaced by the 100 number specified later and displayed.
Combination display with strings
In the previous section, we explained how to use the %d specifier in the printf function to print numbers.
However, with only numbers, it is difficult to understand what on earth they mean.
A single word "100" can mean 100 yen, 100 people, 100 kg, 100 times, and so on.

A solution to this problem can be found by displaying a combination of numbers and strings.
The following program is an example of combining a number and a string to display 100 yen.

source code
 #include <stdio.h>

int main(void)
{
    printf("%d", 100);
    printf("円\n");
    return 0;
}


Handling of Japanese
The C language can display full-width characters as well as half-width characters.
However, because it is based on half-width characters
There are many troublesome points when using full-fledged double-byte characters.
If you just want to display them, there is no problem.
(Problems may occur with compilers made overseas.)

The result of executing this program will be as follows

Execution Result
100 yen

While this method may seem problem-free at first glance, upon closer examination, it is
In order to display one line, the printf function is deliberately used twice.
It would be easier to use a single printf function to display a single line.

The %d specifier can be used in combination with other strings.
If you use the %d specifier in a string, that part of the string is replaced by a number.
The following program is an example of displaying 100 yen with a single printf function.

source code
 #include <stdio.h>

int main(void)
{
    printf("%dYen\n", 100);
    return 0;
}

The result of executing this program will be as follows

Execution Result
100 yen

Thus, the %d specifier can be freely embedded in a string.
Display of multiple numbers
In the previous section, we explained how to embed the %d specifier in a string.
Let's try to use this method to display a simple mathematical expression.
The formula is 100+200=300, a very simple expression.
Of course, the numbers are used to display the data, but no calculations are performed yet.

With the %d specifier, this is no quibble.
The following program is an example that displays 100+200=300.

source code
 #include <stdio.h>

int main(void)
{
    printf("%d+", 100);
    printf("%d=", 200);
    printf("%d\n", 300);
    return 0;
}

The result of executing this program will be as follows

Execution Result
100+200=300

While this method may seem problem-free at first glance, upon closer examination, it is
In order to display one line, the printf function is deliberately used three times.
It would be easier to use only one printf function to display one line.

Actually, it does not matter how many times the %d specifier is used in a single string.
If you use three %d specifiers, you also specify three numerical values.
The following program is an example of a display using three %d designators.

source code
 #include <stdio.h>

int main(void)
{
    printf("%d+%d=%d¥n", 100, 200, 300);
    return 0;
}

The result of executing this program will be as follows

Execution Result
100+200=300

In this program, the %d specifiers correspond one-to-one to the later numbers, in order from the front.


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 more complete 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 Division
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box