请各位大佬帮忙指点指点,感激不尽。

 和书上的代码一样,而且书上的代码还少了”#include <string.h>”我也加进去了

但为何运行结果却不能运行出年龄,生源地,录取成绩

我运行出的张三年龄为1703704   李四年龄为1703656

张三,李四生源地都为(null)

张三,李四录取成绩为 0

我用的是VC6.0

#include <stdio.h>
#include <string.h>

struct student  //声明 struct student 类型
{
	char id[9];     //学号
	char name[9];     //姓名
	int age;     //年龄
	char origin_place[10];     //生源地
	double score;     //录取成绩
};

int main()     //主函数
{
	struct student stul = {"TJUT0001","张三",18,"北京市",550.5};     //定义并初始化变量 stul
	struct student stu2;     //定义变量 stul2

	//为 stu2 的每个成员分别赋值
	strcpy (stu2.id, "TJUT0002");
	strcpy (stu2.name, "李四");
	strcpy (stu2.origin_place, "天津市");
	stu2.age = 20;
	stu2.score = 535.0;

	//按格式输出两个变量的每个成员值,使用\t 控制输出格式
	printf("学生基本信息汇总\n");
	printf("学号\t\t姓名\t\t年龄\t\t生源地\t\t录取成绩\n");
	printf("%s\t%s\t\t%d\t\t%s\t\t%.lf\n",
		stul.id,stul.name,stul.origin_place,stul.score);
	printf("%s\t%s\t\t%d\t\t%s\t\t%.lf\n",
		stu2.id,stu2.name,stu2.origin_place,stu2.score);
	return 0;
}

 

你的输出语句,少了年龄那个字段啊,你用%d占了位,但是后面的语句没有这个值,导致了输出对不上。

第三项生源地格式化用%s,不是用%d

而且你printf前面指定了五个输出项,但后面只给了四个变量,age到哪里了?,第三项应该是age的

应该是由于printf没有写对的原因 

你的参数只有4项 但是printf中的格式控制符有五项

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632