enum question

This is a discussion on enum question within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> main() { enum { test, anothertest } man, animal; printf("%d %d" , test, anothertest); } OUTPUT: 0 ...

  1. #1
    Unregistered
    Guest

    enum question

    Code:
    #include <stdio.h>
    
    main()
    {
    	enum { test, anothertest } man, animal;
    	
    	printf("%d %d" , test, anothertest);
    
    }
    OUTPUT:
    0 1

    Code:
    #include <stdio.h>
    
    main()
    {
    	enum { test, anothertest } man, animal;
    	
    	printf("%d %d" , man, anothertest);
    
    }
    OUTPUT:
    1 1

    Code:
    #include <stdio.h>
    
    main()
    {
    	enum { test, anothertest } man, animal;
    	
    	printf("%d %d" , man, animal);
    
    }
    OUTPUT:
    1 8005096
    what happened so suddenly?

    what does enum { test, anothertest } man, animal; do?
    what values do man, animal have ?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >what values do man, animal have?
    Random. You need to assign it a value before you use it, this is why you are getting varied results.

    >enum { test, anothertest } man, animal;
    Here,
    - test=0
    - anothertest=1
    - man and animal are not yet assigned values.

    On my compiler, I get a warning like this:
    Possible use of 'man' before definition in function main
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest

    but...

    ok, so enum doesn't initialize variables outside the brackets.

    But if I say, enum { test, anothertest } man, animal
    how is it different from
    enum { test, anothertest };
    enum {man, animal};
    ?

  4. #4
    Unregistered
    Guest

    or

    or even
    enum { test, anothertest, man, animal }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    With this:
    >enum { test, anothertest } man, animal
    the names within the brackets are enumeration constants, a list of constant integer values, starting at 0, incrementing by 1 (there are other ways, but these are not covered by your example).

    The names on the right side, define variables that are capable of holding one of the enum's constant values.

    Your example isn't the best to work from, have a look at this instead:
    Code:
    #include <stdio.h>
    
    enum colours { RED, BLUE, GREEN, ORANGE};
    
    int main(void)
    {	
    	enum colours MyColour;
    	
    	MyColour = RED;
    	
    	printf("MyColour is %d\n", MyColour);
    
    	MyColour = ORANGE;
    	
    	printf("MyColour is %d\n", MyColour);
    	
    	return 0;
    }
    Here we declare a enum called colours.
    Then we define an "enum colours" variable called MyColour.
    Then we assign it some values and print it to show the user.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Unregistered
    Guest
    Hammer has the right idea. You should look at what he has provided. Enumerations are straight forward. Do not make it more complicated than it is.

    Mr. C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Question on Enum
    By markcls in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 08:19 AM
  3. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM
  4. Enum question
    By Bill 101 in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 11:33 PM
  5. enum question
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21