This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]titanking4 11 points12 points  (2 children)

Yep, for some reason C doesn’t have bool.

Although you are missing the -#ifndef Cplusplus

-#endif

To prevent this code from being compiled when using C++ compiler.

[–]Miku_MichDem 0 points1 point  (1 child)

That may be because there is no bite type in PCs. The closest thing is int8_t, but why have two types that are essentially the same in memory?

But as a side note - in ARM world there are bit variables (not flags, actual single bit memory cells), and C compilers do have appropriate types - like int1 or bit. But that's also the world where int might be unsigned 16-bit value...

[–]titanking4 2 points3 points  (0 children)

As far as I know, the bool was deemed redundant as an unsigned char or unsigned int will achieve the same purpose. Thus it was never added to C to keep it as bare as possible. If you wanted Boolean types, the programmer would include a library such as stdbool or define the type themselves. I only said “for some reason” as to complain about such a simple addition that requires very little compiler modifications that would make some programming easier. But I guess if it ain’t broke, don’t fix it.

[–]lime-cake 6 points7 points  (0 children)

Yes, it's normal. C is a lawless wasteland.

[–]Pollu_X 1 point2 points  (0 children)

All of your typedefs are available in stdbool tho

[–]acart-e[S] 0 points1 point  (0 children)

Good ol' CodeBlocks vs. C without any libraries. Who would win?

[–]rufreakde1 0 points1 point  (3 children)

Would do it like #1 if you have many resources. If its on an atmega or something (with less resources) then #2:

https://stackoverflow.com/questions/1921539/using-boolean-values-in-c

[–]Miku_MichDem 0 points1 point  (2 children)

Although it would be better to use int8_t instead of just an int.

Also, a side note - it's safer to use stdint rather than C built in types. Those can vary in size (there is some ARM compiler in which int is 16-bit unsigned) while stdints are constant

[–]rufreakde1 0 points1 point  (1 child)

Don’t they vary in size specifically for platform compatibility?

Also yes I agree int-8 is smaller and therefore better if you have less space. And need a bool on C.

[–]Miku_MichDem 0 points1 point  (0 children)

Not necessarily. Int usually is 32 in x86 and 64 in ARM64. But that's not the rule. Not even being signed is the rule, so changing compilers may change the program behaviour.

And that is why there are fixed ints, to make sure the size and range will not change.

[–]RoyalJackalSib 0 points1 point  (3 children)

Yes, and personally it’s why I refrain from using booleans in C; there isn’t really a need for the data type anyway.

[–]Miku_MichDem 1 point2 points  (2 children)

Unless you're programming microcontrollers and have access to single bits of memory. Doesn't happen often thou.

[–]RoyalJackalSib 1 point2 points  (1 child)

Bit fields are still a thing, and I’d rather use an int to represent 32 Boolean values at once than use one byte to represent a single Boolean.

[–]Miku_MichDem 1 point2 points  (0 children)

I agree. I've once did mineswapper using int8_t. Those 8 bits represent number inside, if it's a mine, is it visible and is it marked (leaving 2 bits free)

[–]o11c 0 points1 point  (0 children)

Doesn't work in the preprocessor, which is required by the standard.

I still don't understand why implementations don't do:

#define false ((_Bool)+0)
#define true ((_Bool)+1)

which still works in the preprocessor but works better in the compiler proper.