※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  Constant by other methods   

  1. ..clause 1..: Const constant
  2. ..clause 2..: Enum constant
  3. ..clause 3..: Numerical value and specification enum constants

[1] Const constant

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.

 
#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;
}
The execution result becomes quite the same as the foregoing paragraph.

It is not possible to substitute it for the variable declared applying const.


EXCISETAX = 0.03;
If [noyouna] [bun] is added, it becomes an error. Besides, it is quite the same as a usual variable.

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.

It returns to contents.


[2] Enum constant

In C language, there is enum ([enamu]) constant besides # define and const.
How to declare the enum constant is as follows.

 
Name, name, and  name
};
The numerical value need not be specified (It is possible to specify it), and in the enum constant
When a large amount of constant is declared, it is convenient because the numerical value is automatically applied only by the 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 Normality
1 Poison
2 Paralysis
3 Curse
When you distinguish this directly by the numerical value though it is necessary to distinguish numbering [noyou]
Which whether any is not understood and it is very inconvenient.
Then, if you use # define sentence
 # */#  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 */. 
Because it is possible to show by the [noyounishite] name, it becomes comprehensible.
However, not significant to a numeric deflecting in this case only because it only has to be able to distinguish
It is easy and is better to shake the number with enum automatically as follows.
 
Usually..poison..paralysis..curse.
};
To our regret, enum can treat only the integral value though it is convenient in this manner enum.
There is only making to # define or the const constant when the real number value is treated.
Moreover, the character string cannot be treated.

[ 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.
 enum {     STATE_NORMAL,     STATE_POISON,     STATE_NUMBLY,     STATE_CURSE, /* ここにも,がある */ 
};
However, it seems to be likely not to move in the compiler etc. for building in.

It returns to contents.


[3] Numerical value and specification enum constants

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.

 
Name..numerical value..name..numerical value..name..
numerical value.
};
The following are examples of the enum constant that specifies the numerical value.
 
enum { ENUM_0, ENUM_1, ENUM_5 = 5, ENUM_6, 
ENUM_7, ENUM_9 = 9, 
};
The name of the first head is assumed to be 0 when the numerical value is omitted, and one increased price is set thereafter.
It reaches one increased value there the specified numerical value and thereafter when the numerical value is specified somewhere.
In the example above, the numerical value is applied from the head in order of 0, 1, 5, 6, 7, and 9.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.