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

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.


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