﻿ Exercise 18 - Learning C Sufferingly
C language learned by suffering

### Exercise 18

##### fundamental knowledge

Q1-1
What do you call a variable whose value does not change during the program?

Q 1-2
What do you call a simple function created using a replacement function?

##### program read-only
In the following program, what is the macro TRI for?
Answer judging from the processing contents and variable names.

Q2-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 writing

Q3-1
I created in Exercise 11, a
The program should display whether the Olympics will be held when the year of the year is entered.
However, the part of the program that calculates the date of the Olympics must be created as a separate function.
In addition, the return value of the function should be returned as an enum constant.

##### descriptive expression

Q4-1
You could just write the number directly or use a variable, but you can't
Indirectly explain why you bother to use constants.

Solution 1-1
constant

Solution 1-2
macro

##### Program reading (example solution)

Solution 2-1
Macro to find the area of a triangular shape.

##### Program writing (example of solution)

Solution 3-1
`````` #include <stdio.h>

int olympic(int year);

The 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("not opened\n");
break;
case OLYMIPC_SUMMER:
printf("Summer Olympics\n");
break;
case OLYMIPC_WINTER:
printf("Winter 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;
}
}``````

*The name given to the state in which the event is held has been used to
It is easier to understand than to distinguish by a numerical value such as 0.1.2.
##### Short Answer Type (Sample Answer)

Solution 4-1
Naming them makes them easier to understand and prepare changes.
Furthermore, constants are not assignable, so the values cannot be changed by mistake, leading to the phenomenon of bugs.

Comment
COMMENT

Open the 💬 comment submission box