Constants by other means
const constant
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.
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.
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.
Source code
#include <stdio.h>
int main(void)
{
const double EXCISETAX = 0.05;
int price;
printf("本体価格:");
scanf("%d", &price);
price = (int)((1 + EXCISETAX) * price);
printf("税込価格:%d\n", price);
return 0;
}
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.
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.
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 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,
…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.
The way to declare enum constants is as follows:
enum constant
enum {
name,
name,
name
};
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 呪われ
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,
#define case
#define STATE_NORMAL 0 /* 正常 */
#define STATE_POISON 1 /* 毒 */
#define STATE_NUMBLY 2 /* マヒ */
#define STATE_CURSE 3 /* 呪われ */
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.
enum case
enum {
STATE_NORMAL, /* normal */
STATE_POISON, /* 毒 */
STATE_NUMBLY, /* マヒ */
STATE_CURSE /* 呪われ */
};
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.
The last,
enumのnameの後につける,は、正式には最後のnameにはつけませんが、
実際にはつけてもQuestionなく動作します。
Therefore,次のように書くと、nameの追加や修正が楽になります。
enum {
STATE_NORMAL,
STATE_POISON,
STATE_NUMBLY,
STATE_CURSE, /* ここにも,がある */
};
実際にはつけてもQuestionなく動作します。
Therefore,次のように書くと、nameの追加や修正が楽になります。
enum {
STATE_NORMAL,
STATE_POISON,
STATE_NUMBLY,
STATE_CURSE, /* ここにも,がある */
};
Enumerated constants with numeric values
Enum constants can omit their values, but you can specify them if needed.
The way to declare enum constants is as follows:
Here's an example of an enum constant with a specified value.
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.
The way to declare enum constants is as follows:
Specify numeric values for an enum.
enum {
name = numerics,
name = numerics,
name = numerics
};
Here's an example of an enum constant with a specified value.
Source code
enum {
ENUM_0,
ENUM_1,
ENUM_5 = 5,
ENUM_6,
ENUM_7,
ENUM_9 = 9,
};
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) isThis 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.




