char s ; int("--%c",&s);意思是什么
时间: 2024-06-05 07:06:37 浏览: 122
Null is a term used in computer programming and databases to represent an absence of value or a lack of data. It is often used to indicate that a variable or field has not been assigned a value or that a database record does not contain any data for a particular field. In programming, null is typically represented as a special value or keyword, such as "null" or "None", depending on the programming language.
相关问题
#include <stdio.h> #include <stdlib.h> struct student { int num; char name[10]; int c,oop,db; float avg; }; void main() { int i; struct student s,s1,s2; for(i=1;i<3;i++) { printf("请输入第%d个学生的信息:\n",i); scanf("%d%s%d%d%d",&s.num,s.name,&s.c,&s.oop,&s.db); s.avg = (float)(s.c+s.oop+s.db)/3; if(i=1) s1=s; else s2=s; } if(s1.avg<s2.avg) { s=s1; s1=s2; s2=s; } printf("\n学号 姓名 c语言 OOP 数据库 平均成绩\n"); printf("%-10d%-10s%-8d%-8d%-8d%-6.1f\n",s1.num,s1.name,s1.c,s1.oop,s1.db,s1.avg); printf("%-10d%-10s%-6d%-6d%-6d%-6.1f\n",s2.num,s2.name,s2.c,s2.oop,s2.db,s2.avg); }哪里错了
在 if 语句中,判断 s1 和 s2 平均成绩大小的时候,应该使用比较运算符“<”,而不是赋值运算符“=”,所以程序会出现逻辑错误。正确的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
struct student {
int num;
char name[10];
int c,oop,db;
float avg;
};
void main() {
int i;
struct student s,s1,s2;
for(i=1;i<3;i++)
{
printf("请输入第%d个学生的信息:\n",i);
scanf("%d%s%d%d%d",&s.num,s.name,&s.c,&s.oop,&s.db);
s.avg = (float)(s.c+s.oop+s.db)/3;
if(i==1)
s1=s;
else
s2=s;
}
if(s1.avg<s2.avg)
{
s=s1;
s1=s2;
s2=s;
}
printf("\n学号 姓名 c语言 OOP 数据库 平均成绩\n");
printf("%-10d%-10s%-8d%-8d%-8d%-6.1f\n",s1.num,s1.name,s1.c,s1.oop,s1.db,s1.avg);
printf("%-10d%-10s%-6d%-6d%-6d%-6.1f\n",s2.num,s2.name,s2.c,s2.oop,s2.db,s2.avg);
}
```
这个代码在写入性别时程序错误,请修改成能正确运行的#include <stdio.h> struct student { int num; char name[10]; char gender; int c, oop, db; float avg; }; int main() { int i, j; struct student s[5], temp; for (i = 0; i < 5; i++) { printf("请输入第%d个学生的信息,学号,姓名,性别,C语言,oop ,数据库:\n", i + 1); scanf("%d%s %c%d%d%d", &s[i].num, s[i].name, &s[i].gender, &s[i].c, &s[i].oop, &s[i].db); s[i].avg = (float)(s[i].c + s[i].oop + s[i].db) / 3.0; } for (i = 0; i < 4; i++) { for (j = 0; j < 4 - i; j++) { if (s[j].avg < s[j+1].avg) { temp = s[j]; s[j] = s[j+1]; s[j+1] = temp; } } } printf("\n学号 姓名 性别 c语言 oop 数据库 平均成绩\n"); for (i = 0; i < 5; i++) { printf("%-10d%-10s%-8c%-8d%-8d%-8d%-6.1f\n", s[i].num, s[i].name, s[i].gender, s[i].c, s[i].oop, s[i].db, s[i].avg); } return 0; }
在 scanf 函数中,读取字符类型时应该使用 %c,而不是 %s。因此,将代码中的第 16 行修改为:
scanf("%d%s %c%d%d%d", &s[i].num, s[i].name, &s[i].gender, &s[i].c, &s[i].oop, &s[i].db);
修改后的完整代码如下:
#include <stdio.h>
struct student {
int num;
char name[10];
char gender;
int c, oop, db;
float avg;
};
int main() {
int i, j;
struct student s[5], temp;
for (i = 0; i < 5; i++) {
printf("请输入第%d个学生的信息,学号,姓名,性别,C语言,oop ,数据库:\n", i + 1);
scanf("%d%s %c%d%d%d", &s[i].num, s[i].name, &s[i].gender, &s[i].c, &s[i].oop, &s[i].db);
s[i].avg = (float)(s[i].c + s[i].oop + s[i].db) / 3.0;
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 4 - i; j++) {
if (s[j].avg < s[j+1].avg) {
temp = s[j];
s[j] = s[j+1];
s[j+1] = temp;
}
}
}
printf("\n学号 姓名 性别 c语言 oop 数据库 平均成绩\n");
for (i = 0; i < 5; i++) {
printf("%-10d%-10s%-8c%-8d%-8d%-8d%-6.1f\n", s[i].num, s[i].name, s[i].gender, s[i].c, s[i].oop, s[i].db, s[i].avg);
}
return 0;
}
阅读全文