[C]bash:syntax error near unexpected token `('

数组计算平均数,这块代码反复看都看不出问题。输出 bash:syntax error near unexpected token `('

#include <stdio.h>
int main(void)
{
    int grades[10];
    unsigned int count = 10;
    long sum = 0L; 
    float average = 0.0f;

    //printf("\nEnter the number of people in your class:");
    //scanf("%u", &count);

    for(unsigned int i = 0 ; i < count ; ++i)
    {
        printf("The grade of class number %2u is", i+1);
        scanf("%d", &grades[i]);
        sum += grades[i];
    }
    
    average = (float)sum/count;

    printf("The average grade of the class is: %.2f", average);
    return 0;
}
阅读 2.5k
1 个回答

不是代码的问题, 编译后, 你是怎么执行的?

我猜你是没有编译, 直接按脚本去执行了
你需要把代码保存 c 文件,如 test.c
然后用 gcc 编译

$ gcc test.c

会在当前文件夹下生成一个a.out的可执行文件

执行

$ ./a.out

会得到你要的结果.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进