3

I wanted to use a boolean variable in c as a flag,within a structure,but c does not have any keyword "bool" to make it possible. I got some relevant information here : Using boolean values in C then basically,I tried this

struct bookshop
{
    char name[20];
    char issuer[20];
    int id;
    typedef enum { false, true } flag;

};

to get the following error,on this line:"typedef enum { false, true } flag"; Multiple markers at this line - expected specifier-qualifier-list before ‘typedef’ - Type 'flag' could not be resolved - Syntax error

please help! and thanks in advance :)

Community
  • 1
  • 1
Amit Kumar
  • 93
  • 1
  • 2
  • 6

4 Answers4

5

You can't put a typedef inside of a struct definition like that. It needs to be at the global level.

typedef enum { false, true } bool;

struct bookshop
{
    char name[20];
    char issuer[20];
    int id;
    bool flag;
};

If you have stdbool.h available, you can just include that and it will provide the bool type as well as the true and false constants.

dbush
  • 205,898
  • 23
  • 218
  • 273
1

You cannot define a new type in a declaration. You first have to declare the bool typedef and then you can use it your struct, i.e.:

typedef enum { false, true } bool;

struct bookshop
{
    char name[20];
    char issuer[20];
    int id;
    bool flag;
};
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
1

If you just want to declare the variable of type enum, inside the structure definition you should not use typedef.

The typedef is used to define a type, a user defined type and that's not what you want, you want to declare a variable which in turn is a member of the structure, so

struct bookshop
{
    char name[20];
    char issuer[20];
    int id;
    enum { false, true } flag;    
};

should do it.

Also note, that there is a standard header stdbool.h so that you can do this

struct bookshop
{
    char name[20];
    char issuer[20];
    int id;
    bool flag;    
};
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Note in the first alternative that the `true` and `false` enumeration constants are defined, but no other variable of that type can ever be defined because there is neither an enumeration tag name nor a typedef for it. I was debating whether to add an answer about that; now I don't have to. Also, in the original, if the `typedef` had been allowed, it would have introduced a type name `flag` and would not have created a member at all. It actually isn't allowed, so that isn't a problem in practice. – Jonathan Leffler Oct 17 '16 at 17:58
0

Traditionally, the int type was used to hold 'boolean' values: true for non zero values, and false for zero ones.

Since C99, you could use the macros and values defined in stdbool.h:

bool _Bool

true integer constant 1

false integer constant 0

__bool_true_false_are_defined integer constant 1

But be aware of compatibility with old code, and the way standard C tests values as true or false. As explained before, following code:

int flag = 4;

if(flag) {
     puts("true\n");
}
else {
     puts("false\n");
}

will print true.