MMGames Introduction to C C Language Development Environment C language now Useful Apps Contact Us
MMGames

Automatic version identification

SpineViewer

It's easy to tell by looking at it.

Response Time Checker

I can leave my computer on and do it.

Mouse cleaning time

I can leave my computer on and do it.

Mouse cleaning time

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.

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.


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.

enum constants
In addition to #define and const, the C language has enum constants.
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

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 */
};


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.

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.

Part 0: Program Overview
  1. What is the program?
Chapter 2: How to write a program
  1. Writing Rules
  2. Writing conventions
  3. Exercise 2
Chapter 3: Display on Screen
  1. String display
  2. newline character
  3. Exercise 3
Chapter 4: Numeric Display and Calculation
  1. Numeric Display
  2. Basic Calculations
  3. Type of value
  4. Exercise 4
Chapter 5: Numerical Memory and Calculation
  1. Memorize values
  2. Variable Type
  3. Type conversion
  4. Numeric justification
  5. Exercise 5
Chapter 6: Input from the keyboard
  1. Functions for input
  2. Fear of Input
  3. Exercise 6
Chapter 9: Repetition with a fixed number of times
  1. Sentences that repeat themselves
  2. Loop Operation Mechanism
  3. Exercise 9
Chapter 10: Unknown number of repetitions
  1. Loop of unknown frequency
  2. input check
  3. Exercise 10
Chapter 13: Handling Multiple Variables at Once
  1. Multiple variables are handled together.
  2. How to use arrays
  3. Exercise 13
Chapter 19: Dynamic Arrays
  1. Create arrays at will
  2. Exercise 19
Chapter 20: Multiple Source Files
  1. Minimal division
  2. The Stone of Partition
  3. Exercise 20

Comment
COMMENT

Open the 💬 comment submission box