Thread: enum declaration

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    Question enum declaration

    Code:
    #include<stdio.h>
    
    typedef enum {true,false} boolean;
    
    boolean test(void)
    {
            return false;
    }
    
    int main(void)
    {
        
        if(test())
        printf("the test is true\n");
        else
        printf("the test is false");
      
        return 0;
    }
    Code:
    #include<stdio.h>
    
    typedef enum {false,true} boolean;
    
    boolean test(void)
    {
            return false;
    }
    
    int main(void)
    {
        
        if(test())
        printf("the test is true\n");
        else
        printf("the test is false");
      
        return 0;
    }
    why we get deferent output when we change the order of true/false??

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Because enums are just ints at the low level, similar to using #define.

    The first value of an enum is 0. The next is 1, and so on.

    In C, boolean expressions are resolve to be always true for any number, except 0, in which case it will be resolved as false.

    So when you gave test() to the if statement, it was resolved as the int that the enum value "false" represents. In your first example, that is 1, which the if statement will resolve as true. In your second example, the number returned is 0, which will be resolved by the if statement as false.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Because the C compiler does not understand english, and has no implicit representation for "true" or "false"; it only knows expressions that evaluate to zero to be "false" and non-zero to be "true". So the condition
    Code:
     if(test())
    will pass if test() evaluates to non-zero.
    Enumeration types if not explicitly given a value (i.e. false = value) will be assigned one starting at the next lowest integer value before the preceding type (if its the first in the list, then it will assume the value 0), so the statement:
    Code:
    enum {false, true}
    will give "false" the value 0, "true" the value 1, and would continue upwards had you listed more types. So when you switch the order of declaration to {true, false} then true receives the value 0 instead. Thus in the first scenerio, test() evaluates to "false" which is equal to 1 and so "true" is printed. In the latter scenerio, test() evaluates to "false" which is equal to 0 and so "false" is printed.

    p.s. what up with the new mandatory code tags??... frustrating.
    Edit: lol, slow on the draw again!
    Last edited by @nthony; 04-30-2007 at 09:34 AM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    The first value of an enum is 0. The next is 1, and so on.
    This is the part that I didn't know about it.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by redche View Post
    Code:
    typedef enum {true,false} boolean;
    Defining true to be false, and false to be true? I think I'll use this in a future obfuscated program. I like it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM