C language learned by suffering
C language learned by suffering
Constant by other method
const const constants
In the previous section, we described how to declare constants using #define.
There are other ways to declare constants in the C language.
One method is to declare them as const (const) constants.
A const constant is a variable whose value cannot be changed.
When declaring a variable, specifying const at the beginning of the
The variable cannot change the initial value assigned at the time of declaration.
The following program is an example of rewriting the sales tax program in the previous section to const const constants.
The execution results are exactly the same as in the previous section.
Variables declared with const cannot be assigned to.
Adding a statement such as "The following is an error. Otherwise, it can be handled just like a normal variable.
A const constant is almost the same as #define as long as it is used as a constant.
In general, #define is most often used to declare constants, but
This is useful when you want to declare a constant to be used only within a specific function.
There are other ways to declare constants in the C language.
One method is to declare them as const (const) constants.
A const constant is a variable whose value cannot be changed.
When declaring a variable, specifying const at the beginning of the
The variable cannot change the initial value assigned at the time of declaration.
The following program is an example of rewriting the sales tax program in the previous section to const const constants.
source code
#include <stdio.h>
int main(void)
{
const double EXCISETAX = 0.05;
int price;
printf("Price:");
scanf("%d", &price);
price = (int)((1 + EXCISETAX) * price);
printf("Price including tax:%d\n", price);
return 0;
}
The execution results are exactly the same as in the previous section.
Variables declared with const cannot be assigned to.
Source Code
EXCISETAX = 0.03;
Adding a statement such as "The following is an error. Otherwise, it can be handled just like a normal variable.
A const constant is almost the same as #define as long as it is used as a constant.
In general, #define is most often used to declare constants, but
This is useful when you want to declare a constant to be used only within a specific function.
constant to the number of elements in the array.
In C, const const constants cannot be the number of elements in an array, but
C++ and C99 allow this.
The #define pseudo-instruction can be used in either case.
C++ and C99 allow this.
The #define pseudo-instruction can be used in either case.
Usage of const.
Const may also be used as a function argument type.
This is so that when an array is passed, its value is not modified.
This is so that when an array is passed, its value is not modified.
enum constants
In addition to #define and const, the C language has enum constants.
The following is how to declare an enum constant.
The enum constant does not have to (and can) specify a numeric value.
This is useful when declaring a large number of constants, since the name alone is automatically assigned a numerical value.
The enum constants are primarily used as flag constants.
For example, in an RPG game, if you want to represent the state of a character, you can use
but if we distinguish this directly by a number, such as
It is very inconvenient to know which is which.
So, by using the #define statement
and can be expressed by name for clarity.
In this case, however, the number itself is meaningless, since it only needs to be distinguishable.
It is easier and better to let enum automatically number them as follows.
Although enums are useful in this way, unfortunately, they can only handle integer values.
When dealing with real numbers, the only way is to use #define or const const constants.
Also, strings cannot be handled.
The following is how to declare an enum constant.
enum constants
The enum {
Name,
Name,
Name
};
The enum constant does not have to (and can) specify a numeric value.
This is useful when declaring a large number of constants, since the name alone is automatically assigned a numerical value.
It may seem that a number cannot be used as a constant if it is automatically assigned a numerical value.
The enum constants are primarily used as flag constants.
For example, in an RPG game, if you want to represent the state of a character, you can use
Character Status
0 Normal
1 Poison
2 Paralysis
3 Cursed
1 Poison
2 Paralysis
3 Cursed
but if we distinguish this directly by a number, such as
It is very inconvenient to know which is which.
So, by using the #define statement
For #define
#define STATE_NORMAL 0 /* Normal */
#define STATE_POISON 1 /* poison */
#define STATE_NUMBLY 2 /* paralysis */
#define STATE_CURSE 3 /* cursed */
and can be expressed by name for clarity.
In this case, however, the number itself is meaningless, since it only needs to be distinguishable.
It is easier and better to let enum automatically number them as follows.
For enum
The enum {
STATE_NORMAL, /* Normal */
STATE_POISON, /* Poison */
STATE_NUMBLY, /* paralysis */
STATE_CURSE /* cursed */
};
Although enums are useful in this way, unfortunately, they can only handle integer values.
When dealing with real numbers, the only way is to use #define or const const constants.
Also, strings cannot be handled.
The last ,
The , which follows the name of the enum, is not formally attached to the last name, but is
In practice, it works fine when attached.
Thus, it is easier to add or modify names by writing
enum {
STATE_NORMAL,
STATE_POISON,
STATE_NUMBLY,
STATE_CURSE, /* Here's another one */
};
In practice, it works fine when attached.
Thus, it is easier to add or modify names by writing
enum {
STATE_NORMAL,
STATE_POISON,
STATE_NUMBLY,
STATE_CURSE, /* Here's another one */
};
Numeric enum constant
In the enum constants, numbers can be omitted, but may be specified if desired.
The following is how to declare an enum constant.
The following is an example of an enum constant with a numerical value.
If a numerical value is omitted, the first name is set to 0 and subsequent names are incremented by 1.
If a numerical value is specified somewhere, it will be the number specified there and incremented by 1 thereafter.
In the example above, the numbers are assigned in the order 0, 1, 5, 6, 7, 9, and so on, starting from the top.
The following is how to declare an enum constant.
Specify a numerical value for enum
The enum {
Name = number,
Name = number,
Name = Numeric
};
The following is an example of an enum constant with a numerical value.
Source Code
enum {
ENUM_0,
ENUM_1,
ENUM_5 = 5,
ENUM_6,
ENUM_7,
ENUM_9 = 9,
};
If a numerical value is omitted, the first name is set to 0 and subsequent names are incremented by 1.
If a numerical value is specified somewhere, it will be the number specified there and incremented by 1 thereafter.
In the example above, the numbers are assigned in the order 0, 1, 5, 6, 7, 9, and so on, starting from the top.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.