tags:

views:

301

answers:

6

hi! every body ,here is very simple c code:

         #include<stdio.h>
        int main()
        {
          enum boolean{true,false};
          boolean bl=false;
          if(bl==false)
             printf("This is the false value of boool\n");
         boolean bl1=true;
          if(bl1==true)
           {
            printf("This is the true value of boool\n");
           }
    return 0;
   }

i was just trying to use enum type variable .but it is giving following error:

tryit4.c:5: error: ‘boolean’ undeclared (first use in this function)
tryit4.c:5: error: (Each undeclared identifier is reported only once
tryit4.c:5: error: for each function it appears in.)
tryit4.c:5: error: expected ‘;’ before ‘bl’
tryit4.c:6: error: ‘bl’ undeclared (first use in this function)
tryit4.c:8: error: expected ‘;’ before ‘bl1’
tryit4.c:9: error: ‘bl1’ undeclared (first use in this function)

i dont see any reason for it,can you plz explain,what could be the reason for it!! Thanks in advance!!

+7  A: 

You have to declare the variables to be of type enum boolean, not just boolean. Use typedef, if you find writing enum boolean b1 = foo(); cumbersome.

Tadeusz A. Kadłubowski
+8  A: 

In C, there are two (actually more, but i keep it at this) kind of namespaces: Ordinary identifiers, and tag identifiers. A struct, union or enum declaration introduces a tag identifier:

enum boolean { true, false };
enum boolean bl = false;

The namespace from which the identifier is chosen is specified by the syntax around. Here, it is prepended by a enum. If you want to introduce an ordinary identifier, put it inside a typedef declaration

typedef enum { true, false } boolean;
boolean bl = false;

Ordinary identifiers don't need special syntax. You may declare a tag and ordinary one too, if you like.

Johannes Schaub - litb
what would you say about the declaration style given for enum on page 39 in kernigham and richie.ex enum boolean {yes,no}.
mawia
oh thanks ,i hv got it!!
mawia
@mawia: in `enum boolean {yes, no}` the name `boolean` is being used as a tag identifier. In `typedef enum {true, false} boolean`, the name `boolean` is being used as an ordinary identifier. It's a tag identifier if it comes right after `enum` or `struct` or `union`.
benzado
+3  A: 

You declare the enum, but not the type. What you want is

typedef enum{false, true} boolean;  // false = 0 is expected by most programmers

There are still multiple problems with this:
* true and false are reserved words in many C compilers
* explicitly using true and false goes against the general practice of Boolean expressions in C, where zero means false and anything non-zero means true. For example:

int found = (a == b);


Edit: This works with gcc 4.1.2:

[wally@zf ~]$ ./a.out
This is the false value of boool
This is the true value of boool
[wally@zf ~]$ cat t2.c
#include<stdio.h>
int main()
{
        typedef enum {true,false} boolean;
        boolean bl=false;
        if(bl==false)
                printf("This is the false value of boool\n");
        boolean bl1=true;
        if(bl1==true)
        {
                printf("This is the true value of boool\n");
        }
        return 0;
}
wallyk
The first issue can be worked-around by adding underscore prefix, or something.
Tadeusz A. Kadłubowski
thanks for reply,hey unfortunately problem persisted even after typedefining the enum.
mawia
yeah prog compiled perfectly on typedef like typedef enum{false,true} boolean;but what would you say to the style given on page 39 of kernigham and richie of declaring an enum!
mawia
+6  A: 

It would really be a good idea to define your enum like this:

typedef enum {
  False,
  True,
} boolean;

A couple of reasons:

  • true and false (lowercase) are likely reserved words
  • false being 1 and true being 0 can cause you logic problems later
chrisbtoo
+1 for catching the problem with defining them in the other order.
qid
+1  A: 

Like previous answers demonstrate, use typedef:

typedef enum { true, false } boolean;
rmn
If there are previous answers, to what benefit is there adding a duplicate answer?
GMan
+3  A: 

When you declare enum boolean { true, false }, you declare a type called enum boolean. That the name you'll have to use after that declaration: enum boolean, not just boolean.

If you want a shorter name (like just boolean), you'll have to define it as an alias for the original full name

typedef enum boolean boolean;

If you wish, you can declare both the enum boolean type and the boolean alias on one declaration

typedef enum boolean { true, false } boolean;
AndreyT