Practice Problem 18
Basics
Question 1-1
What do you call variables that don't change their value within a program?
Question 1-2
What should we call simple functions created using the replace function?
Program reading
In the following program, what is the purpose of the macro TRI?
Determine the answer based on the processing content and variable names.
Determine the answer based on the processing content and variable names.
Question 2-1
#include <stdio.h>
#define TRI(A, H) ((A) * (H) / 2)
int main(void)
{
int side, high, square;
scanf("%d,%d", &side, &high);
printf("%d\n", TRI(side, high));
return 0;
}
Program Manual
Question 3-1
Based on the problem created in Exercise 11:
Create a program that displays whether the Olympics are held when a Gregorian year is entered.
However, create the part that calculates whether the Olympics are held as a separate function.
Modify this further so that the function returns an enum constant as its return value.
Create a program that displays whether the Olympics are held when a Gregorian year is entered.
However, create the part that calculates whether the Olympics are held as a separate function.
Modify this further so that the function returns an enum constant as its return value.
explanatory
Question 4-1
Even though you could simply write the number directly or use a variable, explain indirectly why you would deliberately use a constant instead.
Fundamentals (Answer Key)
Solution 1-1
Constant
Solution 1-2
macro
Program Reading (Solution Example)
Solution 2-1
A macro to calculate the area of a triangle.
Program Documentation (Example Solution)
Solution 3-1
#include <stdio.h>
int olympic(int year);
enum {
OLYMIPC_NON,
OLYMIPC_SUMMER,
OLYMIPC_WINTER,
};
int main(void)
{
int year, hold;
scanf("%d", &year);
hold = olympic(year);
switch (hold) {
case OLYMIPC_NON:
printf("Unopened\n");
break;
case OLYMIPC_SUMMER:
printf("Summer Olympics\n");
break;
case OLYMIPC_WINTER:
printf("Winter Olympics\n");
break;
};
return 0;
}
int olympic(int year)
{
if (year % 2 == 0) {
if (year % 4 == 0) {
return OLYMIPC_SUMMER;
} else {
return OLYMIPC_WINTER;
}
} else {
return OLYMIPC_NON;
}
}
By being named for its holding status,
It's clearer than distinguishing with numbers like 0.1.2.
Descriptive (answer example)
Solution 4-1
Naming things makes them clearer and prepares you for change.
Since constants cannot be reassigned, there's no risk of accidentally modifying their values, which can prevent bugs from occurring.
Since constants cannot be reassigned, there's no risk of accidentally modifying their values, which can prevent bugs from occurring.
About This Site
Learning C language through suffering (Kushi C) isThis is the definitive introduction to the C language.
It systematically explains the basic functions of the C language.
The quality is equal to or higher than commercially available books.




