Thread: how to declare an external variable of type "enum"?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    78

    Unhappy how to declare an external variable of type "enum"?

    If I have a global integer x, which I declare in main.c and want to use in function.c, I am able to do so by stating the following in header.h:

    Code:
    extern int x;
    However, what do I do if I have a global enum? In my main.c I declare a global variable as follows:

    Code:
    enum whoseturn {B, W};
    enum whoseturn color=W;
    But how do I declare it in my header in order to use it in function.c? I have tried such things as

    Code:
    extern enum whoseturn color;
    extern whoseturn color;
    extern enum color;
    But I consistently get error messages when I try to set color to B or W, to the effect B and W have not been declared. How do I do this globally?
    Last edited by Adam Rinkleff; 06-22-2011 at 07:35 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The definition of the enum type must be visible when compiling (and remember: source code files are compiled independently of one another). This is the job header files were born to do. You can put the enum definition and the extern in the header, and any source code that needs to know about it #includes the header.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by tabstop View Post
    The definition of the enum type must be visible when compiling (and remember: source code files are compiled independently of one another). This is the job header files were born to do. You can put the enum definition and the extern in the header, and any source code that needs to know about it #includes the header.
    : O That is the correct answer, two points for tabstop. I will simply place in header.h the code that is currently in main.c

    Code:
    enum whoseturn {B, W};
    enum whoseturn color=W;

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No, that won't work. The definition of the enum type refers to only the first of those lines. The second is a definition of a variable of that type. That stays in your c file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Oh, no, that isn't working quite right after all. I added it to the header, but now I'm getting this error:

    Code:
    input.o(.data+0x0): multiple definition of `color'
    main.o(.data+0x0): first defined here
    convertfen.o(.data+0x0): multiple definition of `color'
    main.o(.data+0x0): first defined here
    I don't define it in main anymore, so it seems to be defining it everytime the header is accessed?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    This is in my header file:
    Code:
    enum whoseturn color {B, W};
    This is in my main.c:
    Code:
    color=W;
    This is my error:
    Code:
    main.c: In function `main':
    main.c:62: `color' undeclared (first use in this function)
    main.c:62: `W' undeclared (first use in this function)

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    And this is what I need in my header:
    Code:
    enum whoseturn {B, W};
    enum whoseturn color;
    Thanks everyone!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adam Rinkleff View Post
    And this is what I need in my header:
    Code:
    enum whoseturn {B, W};
    enum whoseturn color;
    Thanks everyone!
    No that most definitely isn't what you need in your header. You need the definition of the whoseturn, and an extern variable declaration. Right now you have two separate color variables, one in your main, and one in your other file.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Really? It compiled? Why do I need an extern variable declaration when I declare it in the header??

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Nope.
    Code:
    enum whoseturn {B, W};
    Goes in the header.

    Code:
    enum whoseturn color;
    or
    Code:
    enum whoseturn color=W;
    go in your .c file.

    EDIT:
    Actually it should be:
    Code:
    enum color{B, W};
    Goes in the header.

    Code:
    enum color whoseturn ;
    or
    Code:
    enum color whoseturn=W;
    Sinse logically, the enum is of two colors, whereas the variable tells who's turn it is.


    I disagree with tabstop that an extern variable is appropriate. You don't need a global variable for something like this.
    Last edited by King Mir; 06-22-2011 at 07:59 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Is this right?

    Header:
    Code:
    enum whoseturn {B, W};
    extern enum whoseturn color;
    Main (global):
    Code:
    enum whoseturn color=W;

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adam Rinkleff View Post
    Is this right?

    Header:
    Code:
    enum whoseturn {B, W};
    extern enum whoseturn color;
    Main (global):
    Code:
    enum whoseturn color=W;
    That's what you want.

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    : DDDDDDd

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    78
    Quote Originally Posted by King Mir View Post
    You don't need a global variable for something like this.
    I do because I don't really know what I'm doing, so I'm happy if it compiles, and passing variables back and forth seems awfully difficulty.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adam Rinkleff View Post
    Really? It compiled? Why do I need an extern variable declaration when I declare it in the header??
    The point of headers is that they can be included (and used) by multiple source files and potentially by other headers. If the variable is going to be shared between two source files (for example, value is set in one file and read in another) then the variable needs to be extern in the header file.

    If you are happy for each source file to have its own local version of the variable (so changes made to the value in one source file are not visible to other source files) the variable doesn't need to be extern. Things will get entertaining if your header file does not have include guards and it is included (directly or indirectly) multiple times, as the result of that is multiple definitions in the affected source files.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC lets you forward declare a struct with "class /name/"
    By Mozza314 in forum C++ Programming
    Replies: 12
    Last Post: 02-20-2011, 04:52 AM
  2. Replies: 3
    Last Post: 05-01-2010, 02:26 AM
  3. problem about handle "double" variable type? or?
    By Mathsniper in forum C Programming
    Replies: 4
    Last Post: 12-31-2006, 10:11 PM
  4. "Deciding" in runtime the type of a variable
    By mikahell in forum C++ Programming
    Replies: 28
    Last Post: 07-22-2006, 09:51 AM
  5. Ask about error when declare "static inline int"
    By ooosawaddee3 in forum C Programming
    Replies: 3
    Last Post: 05-25-2002, 05:03 AM