learn through suffering C language learn through suffering 
C language

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.

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.

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.



About This Site

Learning C language through suffering (Kushi C) is
This 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.


Part 0: Program Overview

  1. What is a program?



Chapter 3: Displaying on the Screen

  1. String Display
  2. line break
  3. Practice Problem 3

Chapter 4: Displaying and Calculating Numbers

  1. Display of numbers
  2. Basic calculations
  3. Numeric types
  4. Practice Problem 4


Chapter 6: Input from the Keyboard

  1. input function
  2. The fear of input
  3. Practice Problem 6



Chapter 9: Repeating a Fixed Number of Times

  1. Iterative sentence
  2. How Loops Work
  3. Practice Problem 9

Chapter 10: Repeating Without Knowing the Number of Times

  1. Unspecified loop
  2. Input validation
  3. Practice Problem 10



Chapter 13: Handling Multiple Variables at Once

  1. Handling multiple variables collectively.
  2. Arrays
  3. Practice Problem 13






Chapter 19: Dynamic Arrays

  1. Create arrays freely.
  2. Practice Problem 19

Loading comment system...