- ..clause 1..: Value of invariability from first to last
- ..clause 2..: Give the numerical value the name.
- ..clause 3..: Give the character string the name.
The value that doesn't change is called a constant while executing it.
Numerical values that have been written directly up to now while programming it are all constants.
The character string written directly is a constant, and it is called the string literal.
When being programming it, the same numerical value and the character string might be used many times.
For instance, pi is about 3.14159, and this value is the same at any time when.
It is useless to write the same value and the character string many times in this manner.
Moreover, when the necessity for changing the numerical value and the character string is caused, the correction is troublesome.
For instance, when there is calculator software that adheres the function to calculate the consumption tax
Suddenly, when the consumption tax becomes 5% from 3%
It is necessary to rewrite the numerical value of 0.03 under programs in all 0.05.
Because there might be 0.03 of the meanings besides the consumption tax possibly
Rewriting confirming not to change it by mistake one by one is serious.
Then, use the name giving the numerical value the name beforehand.
All numerical values are corrected only by correcting the part where the name was given.
The meaning becomes comprehensible from a free numerical value if written by the name.
The method of giving the numerical value the name is prepared in C language.
First of all, in # define quasi-instruction at the end of the sentence;[Wotsuke].#Define name numerical value
The result when this program is executed, and 300 is input becomes as follows.#Include < stdio.h > The constant is declared # define EXCISETAX 0.03 /* here */ Int main. (void) { int price; Printf ("Price of main body"); scanf("%d",&price); price = (int)((1 + EXCISETAX) * price); /* constant use */ Printf ("Price including tax: %d\n" price); return 0; }
In this program, the constant by # define quasi-instruction is used for the math calculation.
Price of main body: 300 (input value)
Price including tax: 309
The numerical value that replaces EXCISETAX in this expression Because 0.03 is a real number value
Usually because it doesn't use the real number for the price though the numerical result also reaches the real number value
It is Cast (conversion) and it substitutes it for the int type.
#If the numerical value of the define sentence is changed, the numerical value under the program is changed.
The following program : the consumption tax It is the first part when changing to 0.05.
The result when this program is executed, and 300 is input becomes as follows.#The constant is declared define EXCISETAX 0.05 /* here */.
The meaning of the numerical value becomes comprehensible if the constant is used, and it becomes easy to correct in this manner.
Price of main body: 300 (input value)
Price including tax: 315
#In the define quasi-instruction, the name is given to not only the numerical value but also the character string.
Naturally, it is necessary to apply ""to the character string though the usage is the same as the case of the numerical value.
The following program displays the name of the author of the program.
The execution result of this program becomes as follows.# # define AUTHOR " Masanori Moriguchi" Include < stdio.h > Int main(void) { Printf ("Name of author: %s\n" AUTHOR); return 0; }
AUTHOR is replaced by "Masanori Moriguchi" here.
Name of author: Masanori Moriguchi