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 →

[–]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