我的程序:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
struct student s={1, 'heeena', 30};
printf("ID is %d\n name is %s\n percentage is %f",s.id,s.name,s.percentage);
return(0);
}
上述程序的输出为:
ID is 1
name is a...
percentage is 0.000000
代替...
的是我无法在此处绘制的非ASCII字符。如何更正程序,使其显示如下所示的结构的预期值?
ID is 1
name is heeena
percentage is 30
我正在 Ubuntu 12.04 下使用 gcc 编译它
最佳答案
这里的警告是
character constant too long
在 C 和 C++ 中,字符代码是使用单引号编写的,例如'a' 给出字母 'a' 的 ASCII 代码 (67),'\n' 给出换行符的 ASCII 代码 (10)。如果使用单引号括住多个字符,则会出现此错误。
因此替换以下行
struct student s={1, 'heeena', 30};
与
struct student s={1, "heeena", 30};
关于c - 除第一个之外,结构成员的值无法正确打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20917226/