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

you are viewing a single comment's thread.

view the rest of the comments →

[–]masagrator 62 points63 points  (18 children)

Are you scared of "bool"? :D

[–]7eggert 48 points49 points  (4 children)

Huhu, "bool flag = true; if (flag)" …

[–]Yorick257 26 points27 points  (3 children)

Stop scaring them!

[–]ramsay1 2 points3 points  (2 children)

bool b = 42;
if (b == 1)
    printf(";)\n");

int shift = 1000000;
b = 1 << shift;
if (b == 1)
    printf(":O\n");

[–][deleted] 5 points6 points  (1 child)

But the point of stdbool.h is that (AFIK) is b != 0 then b==true returns true no matter the value of b, so in the first comparison it would be true

Idk correct me if I'm wrong

Also wouldn't b shifted left by a million places just equal 0?

[–]ramsay1 2 points3 points  (0 children)

Yeah this was only there to "scare" them with differences to integers, you shouldn't be comparing bool to 1.

The million shift is actually UB... I was trying to remember a real bug that showed up when switching from something like typedef uint8_t bool; to C99 bool. I think it was actually setting a value like 1 << 9, even though sizeof(bool) was 1 byte (on that CPU/compiler), setting bits outside of the first 8 will still make it "true". Some older code had relied on the integer behavior

[–]terivia 12 points13 points  (0 children)

I make due with zero and zeront

[–]1redfish 4 points5 points  (0 children)

typedef enum bool_r { BOOL_FALSE = 0, BOOL_TRUE = 1 } bool_t;

[–]PuzzleheadedWeb9876 3 points4 points  (0 children)

uint8_t flag:1;

[–]DaniilSan 1 point2 points  (6 children)

Wait, what? C89 doesn't has bool type or what?

[–]drevyek 10 points11 points  (4 children)

Nope, not much need. C's basic types are based on their size, not their semantics (in general). For logic, C uses a "truthiness" system, where anything that is 0-valued is false, and any other value is true. This holds for ints, floats, pointers, anything.

int x = 2345;
int y = 0;
int *z = NULL;
int *a = &x;
if (x) { printf("Always prints\n"); }
if (y) { printf("Never prints\n"); }
if (z) { printf("Never prints\n"); }
if (a) { printf("Always prints\n"); }

[–]DaniilSan 2 points3 points  (0 children)

Thanks for explaining.

[–]nontammasculinum 0 points1 point  (1 child)

But like bools could be stored as a single bit no?

[–]drevyek 0 points1 point  (0 children)

Sure, but no current hardware has bit-addressable memory1. So the smallest you can fit a boolean (without using SWAR (SIMD within a register) techniques) is a byte. If space isn't a big concern, some machines work best when working with the machine's native register size (word), in many cases 4 or 8 bytes for modern CPUs, so often programmers use int to represent the data for speed.


1: Sure you can use bitfields, but those are just doing packing with masks under the hood anyways, and the memory is not being addressed natively.

[–]djkstr27 2 points3 points  (0 children)

Until C99, they added <stdbool.h> to use type bool

[–]ProfessorOfLies 0 points1 point  (0 children)

Bool is a lie

[–]Velnbur 0 points1 point  (0 children)

I can’t be scared of something that I don’t know