-1

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’
MZimmerman6
  • 8,445
  • 10
  • 40
  • 70

4 Answers4

2

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)
chris
  • 60,560
  • 13
  • 143
  • 205
  • @ArmenTsirunyan, I've never looked at that in the C spec, but GCC complains about it for C code, so I presume it is. I'm aware that it's an implicit `return 0;` from main if you leave it out in C++. – chris Oct 13 '12 at 22:47
  • @ArmenTsirunyan, Looking at C11, in 5.1.2.2.3/1 it says: *...reaching the } that terminates the main function returns a value of 0. ...* I'm not sure why GCC is picky about it in C if that's the case. – chris Oct 13 '12 at 22:51
  • Both C99 and C11 have an implicit `return 0;` right before the terminating brace of the main() function. C89 doesn't. In C89 the statement is required; in C99 and C11 it is good-form. – pmg Oct 13 '12 at 22:53
  • @pmg, Thanks, that clears it up. If you happen to know, does LWS use C89? I'm not experienced with C, but it seems as if it was older sometimes. – chris Oct 13 '12 at 23:41
  • @chris: you can use Standard macros to identify the version in use. See [this answer](http://stackoverflow.com/a/2115886/25324). For C2011 (I hate to call it C11) the `__STDC_VERSION__` is `201112L`. – pmg Oct 14 '12 at 09:53
  • @pmg, Well, after trying that and looking around a bit when it wasn't defined at all, I guess LWS has no -std= flag passed in, so early C90 or thereabouts. – chris Oct 14 '12 at 14:50
2

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;
}
perh
  • 1,668
  • 11
  • 14
  • Yeah I am really stupid and did not realize I was misspelling typedef. I feel like a complete idiot. It is one of those times when you write something and stare at it for hours, and someone else comes along, looks at it for a second and immediately see it. Thanks a lot! – MZimmerman6 Oct 13 '12 at 23:39
  • @MZimmerman6, I got this on GCC: *error: 'typdef' does not name a type*. That's pretty self-explanatory ;) – chris Oct 13 '12 at 23:43
  • GCC did not say that to me. Don't know why – MZimmerman6 Oct 14 '12 at 00:08
2

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).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Add a return in your main function

JulienITARD
  • 137
  • 2
  • 10