﻿ Handling of Invariant Values - C Language Learned the Hard Way
C language learned by suffering

### Handling of invariant values

##### Invariant value from beginning to end
Values that do not change during execution are called constants.
So far, all the numbers that have been written directly in the program are constants.
Strings written directly are also constants and are called string literals.

The same number or string may be used many times in a program.
For example, pi is approximately 3.14159, and this value is the same at all times.

Thus, writing the same value or string over and over is wasteful.
Also, if the need arises to change that number or string, it is cumbersome to modify.

For example, if you had calculator software with the ability to calculate sales tax
Suppose all of a sudden the sales tax went from 3% to 5%.
Nowadays, that is no longer the case, but ...
All numbers of 0.03 in the program must be rewritten to 0.05.
Maybe there's another 0.03 that means something other than sales tax, so
It is very difficult to rewrite them, checking them one by one to make sure they are not changed by mistake.

Therefore, we will name the numbers in advance and use those names.
Just by modifying the named part, all the numbers will be modified, and
If written by name, the meaning is easier to understand than if it is just a number.
##### Name the number
The C language provides a way to name numbers.
That is the #define pseudo instruction.
The usage of the #define pseudo instruction is as follows

#define pseudo instruction
`` #define Name Numeric``

First, the #define pseudo-instruction must not have a ; at the end of the statement.
It is also common practice to place this statement at the beginning of the program.
Also, the name can be the same letter as the variable, but upper case alphabetical characters are common.
The following program displays the price including tax when the price of the unit is entered.

#define pseudo instruction
`````` #include <stdio.h>

#define EXCISETAX 0.03 /* Declare constant here */

int main(void)
{
int price;

printf("Price:");
scanf("%d", &price);
price = (int)((1 + EXCISETAX) * price); /* use constants */
printf("Price including tax:%d\n", price);

return 0;
}``````

If you run this program and enter 300, the result will be as follows

Execution Result
Price: 300 (input value)
Price including tax:309

This program uses constants with the #define pseudo instruction for numerical calculations.
You use the name EXCISETAX in the formula to calculate the price including tax, but
This name is replaced by 0.03 by the #define pseudo instruction at the beginning.

In this expression, the number 0.03 replacing EXCISETAX is the real number, so
The calculation result is also a real number value, but since real numbers are not normally used for prices
The value is cast (converted) to an int type and substituted.

If you change the number in the #define statement, the number in the program will also change.
The following program is the first part of a case where the sales tax is changed to 0.05.

Sales tax changed to 0.05
`` #define EXCISETAX 0.05 /* declare constant here */``

If you run this program and enter 300, the result will be as follows

Execution Result
Price:300 (Input value)
Price including tax:315

In this way, constants make it easier to understand the meaning of the numbers and to modify them.
##### Name the string
The #define pseudo instruction allows you to name strings as well as numbers.
The usage is the same as for numerical values, but strings must of course be marked with "".
The following program displays the name of the author of the program.

source code
`````` #include <stdio.h>

#define AUTHOR "Masanori Moriguchi"

int main(void)
{
printf("Author name:%s\n", AUTHOR);
return 0;
}``````

The result of executing this program will be as follows

Execution Result
Author:Masanori Moriguchi

Again, AUTHOR is replaced by "Masanori Moriguchi".
Of course, changing the #define pseudo instruction part will affect all AUTHORs.

Comment
COMMENT

Open the 💬 comment submission box