7

I'm trying to make a function with return type as boolean...the syntax of the program seems to be correct but the compiler is giving errors....

The header files I've included are:

#include<stdio.h>
#include<stdlib.h>

the function I've created is:

34.bool checknull(struct node* node){
35.    if ( node != NULL )
36.        return TRUE;
37.       
38.    return false;
39.}

and what I'm getting at compile time is

bininsertion.c:34:1: error: unknown type name ‘bool’
bininsertion.c: In function ‘checknull’:
bininsertion.c:36:10: error: ‘TRUE’ undeclared (first use in this function)
bininsertion.c:36:10: note: each undeclared identifier is reported only once for each  function it appears in
bininsertion.c:38:9: error: ‘false’ undeclared (first use in this function)

I've tried "TRUE,false" both in small and capital letters but doesn't seem to work...

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Sabre.Tooth
  • 189
  • 1
  • 3
  • 10
  • Possibly duplacated http://stackoverflow.com/questions/1921539/using-boolean-values-in-c – yuan Mar 03 '13 at 10:40

3 Answers3

22

You should include <stdbool.h> if you want bool, true and false. Also it's true, not TRUE.


If you don't want to include stdbool.h you can just use the slightly ugly _Bool.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • including stdbool.h solves my problem...thanx for that...and can you please explain the slightly ugly part i.e. how to use "_Bool" ( Syntax and an example )... – Sabre.Tooth Mar 03 '13 at 08:47
  • @Sabre.Tooth Use it like a normal type. So wherever you would write `bool` you could also write `_Bool`. The fact is `_Bool` is the "real" type, introduced in C99 and `bool` is just an alias. They couldn't introduce `bool` since it wasn't a reserved keyword in previous standards so it would have broken programs already using it. – cnicutar Mar 03 '13 at 08:49
  • using _Bool I still have to include stdbool.h because compiler treats "true" and "false" as undeclared variables...thnxxx for the help... – Sabre.Tooth Mar 03 '13 at 08:52
  • 1
    @Sabre.Tooth Yeah, or you can use `1` and `0`. But by all means, I recommend `stdbool.h`. – cnicutar Mar 03 '13 at 08:53
-1

Original Answer

Try include cstdio and cstdlib. Might not make any difference, but I've been noticing these weird errors with my compiler also. Things that used to work, no longer work

Edit

In C, false is represented by 0, while true is represented by anything non zero.

In essence, you can roll your own bool datatype like this

typedef enum {false, true} bool;

Then you can use it just like you have already in your app.

You can also just include stdbool.h which should have something similar to the enum suggestion

smac89
  • 39,374
  • 15
  • 132
  • 179
-1

bool is not a data type..
It works fine in Visual studio.. cause its a Microsoft-Specific thingy..
Just include stdbool.h and it will work fine :)

geekybedouin
  • 549
  • 1
  • 11
  • 23