Values that do not change during execution are called constants.
So far, any numbers written directly into the program have been constants.
Strings directly written are also constants and are called string literals.
In programming, you often use the same numbers or strings repeatedly.
For example, pi is approximately 3.14159, and this value remains constant at all times.
Like this, writing the same value or string repeatedly is wasteful.
Also, if it becomes necessary to change those numbers or strings, it's a hassle to correct them.
For example, if there were a calculator software with a function to calculate consumption tax,
"What if the consumption tax suddenly increased from 3% to 5%?"
現在ではそれどころではなくなってしまいましたが・・・
We need to replace all instances of the number 0.03 in the program with 0.05.
There might be a 0.03 with a meaning other than consumption tax.
It's quite a laborious process to rewrite it, checking each one individually to avoid making mistakes.
Therefore, we will assign names to the numerical values and use those names.
Just modify the named section, and all the numbers will be corrected.
If it's labeled with a name, it's easier to understand than just a number.
C has a mechanism for naming numeric values.
That is a
#define pseudo-instruction.
The usage of #define pseudo-instructions is as follows.
First, with the #define preprocessor directive, do not put a semicolon at the end of the line.
Also, it's common to place this statement at the beginning of the program.
Also, while the same characters can be used in names as in variables,
uppercase letters are generally used.
The following program takes a base price as input and displays the price including tax.
#include <stdio.h>
#define EXCISETAX 0.03 /* ここでConstantを宣言 */
int main(void)
{
int price;
printf("本体価格:");
scanf("%d", &price);
price = (int)((1 + EXCISETAX) * price); /* Constant使用 */
printf("税込価格:%d\n", price);
return 0;
}
"If you run this program and enter 300 as input, the result will be as follows:"
This program uses constants defined by `#define` pseudo-instructions for numerical calculations.
"I'm using the name EXCISETAX in the formula for calculating the inclusive price,"
This name is replaced with 0.03 by the leading #define pseudo-instruction.
"Since the number 0.03 replacing EXCISETAX in this formula is a real number,"
"The calculation results are also real numbers, but normally real numbers aren't used for prices."
Casting to an integer and assigning it.
Changing the value in a #define statement will change the corresponding numerical values in the program.
The following is a snippet of code that changes the sales tax to 0.05.
#define EXCISETAX 0.05 /* ここでConstantを宣言 */
"If you run this program and enter 300 as input, the result will be as follows:"
In this way, using constants makes the meaning of numbers clearer and makes corrections easier.
The #define pseudo-instruction allows you to name not only numbers, but also strings.
The usage is the same as with numbers, but for strings, you'll of course need to enclose them in "".
The following program displays the author's name.
#include <stdio.h>
#define AUTHOR "Masanori Moriguchi"
int main(void)
{
printf("作者名:%s\n", AUTHOR);
return 0;
}
The results of this program's execution are as follows:
Author: Masanori Moriguchi
Here, AUTHOR will also be replaced with "Masanori Moriguchi."
"Of course, modifying the #define pseudo-instruction will affect all AUTHORS."