#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:45 浏览: 11
好的,以下是模仿以上代码描述一本书的结构体,并输出这本书的信息的代码:
```c
#include <stdio.h>
struct Book { //声明结构体类型struct Book
char title[50]; //以下4行为结构体的成员
char author[50];
float price;
int pages;
}book1 = {"C语言程序设计", "谭浩强", 48.8, 360}; //定义结构体变量book1并初始化
int main() {
printf("书名:%s\n", book1.title);
printf("作者:%s\n", book1.author);
printf("价格:%.2f元\n", book1.price);
printf("页数:%d页\n", book1.pages);
return 0;
}
```
输出结果为:
```
书名:C语言程序设计
作者:谭浩强
价格:48.80元
页数:360页
```
相关问题
#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; } //模仿设计描述一本书的结构体,并输出这本书的信息
好的,以下是模仿设计描述一本书的结构体,并输出这本书的信息的代码:
```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;
}
```