#include <stdio.h> int main() { struct Student //声明结构体类型struct Student { long int num; //以下4行为结构体的成员 char name[20]; char sex; char addr[20]; }a={10101,"Li Lin",'M',"123 Beijing Road"}; //定义结构体变量a并初始化 printf("NO.:%ld\nname:%s\nsex:%c\naddress:%s\n",a.num,a.name,a.sex,a.addr); return 0; } //模仿设计描述一本书的结构体,并输出这本书的信息
时间: 2024-02-16 17:04:40 浏览: 49
好的,以下是模仿设计描述一本书的结构体,并输出这本书的信息的代码:
```c
#include <stdio.h>
struct Book { //声明结构体类型struct Book
char title[50]; //以下4行为结构体的成员
char author[50];
int pages;
float price;
}book1 = {"C语言程序设计", "谭浩强", 360, 48.8}; //定义结构体变量book1并初始化
int main() {
printf("书名:%s\n", book1.title);
printf("作者:%s\n", book1.author);
printf("页数:%d\n", book1.pages);
printf("价格:%.2f元\n", book1.price);
return 0;
}
```
输出结果为:
```
书名:C语言程序设计
作者:谭浩强
页数:360
价格:48.80元
```
相关问题
#include<stdio.h> #include<string.h> struct student { int id; int age; }; int main() { struct student stu1; struct student *p1 = &stu1; scanf("%d %d",p1->age,p1->id); printf("%d %d",p1->age,p1->id); return 0; } 这串代码哪里错了
这段代码有两处错误:
1. scanf() 函数中应该传入参数的地址,而不是参数的值。因此,应该将第 10 行的 scanf() 函数修改为:
```c
scanf("%d %d", &p1->age, &p1->id);
```
2. 在第 11 行中,应该使用箭头运算符 -> 访问结构体指针中的成员,而不是使用点运算符 .。因此,应该将第 11 行的 printf() 函数修改为:
```c
printf("%d %d", p1->age, p1->id);
```
修改后的代码如下:
```c
#include<stdio.h>
#include<string.h>
struct student {
int id;
int age;
};
int main() {
struct student stu1;
struct student *p1 = &stu1;
scanf("%d %d", &p1->age, &p1->id);
printf("%d %d", p1->age, p1->id);
return 0;
}
```
#include<stdio.h> #include<string.h> struct student{ char name[100]; int age; }; int main(void) { int i,j; struct student arr[5]; for(i=0;i<5;i++) { scanf("%s%d",arr[i].name,&arr[i].age); } for(i=0;i<4;i++) {
在代码中,最后一个for循环没有结束,缺少右括号。下面是修改后的代码:
```
#include<stdio.h>
#include<string.h>
struct student{
char name[100];
int age;
};
int main(void) {
int i,j;
struct student arr[5];
for(i=0;i<5;i++) {
scanf("%s%d",arr[i].name,&arr[i].age);
}
for(i=0;i<4;i++) {
for(j=0;j<4-i;j++) {
if(arr[j].age>arr[j+1].age) {
struct student t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
for(i=0;i<5;i++) {
printf("%s %d\n",arr[i].name,arr[i].age);
}
return 0; //需要加上返回值
} //需要加上右括号
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)