Global listing listing

gcc 4.4.4 c89

I have the following in state.c file:

enum State { IDLE_ST, START_ST, RUNNING_ST, STOPPED_ST, }; State g_current_state = State.IDLE_ST; 

When I try and compile, I get the following error.

 error: expected '=', ',', ';', 'asm' or '__attribute__' before 'g_current_state' 

Are there some with a variable declaration of enum type in the global scope?

Thanks so much for any suggestions,

+6
c
source share
4 answers

There are two ways to do this in direct C. Either use the full enum name everywhere:

 enum State { IDLE_ST, START_ST, RUNNING_ST, STOPPED_ST, }; enum State g_current_state = IDLE_ST; 

or (this is my preference) typedef it:

 typedef enum { IDLE_ST, START_ST, RUNNING_ST, STOPPED_ST, } State; State g_current_state = IDLE_ST; 

I prefer the second one, as it makes the type look like the first class, like int .

+19
source share

Missing semicolon after closing enum bracket. By the way, I really don’t understand why semicolon errors are so mysterious in gcc.

+2
source share

State alone is not a valid identifier in your fragment.

You need enum State or type t enum State in a different name.

 enum State { IDLE_ST, START_ST, RUNNING_ST, STOPPED_ST, }; /* State g_current_state = State.IDLE_ST; */ /* no State here either ---^^^^^^ */ enum State g_current_state = IDLE_ST; 

/* or */

 typedef enum State TypedefState; TypedefState variable = IDLE_ST; 
+2
source share

So there are 2 problems:

  • Absent ; after enum .
  • When declaring a variable, use enum State instead of just State .

It works:

 enum State { IDLE_ST, START_ST, RUNNING_ST, STOPPED_ST, }; enum State g_current_state; 
+2
source share

All Articles