In the previous section, we explained how to declare constants using #define.
There are other ways to declare constants in C.
One way is to declare it as a `const` constant.
A constant is a variable whose value cannot be changed.
When declaring a variable, specifying `const` at the beginning indicates that...
The variable becomes immutable after it is initialized.
The following program is an example of rewriting the sales tax program from the previous section using const constants.
The results will be exactly the same as in the previous section.
Variables declared with the `const` keyword cannot be assigned a value.
Source code
EXCISETAX = 0.03;
Adding sentences like that will result in an error.They can otherwise be treated like regular variables.
const constants are largely the same as #define as long as they are used as constants.
Generally, when declaring constants, you often use #define.
"It can be useful when you want to declare constants that are only used within a specific function."
Set the constant to the number of array elements.
C languageでは、const constantをarrayの要素数にできませんが、 It's possible in C++ and C99. "Alternatively, with a #define pseudo-instruction, either is possible."
Ways to Use const
const can also be used as a type for function arguments. This is to prevent the values from being modified when passing an array.
enum constant
C has enums in addition to #define and const.
The way to declare enum constants is as follows:
enum constant
enum {
名前,
名前,
名前
};
Enum constants don't require a value to be specified (though they can be given one).
"It's convenient for declaring large numbers of constants because they can be automatically assigned values based on their names alone."
"It seems like if they are automatically numbered, they might not be usable as constants, though."
Enum constants are primarily used as flag constants.
For example, when representing a character's status in an RPG game,
Character Status
0 正常 1 毒 2 マヒ 3 呪われ
"…need to be differentiated with numbers like that, but directly distinguishing them with numbers…"
It's very inconvenient because I can't tell them apart.
Therefore, by using a #define statement,
It makes things clearer because they can be represented by name.
However, in this case, the number itself has no meaning; it only needs to be distinguishable.
It would be easier and better to have the enum automatically numbered like this.
While enums are convenient like this, unfortunately, they can only handle integer values.
When dealing with real numbers, you have no choice but to use `#define` or `const` constants.
Also, strings cannot be handled.
If values are omitted, the first name is assigned a value of 0, and subsequent names are incremented by 1.
Wherever a number is specified, that number is used, and subsequent values are incremented by one.
In the example above, the numbers are assigned in sequence from the beginning: 0, 1, 5, 6, 7, and 9.
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.