Writing a function in C that returns a boolean

Since C has no boolean types, how can I write such a function in C:

bool checkNumber()
{
   return false;
}
+3
source share
4 answers

The type is booldefined in the header <stdbool.h>and is available under the name _Boolotherwise (if you use the C99 compiler). If you do not have C99, you can always come up with your own bool type as follows:

typedef enum {false, true} bool;
+17
source

intcommonly used as a boolean in C, at least until C99. Zero means false, a nonzero value means true.

+9
source

define, ints 1s 0s .

#define BOOL char
#define TRUE 1
#define FALSE 0

char BOOL, 1 4. ( )

+1

If you are not using C99 and make sure you need to add your own boolean type, make sure you give it your name. Using "bool" or "BOOL" will cause trouble if you enable a third-party library. The one exception would be to use the de facto standard:

#define BOOL int
#define TRUE 1
#define FALSE 0

But pin them to #ifndef. But note that some libraries use 'char' as BOOL. If you come from the background of C ++, think about whether you want to interact with C ++.

+1
source

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


All Articles