Error listing listing

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 a variable like enum . but it gives the 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 see no reason for this. Could you explain what could be causing this?

+3
source share
7 answers

When you declare enum boolean { true, false }, you declare a type with a name enum boolean. This is the name you will need to use after this declaration: enum booleanand not just boolean.

If you need a shorter name (e.g. just boolean), you will need to define it as an alias for the original full name

typedef enum boolean boolean;

, enum boolean, boolean

typedef enum boolean { true, false } boolean;
+7

C ( , ) : . struct, union enum :

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

, , . enum. , typedef

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

. , .

+10

enum boolean, . typedef, enum boolean b1 = foo(); .

+7

:

typedef enum {
  False,
  True,
} boolean;

:

  • true false ( ) , ,
  • false being 1 true 0
+7

, .

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

:
* true false C
* true false C, zero false, - true. :

int found = (a == b);

</" > : 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;
}
+3

, typedef:

typedef enum { true, false } boolean;
+1

From the FAQ - a list of features supported by C ++ that do not include:

bool keyword

This FAQ is a little inaccurate and is better listed as a "list of features that C ++ supports that does not contain C89"

Add #include <stdbool.h>to your code and it will compile as C99 in the compiler that is trying to implement C99 (e.g. gcc).

0
source

Source: https://habr.com/ru/post/1725750/


All Articles