C中的变量不显示?

时间:2017-08-31 16:24:01

标签: c

我已经存储了所有变量,但出于某种原因,它们并没有累加并给我我的总数。 (每个公交车号码存储用户输入以输入学生数量)

allSuburbs= bus121+bus711+bus451+bus562+bus122+bus113+bus123+bus324+bus654;
northSuburbs= bus121+bus711+bus451;
southSuburbs= bus562+bus122;
eastSuburbs= bus113+bus123;
westSuburbs= bus324+bus654;



printf("Total students:\n",&allSuburbs);
printf("Total students from North Suburbs:\n",&northSuburbs);   
printf("Total students from South Suburbs:\n",&southSuburbs);
printf("Total students from East Suburbs:\n",&eastSuburbs);
printf("Total students from West Suburbs:\n",&westSuburbs);
return 0;
}

2 个答案:

答案 0 :(得分:6)

您缺少格式字符串以获取格式化为消息的值(printf的第一个参数)。另外,您应该注意到您可能只想打印变量而不是它们的地址 - 所以丢失了&运算符:

printf("Total students %d:\n", allSuburbs);
// Format string ------^
// No & here ------------------^

答案 1 :(得分:1)

您拨打printf

时遇到2个问题

1)您实际上并没有告诉printf您想要显示值的位置 - 您需要添加适当的格式代码

2)你传递了一个指向变量的指针

所以例如

printf("Total students:\n",&allSuburbs);

可能应该是

printf("Total students: %d\n",allSuburbs);

假设allSuburbsint