MMGameslogo  MMGames
TwitterSharebutton  FacebookSharebutton   
learn through sufferingC Language
learn through sufferingC Language

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.

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."


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.

enum constant
C has enums in addition to #define and const.
The way to declare enum constants is as follows:

enum constant
enum {
    名前,
    名前,
    名前
};

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 呪われ

"…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の名前の後につける,は、正式には最後の名前にはつけませんが、
実際にはつけても問題なく動作します。
Therefore,次のように書くと、名前の追加や修正が楽になります。

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:

Specify values for enum
enum {
    名前 = numerics,
    名前 = numerics,
    名前 = 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) is
This 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.

Part 0: Program Overview
  1. What is a program?
Chapter 3: Displaying on the Screen
  1. String Display
  2. newline character
  3. Practice Problem 3
Chapter 4: Displaying and Calculating Numbers
  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4
Chapter 6: Input from the Keyboard
  1. input function
  2. The fear of input
  3. Practice Problem 6
Chapter 9: Repeating a Fixed Number of Times
  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9
Chapter 10: Repeating Without Knowing the Number of Times
  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10
Chapter 13: Handling Multiple Variables at Once
  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13
Chapter 19: Dynamic Arrays
  1. Create arrays freely.
  2. Practice Problem 19