As C does not have boolean types, how can I write a function like this in C:
bool checkNumber()
{
return false;
}
As C does not have boolean types, how can I write a function like this in C:
bool checkNumber()
{
return false;
}
The bool
type is defined in the <stdbool.h>
header, and is available under the name _Bool
otherwise (assuming you're using a C99 compiler). If you don't have C99, you can always invent your own bool type like this:
typedef enum {false, true} bool;
int
is commonly used as a Boolean in C, at least prior to C99. Zero means false, non-zero means true.
You could use defines to avoid using ints and 1s and 0s directly for boolean logic.
#define BOOL char
#define TRUE 1
#define FALSE 0
I chose char for BOOL
because it's only 1 byte instead of 4. (For most systems)
If you are not using C99, and determine that you need to add your own boolean type, then ensure that you give it its own name. Using 'bool' or 'BOOL' will only get you into trouble when you include a 3rd party library. The only exception would be to use the de-facto standard of:
#define BOOL int
#define TRUE 1
#define FALSE 0
But ensure you wrap these in #ifndef. But note that some libraries do use 'char' as BOOL. If you are coming from C++ background, have a think as to whether you will want to interoperate with C++.