Practice Problem 20
Basics
Question 1-1
What should a file containing only declarations be called?
Question 1-2
What do you call the files where the actual program is written?
Program Manual
Question 2-1
#include <stdio.h>
#include <string.h>
typedef struct {
char name[256];
int age;
int sex;
} People;
void InputPeople(People* data)
{
printf("name:");
scanf("%s", data->name);
printf("Age:");
scanf("%d", &data->age);
printf("Gender(1-Male、2-Woman):");
scanf("%d", &data->sex);
printf("\n");
}
void ShowPeople(People data)
{
char sex[16];
printf("name:%s\n", data.name);
printf("Age:%d\n", data.age);
if (data.sex == 1) {
strcpy(sex, "Male");
} else {
strcpy(sex, "Woman");
}
printf("Gender:%s\n", sex);
printf("\n");
}
Please separate this program into header and source files.
explanatory
Question 3-1
Even though all functions can be written in a single source file and still function, briefly state the reason for deliberately splitting them.
Fundamentals (Answer Key)
Solution 1-1
Header file
Solution 1-2
Source file
Program Documentation (Example Solution)
Include People.h
/* People.h */
#ifndef __PEOPLE_H__
#define __PEOPLE_H__
#include <stdio.h>
#include <string.h>
typedef struct {
char name[256];
int age;
int sex;
} People;
/* Enter your personal data */
extern void InputPeople(People* data);
/* Display personal data */
extern void ShowPeople(People data);
#endif
Solve 2-1 People.c
/* People.c */
#include "People.h"
void InputPeople(People* data)
{
printf("name:");
scanf("%s", data->name);
printf("Age:");
scanf("%d", &data->age);
printf("Gender(1-Male、2-Woman):");
scanf("%d", &data->sex);
printf("\n");
}
void ShowPeople(People data)
{
char sex[16];
printf("name:%s\n", data.name);
printf("Age:%d\n", data.age);
if (data.sex == 1) {
strcpy(sex, "Male");
} else {
strcpy(sex, "Woman");
}
printf("Gender:%s\n", sex);
printf("\n");
}
There are various schools of thought on division.
Here, we used the approach of including it within a header file.
It is also possible to perform all #include directives in source files.
Descriptive (answer example)
Solution 4-1
Splitting improves program readability,
making reuse and multi-person development easier.
making reuse and multi-person development easier.
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.




