- ..clause 1..: Const constant
- ..clause 2..: Enum constant
- ..clause 3..: Numerical value and specification enum constants
Though it explained the method to declare a constant by using # define in the foregoing paragraph
In C language, there is a method of declaring other constants.
One is the method of the declaration as const (construction) constant.
The const constant is a variable that cannot change the value.
When you specify const at the head when declaring a variable
It becomes impossible for the variable to change the initial value substituted when declaring.
The following program is an example of rewriting the consumption tax program in the foregoing paragraph in the const constant.
The execution result becomes quite the same as the foregoing paragraph.#include <stdio.h> int main(void) { const double EXCISETAX = 0.05; int price; Printf ("Price of main body"); scanf("%d",&price); price = (int)((1 + EXCISETAX) * price); Printf ("Price including tax: %d\n" price); return 0; }
It is not possible to substitute it for the variable declared applying const.
If [noyouna] [bun] is added, it becomes an error. Besides, it is quite the same as a usual variable.EXCISETAX = 0.03;
The const constant is almost the same as # define as long as it uses it as a constant.
In general, using # define when declaring a constant : though is almost.
It is convenient to want to declare the constant used only in a specific function.
[ Make the constant the number of array elements ].
Though it doesn't make the const constant the number of array elements in C language
It is possible in C++ and C99.
If it is # define quasi-instruction, even both are possible.
[ Usage of const ]
Besides, const might be used as a type of the argument of the function.
The purpose of this is not to change the value when the array is passed.
I will assume the explanation of this respect to be a deferment.
In C language, there is enum ([enamu]) constant besides # define and const.
How to declare the enum constant is as follows.
The numerical value need not be specified (It is possible to specify it), and in the enum constantName, name, and name };
Though it is not possible to seem to use as a constant by the numerical value's being automatically applied
The enum constant is chiefly used as a flag constant.
For instance, when you show the state of the character when playing a game RPG
0 NormalityWhen you distinguish this directly by the numerical value though it is necessary to distinguish numbering [noyou]
1 Poison
2 Paralysis
3 Curse
Because it is possible to show by the [noyounishite] name, it becomes comprehensible.# */# define STATE_POISON 1 /* poison */# define STATE_NUMBLY 2 /* paralysis */# define STATE_CURSE 3 /* of normality of define STATE_NORMAL 0 /* is cursed and is */.
To our regret, enum can treat only the integral value though it is convenient in this manner enum.Usually..poison..paralysis..curse. };
[ The end ]
It applies after the name of enum, and though it peels off and it doesn't apply , to the last name formally
Actually because it operates without trouble in case of almost even if it applies it
It becomes easy to add and to correct the name as follows if it writes.
However, it seems to be likely not to move in the compiler etc. for building in.enum { STATE_NORMAL, STATE_POISON, STATE_NUMBLY, STATE_CURSE, /* ここにも,がある */ };
If it is necessary, it is possible to specify it though the numerical value can be omitted in the enum constant.
How to declare the enum constant is as follows.
The following are examples of the enum constant that specifies the numerical value.Name..numerical value..name..numerical value..name.. numerical value. };
The name of the first head is assumed to be 0 when the numerical value is omitted, and one increased price is set thereafter.enum { ENUM_0, ENUM_1, ENUM_5 = 5, ENUM_6, ENUM_7, ENUM_9 = 9, };