c - 迭代程序

标签 c loops iteration

我写了下面的代码:

#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/

相关文章:

c - C语言中如何遍历二维数组对角线?

C X11 MandelBrot 集应用中的段错误

c - Linux TCP 服务器 : reading client's IP address before accepting connection

c - ruby C 扩展 : turning int function into ruby value

python - python中通过键迭代字典多个值

javascript - 使用 jquery 遍历列表

loops - ffmpeg:保存重复 3 次的 mp3 部分

loops - 编写一个 MIPS 程序来测试一个数是否是 2 的幂

python - 列操作 R

objective-c - 通过 arraycontroller 获取选中的复选框列表