简体   繁体   中英

Simple program won't compile in C

Okay I know right off the bat this is going to be a stupid question, but I am not seeing why this simple C program is not compiling.

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

I am new to C programming, not to programming in general, but to C. I am familiar with Objective-C, Java, Matlab, and a few others, but for some reason I can not figure this one out. I am trying to compile it using GCC in OS X if that makes a difference. Thanks for your help!

The error message I am getting is

functionTest.c:5: error: expected specifier-qualifier-list before ‘LIST’
functionTest.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’

The main reason is that you typoed typedef as typdef . However, there are a couple other things you should do:

  • Add return 0; to the end of main() .
  • Change the signature of main to int main(void)

Most importantly: You have misspelled typedef.

Then, at least these days, we normally add a return type to main, like so:

int main()

Also, main is supposed to return the exit status, so:

#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;
}

Did you try to compile it with gcc -Wall -g yourprog.c -o yourbinary ?

I'm getting:

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]

and you mispelled typedef and you should change the signature of main and add a return 0; inside.

By the way, I find your typedef very poor taste. I suggest to code (like Gtk does) something like typedef struct CELL CELL_t and declare CELL_t* value = NULL. because you really want to remember that value is a pointer to CELL_t . In particular, I hate typedef-s like typedef struct CELL* CELL_ptr; because I find very imporrtant (for readability reasons) to quickly understand what is a pointer and what is not a pointer.

Actually I would rather suggest

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

(I do like initializing all pointers to NULL).

在主函数中添加收益

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM