我写了下面的代码:
#include <stdio.h>
/* max_number.c: outputs the largest number of five numbers entered */
void main(void) {
int i, num, max;
for (i = 1; i <= 5; i++) {
printf("Enter a number: ");
scanf("%d", &num);
if (num >= max)
max = num;
}
printf("The maximum number is %d\n", max);
}
当我用任何类型的数据运行程序时,我不断得到“最大数量是 14”。有人可以指出我做错的方向吗?谢谢!
最佳答案
变量 max
未初始化。
尝试
int i, num, max = INT_MIN;
关于c - 迭代程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953095/