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

Basic Calculations

Calculation and display of results
In the previous section, the equation 100+200=300 was displayed on the screen.
At that time, the numbers 100, 200, and 300 were arranged directly.
In other words, the "300" part of the answer was calculated by a human being first.

However, this makes no sense using a computer.
Such calculations should be done by computers.

In the C language, to calculate, one only needs to write a mathematical expression.
The following program is an example of having the computer calculate 100+200.

source code
 int main(void)
{
    100 + 200;
}

The result of executing this program will be as follows

Execution Result

The screen does not show any results.
This is not surprising, since the previous program only says to compute 100+200, and
The reason is that there is no instruction to display the result of the calculation.

Computers are stubborn and obedient
Thus, computers are both stubborn and straightforward.
In short, it obediently executes every instruction it is given.
It also has a stubbornness that will not do anything else.

From a human point of view, it is only natural that it should display the result of a calculation.
Computers have absolutely no such attentiveness.

So how do you display the results of your calculations?
Of course, the printf function is used to display on the screen.
We know how to use the printf function to display text and convert numbers to digits.

In fact, it is also possible to pass a formula and convert the result of the formula's calculation into a number.
The following program is an example of having 100+200 calculated and displayed.

source code
 #include <stdio.h>

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

The result of executing this program will be as follows

Execution Result
300

The noteworthy part of this program is that the printf function is passed to the
The result of the expression is 300, not 100+200.
The 100+200 part is calculated by itself (regardless of the printf function) and converted to 300.
The 300 was then further converted into a number and displayed on the screen as 300.
four arithmetic operators
In the previous section, addition was performed, but of course other calculations are also possible.
The following table shows the basic operators in the C language.

Symbols in C Symbols in Mathematics Function
+ + (+) Addition
-1 Subtraction
* ×x Multiplication
/ ÷ (÷) Division
% (%) ... remainder (remainder of division)

In this table, you will notice that we sometimes use different symbols than in mathematics.
Since there is no x or ÷ among the symbols that can be represented on a typical computer keyboard, the
Many programming languages, not just C, use symbols that are different from those used in mathematics.

The use of operators is exactly the same as in mathematics.
The following program is an example of using all the operators introduced here.

Source Code
 #include <stdio.h>

int main(void)
{
    printf("%d\n", 10 + 3);
    printf("%d\n", 10 - 3);
    printf("%d\n", 10 * 3);
    printf("%d\n", 10 / 3);
    printf("%d\n", 10 % 3);
    return 0;
}

The result of executing this program will be as follows

Execution Result
13
7
30
3
1

The part of this program I want you to focus on is.
The result of the 10/3 (10 divided by 3) calculation is shown as 3.
A calculator would normally display 3.333333333.
Here, any number is calculated as an integer, so the result is also an integer.

Note that the results of integer calculations are rounded down, not rounded off.
Because if you round to the nearest whole number, when you do the reverse calculation, quotient times the number to divide by, you get
This is because the result is inconsistently greater than the original number to be divided.

Cheap calculators and high-end calculators
In fact, this calculation is a handy way to distinguish between cheap calculators and high-end calculators.
Try calculating 10 ÷ 3 × 3 on your calculator.
On my high-end calculator, the number is 10, but on my cheap calculator, the number is 3.
A calculator at a 100 yen store displayed 9.99999999.
High-end calculators use calculation methods that minimize errors.

Complex expressions
It is not only simple expressions that can be computed in C.
Even more complex expressions can be calculated without problems.
The following program is an example of a formula to calculate the sum from 1 to 100.

source code
 #include <stdio.h>

int main(void)
{
    printf("%d\n", (1 + 100) * 100 / 2);
    return 0;
}

The result of executing this program will be as follows

Execution Result
5050

In C, precedence in mathematical expressions is the same as in mathematics.
Multiplication and division are computed before addition and subtraction.
When you want to change the order of precedence, the () is exactly the same as in mathematics.

However, in mathematics, {} is used for the double (), but not in C.
In C, the () symbol is used for any number of ().


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 Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box