From best to worse:
Option 1 (C99 and newer)
#include <stdbool.h>
Option 2
typedef enum { false, true } bool;
Option 3
typedef int bool; enum { false, true };
Option 4
typedef int bool; #define true 1 #define false 0
Explanation
- Option 1 will work only if you use C99 (or newer) and it’s the “standard way” to do it. Choose this if possible.
- Options 2, 3 and 4 will have in practice the same identical behavior. #2 and #3 don’t use #defines though, which in my opinion is better.
If you are undecided, go with #1!
Related Posts:
- How to use execvp()
- How to use execvp() to execute a command
- How does strtok() split the string into tokens in C?
- What causes a segmentation fault (core dump) to occur in C?
- uint8_t vs unsigned char
- How to use symbols of extended ASCII table in C?
- What is the difference between ++i and i++?
- What does “collect2: error: ld returned 1 exit status” mean?
- How to convert an int to string in C?
- typedef struct vs struct definitions [duplicate]
- How to print in C
- What can be the reasons of connection refused errors?
- What is newline character — ‘\n’
- Why does ENOENT mean “No such file or directory”?
- What does the question mark character (‘?’) mean?
- Cannot figure out how to use getchar(); in C
- makefile:4: *** missing separator. Stop
- c stack smashing detected
- Undefined reference to pthread_create
- Convert char to int in C and C++
- Difference between malloc and calloc?
- What is Bit Masking?
- segmentation fault : 11
- Strings and character with printf
- What are .a and .so files?
- Why is the gets function so dangerous that it should not be used?
- What does “collect2: error: ld returned 1 exit status” mean?
- Warning comparison between pointer and integer
- Using ssize_t vs int
- Why a warning of “control reaches end of non-void function” for the main function?
- How do I use extern to share variables between source files?
- Cannot assign requested address – possible causes?
- ARM Assembler – How do I use CMP, BLT and BGT?
- Invalid read of size 8 – Valgrind + C
- error: called object is not a function or function pointer
- Is there any format specifier of bool in C?
- munmap_chunk(): invalid pointer
- C read file line by line
- No Symbol Table using GDB on Compiled Programs
- What is char ** in C?
- What is a bus error? Is it different from a segmentation fault?
- Get the current time in C
- Does connect() block for TCP socket?
- Converting a C program to MIPS
- C: linker command failed with exit code 1
- where does stdio.o live in linux machine?
- What’s the equivalent of new/delete of C++ in C?
- sizeof float (3.0) vs (3.0f)
- Get text from user input using C
- Using Dynamic Memory allocation for arrays
- How do I lowercase a string in C?
- warning: expression result unused
- conflicting types error when compiling c program using gcc
- Get the current time in C
- error: indirection requires pointer operand (‘int’ invalid)
- What is a file with extension .a?
- What is the difference between exit(0) and exit(1) in C?
- error: indirection requires pointer operand (‘int’ invalid)
- Finding the length of a Character Array in C
- Return char[]/string from a function
- Valgrind: Invalid read of size 1
- How to print a char array in C through printf?
- How to clear all the elements of array in C?
- Constant pointer vs Pointer to constant
- Variable warning set but not used
- Zero an array in C code
- Excess elements in char array initializer error
- Expected declaration specifier error in function
- Encrypt with private key and decrypt with public key
- strcmp giving segmentation fault
- What does “pointer being freed was not allocated” mean exactly?
- How to use timer in C?
- C char* to int conversion
- What is the difference between stdin and STDIN_FILENO?
- error: aggregate value used where an integer was expected
- char pointers: invalid conversion from ‘char*’ to ‘char’?
- Allocating string with malloc
- How to simply convert a float to a string in c?
- What is the difference between char array and char pointer in C?
- hash function for string
- What happens during a “relocation has invalid symbol index” error?
- What are .axf files?
- Error: “Access not within mapped region at address” (Valgrind)
- printf format specifiers for uint32_t and size_t
- How to convert string to float?
- error: function returns address of local variable
- What is an simple example of copy_from_user
- What is `S_ISREG()`, and what does it do?
- typedef struct pointer definition
- How does the fscanf function work?
- Math constant PI value in C
- The code does not execute properly. Try to figure out why
- ld.exe: cannot open output file … : Permission denied
- How to read from input until newline is found using scanf()?
- getline() vs. fgets(): Control memory allocation
- What is char ** in C? [duplicate]
- segmentation fault using scanf
- Segmentation fault- strcat
- How to Compare 2 Character Arrays [duplicate]
- segmentation fault with strcpy [duplicate]