Practice Problem 16
Basics
Question 1-1
What is the term for the practice of combining multiple distinct types?
Program documentation
Question 2-1
Create a program that takes and displays the name, age, and gender of three people.
However, the data will be stored in a structure.
Data input and display will be handled by dedicated functions.
However, the data will be stored in a structure.
Data input and display will be handled by dedicated functions.
explanatory
Question 3-1
別々にDeclare variables.してもQuestionなくデータを記憶できるのに、
Briefly explain why structs are used.
Briefly explain why structs are used.
Fundamentals (Answer Key)
Solution 1-1
struct
Program Documentation (Example Solution)
Solution 2-1
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[256];
int age;
int sex;
} People;
void InputPeople(People *data);
void ShowPeople(People data);
int main(void)
{
People data[3];
int i;
for (i = 0; i < 3; i++)
{
InputPeople(&data[i]);
}
for (i = 0; i < 3; i++)
{
ShowPeople(data[i]);
}
return 0;
}
void InputPeople(People *data)
{
printf("name:");
scanf("%s", data->name);
printf("Age:");
scanf("%d", &data->age);
printf("Gender(1-男性、2-女性):");
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, "男性");
}
else
{
strcpy(sex, "女性");
}
printf("Gender:%s\n", sex);
printf("\n");
}
If this program were significantly expanded,
It is also possible to create an address book management program with sufficient practicality.
descriptive (answer)
Solution 3-1
It's convenient to manage related data together if you use a struct.
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.




