MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

Basic calculations

Calculations and display of results.
In the previous section, we displayed the equation 100+200=300 on the screen.
"Back then, I just listed the numbers one hundred, two hundred, and three hundred directly."
"So, the part about the answer being 300 was actually calculated by humans beforehand."

However, that defeats the purpose of using a computer.
Such calculations should be left to computers.

In C, you only need to write an expression to perform calculations.
The following program is an example of making a computer perform the calculation 100+200.

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

The results of this program's execution are as follows:

Execution results

The screen shows no results.
This is as it should be, and the previous program was only written to calculate 100+200.
Because it doesn't say anywhere to display the calculated results.

Computers are stubborn yet obedient.
この様に、コンピュータは頑固であり素直でもあります。
要するに、与えられた命令はすべてその通りに素直にrunしますが、
それ以外のことは一切やってくれないという頑固さも持っています。

人間からすれば、計算したら結果を表示するのが当然ですが、
コンピュータにはそんな気配りはまったくありません。

"So, how do we display the calculated results?"
Of course, we'll use the printf function to display on the screen.
I know how to display characters using the printf function and how to convert numbers to strings and display them.

Actually, it's possible to pass a formula and convert the result of the calculation into a number.
The following program is an example that calculates and displays the result of 100 + 200.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
300

The noteworthy aspect of this program is that the argument passed to the printf function is...
It's not about saying "100+200," it's about stating the result, which is 300.
The portion "100+200" is automatically calculated to 300, regardless of the printf function.
And then that 300 was further converted into a number and displayed on the screen as 300.
Arithmetic operators
In the preceding section, we performed addition, but of course, other calculations are possible.
The basic operators in C are shown in the following table.

C languageでのsymbol 数学でのsymbol 機能
+ + 加算(足し算)
- - 減算(引き算)
* × 乗算(掛け算)
/ ÷ 除算(割り算)
% 剰余算(割り算の余り)

You'll likely notice that this table uses symbols different from those used in mathematics.
"Since symbols like × and ÷ are not available on a standard computer keyboard,"
Many programming languages, not just C, use symbols distinct from those used in mathematics.

The way operators are used is exactly the same as in mathematics.
The following program demonstrates the use of all the operators presented 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 results of this program's execution are as follows:

Execution results
13
7
30
3
1

The part I want you to focus on in this program is...
The issue is that the calculation of 10/3 (10÷3) is displaying as 3.
"A calculator would typically display this as 3.333333."
Here, all numbers are calculated as integers, so the results will also be integers.

Please note that the result of integer calculations will be truncated, not rounded.
Because when rounding, it leads to an inverse calculation error when checking the product divided by the divisor.
This is because the result contradicts the original dividend.

Cheap Calculators vs. High-End Calculators
実は、この計算は、Cheap Calculators vs. High-End Calculatorsを区別する手軽な方法is.
皆さんもお手持ちの電卓で 10÷3×3 と計算してみてください。
筆者が所持している高級電卓では、10 と表示されましたが、
100 yenショップの電卓では 9.9999999 と表示されました。
高級電卓では、誤差をなるべく少なくする計算方法が採用されています。

complex equation
C can do more than just simple calculations.
Even with more complex formulas, it can calculate without any issues.
The following program is an example of calculating the sum from 1 to 100 using a formula.

Source code
#include <stdio.h>

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

The results of this program's execution are as follows:

Execution results
5050

Even in C, the order of operations in expressions is the same as in mathematics.
Multiplication and division are performed before addition and subtraction.
"Changing priorities is just like adding parentheses in math."

However, in mathematics, we use {} for double parentheses, but it's different in C.
In C, regardless of the level of nesting, parentheses are always used as parentheses.


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