C language learned by suffering
C language learned by suffering
Type of value
Various numbers
So far, we have used the expression "numerical value.
There are two types of numbers handled by the C language.
They are integers and real numbers.
An integer is a natural number plus zero and a negative number.
For example, 1, -1, 0, 5, 8, 7... and so on.
In short, the ordinary numbers we normally use are called integers.
A real number is an integer plus a fractional number.
For example, 1.0, 5.2, -9.687, 3.14159, and so on.
In short, a number that contains decimals is called a real number.
Also, integers can be written in three different ways.
It is divided into three types: decimal, octal, and hexadecimal.
Numbers without leading zeros are treated as decimal numbers.
For example, 100, 25, 68, 71, 19023, etc. are decimal numbers.
Numbers with leading zeros are treated as octal numbers.
For example, 0152, 027, 0756, 030303, etc. are octal numbers.
In the ordinary sense, one would think that 0152 and 152 are the same number.
Note that in C, 0152 is interpreted as an octal number (106 in decimal).
Also, for the same reason, numbers such as 089, say, are an error in C.
This is because octal numbers do not use numbers like 8 or 9.
In reality, octal numbers are rarely used, so it is better not to add leading zeros.
Incidentally, there is no problem if the number is set to 0, since it is also 0 in octal.
Surprisingly often used are hexadecimal numbers, which are represented by a leading 0x.
For example, 0xFF, 0xA7, 0x912C, 0xABCD, etc. are hexadecimal numbers.
In C, the representation of a number with a specific meaning, such as a character code, is represented by a
In addition to the customary use of hexadecimal numbers, the
Hexadecimal notation is frequently used for bit operations and other processes.
Of course, beginners are not likely to use it for the time being.
There are many ways to handle real numbers on a computer.
The C language uses a method called floating point method.
For this reason, real numbers are often referred to as floating decimals in the C language world.
There are two types of numbers handled by the C language.
They are integers and real numbers.
An integer is a natural number plus zero and a negative number.
For example, 1, -1, 0, 5, 8, 7... and so on.
In short, the ordinary numbers we normally use are called integers.
A real number is an integer plus a fractional number.
For example, 1.0, 5.2, -9.687, 3.14159, and so on.
In short, a number that contains decimals is called a real number.
Also, integers can be written in three different ways.
It is divided into three types: decimal, octal, and hexadecimal.
Format | decimal number |
---|---|
Numerals | Decimal |
0 digit | octal |
0x number | Hexadecimal |
Numbers without leading zeros are treated as decimal numbers.
For example, 100, 25, 68, 71, 19023, etc. are decimal numbers.
Numbers with leading zeros are treated as octal numbers.
For example, 0152, 027, 0756, 030303, etc. are octal numbers.
In the ordinary sense, one would think that 0152 and 152 are the same number.
Note that in C, 0152 is interpreted as an octal number (106 in decimal).
Also, for the same reason, numbers such as 089, say, are an error in C.
This is because octal numbers do not use numbers like 8 or 9.
In reality, octal numbers are rarely used, so it is better not to add leading zeros.
Incidentally, there is no problem if the number is set to 0, since it is also 0 in octal.
Surprisingly often used are hexadecimal numbers, which are represented by a leading 0x.
For example, 0xFF, 0xA7, 0x912C, 0xABCD, etc. are hexadecimal numbers.
In C, the representation of a number with a specific meaning, such as a character code, is represented by a
In addition to the customary use of hexadecimal numbers, the
Hexadecimal notation is frequently used for bit operations and other processes.
Of course, beginners are not likely to use it for the time being.
Note that only decimal numbers can be used to represent real numbers.
There are many ways to handle real numbers on a computer.
The C language uses a method called floating point method.
For this reason, real numbers are often referred to as floating decimals in the C language world.
Keywords.
Floating point method
A method of expressing a real number in terms of a sequence of numbers (the mantissa part) and the position of the decimal point (the exponent part).
Real numbers are expressed by multiplying the mantissa part by a value such as a power of 10.
This method is convenient for handling both large and very small numbers, but it is slow.
Computing Real Numbers
The calculation we did in the previous chapter gave the answer 10/3 (10 divided by 3) as 3.
I would like to calculate this as accurately as possible using real numbers.
However, since it is not a fraction, it cannot be calculated with perfect accuracy. Please forgive me.
If you want to calculate with real numbers, just make the number a real number and it will automatically be calculated as a real number.
That's fine for the calculation itself, but there's one more thing to keep in mind.
The output conversion specifier used for display will also change.
Previously, when displaying numerical values, the %d specifier has been used.
This is a specifier for converting integer values to numbers.
If you want to convert a real number to a numeric value, you must use the %f specifier.
Once you know this much, the rest is easy.
The following program is an example of a program from the previous section reworked into a calculation using real numbers.
The result of executing this program will be as follows
The answer is very much like a calculation with real numbers.
Note that the % (remainder) operator is not used this time, but this is to be expected.
Since the remainder cannot be calculated with real numbers, the % operator cannot be used.
I would like to calculate this as accurately as possible using real numbers.
However, since it is not a fraction, it cannot be calculated with perfect accuracy. Please forgive me.
Accurate calculations
As you can see, it is difficult to perform exact calculations on a computer.
When real numbers are used, there will always be some numbers that are not divisible.
There is also an error caused by the use of binary numbers.
In fact, binary numbers cannot accurately represent numbers such as 0.1.
Under normal circumstances, rounding off the rounded numbers would not be a big problem.
Bank computers, for example, which need to be accurate, are
They have a mechanism to calculate as a decimal fraction.
When real numbers are used, there will always be some numbers that are not divisible.
There is also an error caused by the use of binary numbers.
In fact, binary numbers cannot accurately represent numbers such as 0.1.
Under normal circumstances, rounding off the rounded numbers would not be a big problem.
Bank computers, for example, which need to be accurate, are
They have a mechanism to calculate as a decimal fraction.
If you want to calculate with real numbers, just make the number a real number and it will automatically be calculated as a real number.
That's fine for the calculation itself, but there's one more thing to keep in mind.
The output conversion specifier used for display will also change.
Previously, when displaying numerical values, the %d specifier has been used.
This is a specifier for converting integer values to numbers.
If you want to convert a real number to a numeric value, you must use the %f specifier.
Once you know this much, the rest is easy.
The following program is an example of a program from the previous section reworked into a calculation using real numbers.
source code
#include <stdio.h>
int main(void)
{
printf("%f\n", 10.0 + 3.0);
printf("%f\n", 10.0 - 3.0);
printf("%f\n", 10.0 * 3.0);
printf("%f\n", 10.0 / 3.0);
return 0;
}
The result of executing this program will be as follows
Execution Result
13.000000
7.000000
30.000000
3.333333
7.000000
30.000000
3.333333
The answer is very much like a calculation with real numbers.
Note that the % (remainder) operator is not used this time, but this is to be expected.
Since the remainder cannot be calculated with real numbers, the % operator cannot be used.
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.