Jump to content

Use of enum variables in .h and .c

Recommended Posts

Hi. Got stuck.

I have a project with a line in variables.h:

enum IOchannel {V1 = 11, I1 = 101, V2 = 12, I2 = 102};

Then I have an input.c, where I initialize all variables declared in variables.h(as extern), however, I don't use it for my enum, because I would get an error that my variables are already defined (due to include).

Finally, I have another file which sends on LCD my enum variables, i.e. if it was V1 received, then you'll see "V1 = xxx" on the LCD.


where the function is void lcd_send_string(char *s);

and so there's a function

char *enum2str(enum IOchannel enumVariable)


// convert ENUM to CHAR

switch (enumVariable)

{case V1:

return "V1";

case V2:

return "V2";

case I1:

return "I1";

case I2:

return "I2";}


When I am trying to compile my code (GCC 4.2.4) I receive a message

"argument of type "enum IOchannel" is incompatible with parameter of type "enum IOchannel""

every time I am trying to use my function.


What is the correct way to declare, initialize and use my enum variable? Thanks.

Link to post
Share on other sites

Welcome to 43oh.


What is the "haltIn" type. Can you type type cast or initialize it to the enum.

The below works for me. Try to use typedef, to avoid tping "enum IOChannel" everytime.

typedef enum {V1 = 11, I1 = 101, V2 = 12, I2 = 102}IOchannel;

char *enum2str(IOchannel enumVariable)
      case V1:
        return "V1";
        return "I1";

 * Main program.
int main(void)
   IOchannel haltIn = V1;


Link to post
Share on other sites

Thank you.

I am sorry for my late answer - I tried to change the code and compile it, but due to other errors couldn't do that. Anyway, I think that my problem was also related to code writing rather then enum mistakes: I was able today to compile the code.

My lcd_send_string(* char) function was declared in a functions.h file as following:

char *enum2str(enum IOchannel);

#ifdef LCD
	void lcd_init(void);
	void lcd_send_string(char *s);

while LCD was defined in another definitions.h file:

#define         LCD            LCD4

I had to comment #ifdef and #endif, and then everything compiled succesfully, including that function with enums: lcd_send_string(enum2str(haltIn)).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...