error with the declaration of enum
I have a 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 开发者_JAVA百科(first use in this function)
tryit4.c:8: error: expected ‘;’ before ‘bl1’
tryit4.c:9: error: ‘bl1’ undeclared (first use in this function)
I don't see any reason for it. Can you please explain what could be the reason for it?
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.
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;
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.
It would really be a good idea to define your enum like this:
typedef enum {
False,
True,
} boolean;
A couple of reasons:
true
andfalse
(lowercase) are likely reserved words- false being 1 and true being 0 can cause you logic problems later
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;
}
Like previous answers demonstrate, use typedef:
typedef enum { true, false } boolean;
From FAQ - A list of features that C++ supports which C does not includes:
bool keyword
That FAQ is a little inaccurate, and is better stated as "a list of features that C++ supports which C89 does not include"
Add #include <stdbool.h>
to your code and it will compile as C99 on a compiler that attempts to implement C99 (such as gcc).
精彩评论