C language learned by suffering
C language learned by suffering
Exercise 20
fundamental knowledge
Q1-1
What do you call a file that describes only declarations?
Q 1-2
What do you call the file in which the actual program is written?
program writing
Q2-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-female):"); scanf("%d", &data->age); printf("%d", &data->age); scanf("%d", &data->age)
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, "female");
}
printf("Gender:%s\n", sex);
printf("\n");
}
Separate this program into header and source files.
descriptive expression
Q3-1
It would work even if all functions were written in one source file.
Briefly describe the reason for the division.
Briefly describe the reason for the division.
Basic Knowledge (sample answers)
Solution 1-1
header file
Solution 1-2
source file
Program writing (example of solution)
Solution 2-1 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;
/* Input personal data */
extern void InputPeople(People* data);
/* Display personal data */
extern void ShowPeople(People data);
#endif
Solution 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-female):"); scanf("%d", &data->age); printf("%d", &data->age); scanf("%d", &data->age)
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, "female");
}
printf("Gender:%s\n", sex);
printf("\n");
}
*There are various schools of division.
Here, the technique is to #include in the header file, but you can also #include in the
All #includes may be done in the source file.
Short Answer Type (Sample Answers)
Solution 4-1
The division of the program improves the program's visibility and
Reuse and multi-person development will be easier.
Reuse and multi-person development will be easier.
About this Site
The C language (bitter C), which is learned by suffering, is
This is the definitive C language introductory site.
It systematically explains the basic functions of the C language and
It is as complete as or more complete than any book on the market.