Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单程序赢得';不能用C语言编译_C_Gcc_Compiler Construction - Fatal编程技术网

简单程序赢得';不能用C语言编译

简单程序赢得';不能用C语言编译,c,gcc,compiler-construction,C,Gcc,Compiler Construction,好吧,我马上就知道这将是一个愚蠢的问题,但我不明白为什么这个简单的C程序没有编译 #include <stdio.h> #include <stdlib.h> typdef struct CELL *LIST; struct CELL { int element; LIST next; }; main() { struct CELL *value; printf("Hello, World!"); } 在主函数中添加一个返回值主要原因是您

好吧,我马上就知道这将是一个愚蠢的问题,但我不明白为什么这个简单的C程序没有编译

#include <stdio.h>
#include <stdlib.h>
typdef struct CELL *LIST;
struct CELL {
    int element;
    LIST next;
};
main() {
    struct CELL *value;
    printf("Hello, World!");
}

在主函数中添加一个返回值

主要原因是您键入了
typedef
作为
typedef
。但是,您还应该做以下几件事:

  • 添加
    返回0
    main()
    的末尾
  • main
    的签名更改为
    int main(void)

最重要的是:您拼错了typedef

然后,至少现在,我们通常会在main中添加一个返回类型,如下所示:

int main()
此外,main应该返回退出状态,因此:

#include <stdio.h>
#include <stdlib.h>

typedef struct CELL *LIST;

struct CELL {
  int element;
  LIST next;
};

int main() {
  struct CELL *value;
  printf("Hello, World!\n");
  return 0;
}
#包括
#包括
类型定义结构单元*列表;
结构单元{
int元素;
下一个列表;
};
int main(){
结构单元*值;
printf(“你好,世界!\n”);
返回0;
}

您是否尝试使用
gcc-Wall-g yourprog.c-o yourbinary
编译它

我得到:

yourprog.c:3:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
yourprog.c:6:5: error: unknown type name 'LIST'
yourprog.c:8:1: warning: return type defaults to 'int' [-Wreturn-type]
yourprog.c: In function 'main':
yourprog.c:9:18: warning: unused variable 'value' [-Wunused-variable]
yourprog.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
并且您错贴了
typedef
,您应该更改
main
的签名并添加一个
返回0内部

顺便说一下,我发现你的
typedef
品味很差。我建议编写类似于
typedef-struct-CELL-CELL-CELL-t
的代码,并声明
CELL-t*value=NULL。
因为您确实想记住
value
是指向
CELL-t
的指针。特别是,我讨厌typedef-s,比如
typedef结构单元*CELL\u ptr
实际上我宁愿建议

 struct cell_st;
 typedef struct cell_st cell_t;
 cell_t *value = NULL;

(我确实喜欢将所有指针初始化为NULL)。

您是否尝试使用
gcc-Wall-g yourprog.c-o yourbinary
编译它?一般来说,阅读错误消息是找出原因的最佳方法。如果您需要我们的帮助,也许您应该发布错误消息。实际上我正准备这样做。虽然拥有它确实是一个好主意,但代码实际上将在没有返回的情况下编译。@perh:不是没有发出两个警告(使用
-Wall
)@ArmenTsirunyan,我从未在C规范中看到过这一点,但是GCC对C代码抱怨它,所以我认为是这样。我知道这是一个隐式
返回0从main中,如果你在C++中省略。@ ARMTENSIRYNYN,在C11中查看,在5.1.2.2.3/1中它表示:……到达终止main函数的}返回0的值…如果是这样的话,我不知道为什么GCC在C中对它挑剔位于main()函数的终止大括号之前。C89没有。在C89中,需要声明;在C99和C11中,这是一个很好的形式。@pmg,谢谢,这可以清除它。如果您碰巧知道,LWS是否使用C89?我对C没有经验,但有时它似乎更老了。@chris:你可以使用标准宏来识别正在使用的版本。看见对于C2011(我讨厌称之为C11),
\uuu STDC\u VERSION\uuuu
201112L
。是的,我真的很愚蠢,没有意识到我拼错了typedef。我觉得自己像个十足的白痴。这是一个这样的时刻,当你写东西,盯着它看了几个小时,其他人走了过来,看了一秒钟,立即看到了它。非常感谢@MZimmerman6,我在GCC上得到了这个:错误:“typdef”没有命名类型。这是不言自明的;)GCC没有对我说这些。不知道为什么
 struct cell_st;
 typedef struct cell_st cell_t;
 cell_t *value = NULL;