LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C Enum (https://www.linuxquestions.org/questions/programming-9/c-enum-367573/)

exvor 09-27-2005 02:23 PM

C Enum
 
Code:


#include<stdio.h>

int main()

{
  int east = 5;
  enum direction {
        north,
        south,
        west
      };
  enum direction points;
    points = east;

    if (points == north)
        printf("The direction is north\n");
    else
      printf("The direction is not north\n");
    return 0;
}


In an effort not to hijack another thread i created this one for a little
help understanding enum


now according to what im told/read this code should generate an error
when compiling because im assigning it something thats not enum'ed
at least a warning. This is not so. Gcc gives no error or warning not even
with -Wall


so is this a leagal use of enum or should i be getting a warning ?

kjordan 09-27-2005 02:41 PM

Yes, that's legal. If you define something with an enum type, the compiler makes it an integer so you can have any value assigned to it, not just those in your enum statement.

exvor 09-27-2005 02:44 PM

then whats the point in using it ? why not just declare regular varibles.

kjordan 09-27-2005 02:51 PM

The advantage of an enum is that it has scope, compared to a #define statement. You could just use regular variables, but that advantage to this is it automatically numbers them for you so you don't have to go through the process of doing that.


All times are GMT -5. The time now is 10:50 PM.