C language | Use structure variables to store student information

Example 37: C language implementation puts a student's information (including student ID, name, name, address) in a structure variable. Then output the student's information.

Problem-solving ideas: first create a structure type in the program, including the members of student information. Then use it to define structure variables and assign initial values.

You can initialize its members when defining structure variables. The initialization list is some constants enclosed in curly braces, which are assigned to each member of the structure variable in turn.

Source code demo:

#include<stdio.h>//头文件 
int main()//主函数 
{
    
    
  struct student_Information   //定义学生结构体 
  {
    
    
    int num; //学号 
    char name[20];//名字 
    char sex[20];//性别 
    char address[20]; //地址 
  }
  student_Information={
    
    8888,"闫小林","男生","广州市"};//赋值 
  printf("学号是:%d\n",student_Information.num);//输出学号 
  printf("姓名是:%s\n",student_Information.name);//输出名字 
  printf("性别是:%s\n",student_Information.sex);//输出性别 
  printf("住址是:%s\n",student_Information.address);//输出住址
  return 0;//主函数返回值为0 
}

The compilation and running results are as follows:

学号是:8888
姓名是:闫小林
性别是:男生
住址是:广州市

--------------------------------
Process exited after 2.791 seconds with return value 0
请按任意键继续. . .

Readers need to note that when Xiaolin outputs name, gender, and address, the format control characters used are:

%s

Because the stored array is a string, not a single character, if the %c format control character is used, the output result will be as follows:

学号是:8888
姓名是:?
性别是:?
住址是:?

--------------------------------
Process exited after 1.785 seconds with return value 0
请按任意键继续. . .

Just a question, if the gender is only male or female, and a single text, can the format control character use %c?

C language uses structure variables to store student information. For
more cases, you can go to the public account: C language entry to proficient

Guess you like

Origin blog.csdn.net/weixin_48669767/article/details/111322114