【问题标题】:Compiling in C Programming在 C 编程中编译
【发布时间】:2015-02-18 11:22:34
【问题描述】:

我在编译最近启动的名为 format 的程序时遇到问题。我收到以下错误:

format.c:7:1: warning: return type defaults to ‘int’ [enabled by default]
format.c: In function ‘main’:
format.c:11:27: error: expected expression before ‘!=’ token
format.c:14:10: warning: missing terminating " character [enabled by default]
format.c:14:3: error: missing terminating " character
format.c:16:1: error: expected expression before ‘}’ token
format.c:16:1: error: expected ‘;’ before ‘}’ token
format.c:9:10: warning: variable ‘n1’ set but not used [-Wunused-but-set-variable]

当我使用 gcc -Wall -pedantic -std=c99 -O2 -o format format.c 编译时

#include <stdio.h>

main()
 { 
  int c, n1;
  n1 = 0;
  while ((c + getchar())) !=EOF
     if 
(c == 40'\n')
     ++n1;
      printf("%d\",n1);

}

【问题讨论】:

  • 你的代码真的是这样格式化的吗?
  • 拜托,int main(void)return 0
  • 作为初学者,不要一开始就用-O2代替gcc。使用gcc -Wall -Wextra -g 编译。了解如何使用调试器 (gdb)。只有当您的程序没有错误时,您才能使用 -O2 重新编译它(例如,用于基准测试)。

标签: c compilation


【解决方案1】:

我认为这是您正在尝试的:

#include <stdio.h>

main()
{
    int c, n1 = 0;
    c = getchar();
    while (c != EOF){
        if(c == 40 || c == '\n')
            ++n1;
        printf("%d",n1);
        c = getchar();
    }
}

【讨论】:

  • 我是 C 的新手,正在从事一个项目,并从教科书中获得了该代码。当我输入其他代码时仍然收到编译错误。
【解决方案2】:
#include <stdio.h>

int
main(void)
  {
    int c, n1;
    n1 = 0;
    while ((c = getchar()) !=EOF)
        if
(c == '\n')
        ++n1;
        printf("%d\n", n1);

}

【讨论】:

    猜你喜欢
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    相关资源
    最近更新 更多