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("Price:");
scanf("%d", &price);
price = (int)((1 + EXCISETAX) * price);
printf("Price including tax:%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.
In C, you cannot define a const constant as the number of elements in an array,
but this is possible in C++ and C99.
Note that using the #define pseudo-directive is possible in both languages.
but this is possible in C++ and C99.
Note that using the #define pseudo-directive is possible in both languages.
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 Normal
1 Poisoned
2 Paralyzed
3 Cursed
1 Poisoned
2 Paralyzed
3 Cursed
…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 /* Normal */
#define STATE_POISON 1 /* Poisoned */
#define STATE_NUMBLY 2 /* Paralyzed */
#define STATE_CURSE 3 /* Cursed */
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, /* Poisoned */
STATE_NUMBLY, /* Paralyzed */
STATE_CURSE /* Cursed */
};
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,
While the formal syntax does not require adding a semicolon after the last name in an enum, it actually works fine even if you do. Therefore, writing it like this makes adding or modifying names easier.
enum {
STATE_NORMAL,
STATE_POISON,
STATE_NUMBLY,
STATE_CURSE, /* Here too, there is a */
};
enum {
STATE_NORMAL,
STATE_POISON,
STATE_NUMBLY,
STATE_CURSE, /* Here too, there is a */
};
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 values for 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.




