learn through suffering C language learn through suffering 
C language

Numeric types

various numbers

So far, we have been using the term numerical value.
There are two types of numbers handled in C.
It is integers and real numbers.

Integers are numbers that include natural numbers, zero, and negative numbers.
For example, numbers like 1, -1, 0, 5, 8, 7,...
In short, we call the numbers we normally use, ordinary numbers, integers.

Real numbers are numbers that include integers and numbers with decimal values.
For example, numbers like 1.0, 5.2, -9.687, 3.14159, and so on,
In short, numbers that include decimals are called real numbers.

Also, integers can be represented using three different notations.
It is divided into three types: decimal, octal, and hexadecimal.

Format 進数
digit decimal number
0digit octal
0xdigit hexadecimal

Numbers without leading zeros are treated as decimal numbers.
For example, 100, 25, 68, 71, and 19023 are decimal numbers.

Numbers starting with a zero are treated as octal.
For example, 0152, 027, 0756, and 030303 are octal numbers.
Commonly, 0152 and 152 seem like the same number.
In C, be aware that 0152 is interpreted as an octal number (106 in decimal).

For similar reasons, the number 089 would result in an error in C.
Because octal doesn't use digits like 8 or 9.

In practice, octal is rarely used, so it's probably best not to prefix it with a zero.
By the way, if it's set to 0, it's also 0 in octal, so there's no problem.

Surprisingly, hexadecimal is frequently used, and it's represented by prefixing it with 0x.

For example, 0xFF, 0xA7, 0x912C, and 0xABCD are hexadecimal numbers.
In C, certain numeric values are used to represent specific meanings, such as character encodings.
Besides being used conventionally in hexadecimal,
Hexadecimal notation is frequently used in operations such as bitwise operations.
Of course, beginners probably won't be using it for quite some time.

Please note that we can only use decimal notation for representing real numbers.
There are various ways to handle real numbers in computers.
In C, a method called floating-point notation is used.
For this reason, in the world of C programming, real numbers are often referred to as floating-point numbers.

Keyword
【Floating-point arithmetic】

A method of representing real numbers as a sequence of digits (the mantissa) and the position of the decimal point (the exponent).
The real number is expressed by multiplying the mantissa by a value such as 10 raised to a power.
It is convenient for handling numbers ranging from extremely large to extremely small, but calculations are slow.

Real number calculations

In the previous chapter's calculations, the answer to 10/3 (10÷3) was incorrectly stated as 3.
I would like to calculate this as accurately as possible using real numbers.
However, since it's not a fraction, it can't be calculated perfectly accurately.Please forgive me.

Accurate calculations
As such, it is difficult for computers to perform precise calculations.
Using real numbers inevitably results in non-integer values, and there are also errors arising from calculations in binary.
This is because binary cannot accurately represent numbers like 0.1.

While rounding off usually isn't a major issue,
computers in fields requiring precision, such as banking,
apparently employ systems that perform calculations as decimal fractions.

If you want to perform calculations with real numbers, simply make the numbers real, and the calculation will automatically be done with real numbers.
The calculation itself is fine, but another thing to keep in mind is...
The output conversion specifiers used for display will also change.

Previously, when displaying numbers, we have been using the %d specifier,
This is a specifier for converting an integer value to a digit.
When converting real numbers to digits, you must use the %f specifier.

Now that you understand this much, the rest is easy.
The following program is an example of adapting the program from the previous chapter to perform calculations with 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 output of this program is as follows:

Results
13.000000
7.000000
30.000000
3.333333

It looks like the answer was calculated using real numbers.
However, we haven't used the %(remainder) operator this time, which is perfectly natural.
Since remainders cannot be calculated with real numbers, the % operator cannot be used.


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

Loading comment system...