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.
The result of executing this program will be as follows
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.
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.
The result of executing this program will be as follows
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.
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.
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.
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.
The result of executing this program will be as follows
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.
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.
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
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.
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.
The result of executing this program will be as follows
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 ().
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.