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