this post was submitted on 14 Mar 2026
18 points (95.0% liked)

Learn Programming

2134 readers
18 users here now

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 2 years ago
MODERATORS
 

I saw one example

int x = 10;
int y = 5;

bool isGreater = x > y;

printf("%d", isGreater);

But I could write this

int x = 10;
int y = 5;

printf("%d", x > y);

I am a complete beginner and I have no real reason why I would or would not want to deal with boolean variables, but I want to understand their raison d'être.

Edit: typo city

you are viewing a single comment's thread
view the rest of the comments
[–] emotional_soup_88@programming.dev 3 points 1 day ago (2 children)

Very nice point about the memory/storage usage! Also, I didn't realize one can "map" other values as true/false than 1/0. But then again, I know very little at this moment. XD

[–] calcopiritus@lemmy.world 3 points 1 day ago (1 children)

Don't be mislead by that comment. It implies that a boolean variable takes up 1 bit instead of 32. But most probably it will take 8 bits. You're still "wasting" 7 bits instead of 31.

[–] hendrik@palaver.p3x.de 1 points 22 hours ago* (last edited 22 hours ago)

Good point. Yeah, my "probably" is doing a lot of heavy-lifting there. Thx, I've added a short sentence.) In reality we don't really know the length of a bool nor an int type in C. Likely a bool is 1 Byte and an int is 4 Bytes. But that depends on the architecture and compiler. The bool it guaranteed to hold 0 and 1, so it must be at least 1 bit. But with how addressing works, it ends up being 8 bits (1 Byte) anyway. If we want to be more efficient than that, I believe we have to code something with bit fields. It's a bit out of scope for a beginner, unless they do microcontroller stuff.

[–] hendrik@palaver.p3x.de 4 points 1 day ago (2 children)

Sure. As far as I remember in C, 0 is False. Every other number is True, you can use 1 or 42, doesn't matter, they're all "true".

[–] CameronDev@programming.dev 7 points 1 day ago (2 children)

Except that a lot of the cstdlib functions return an int, where 0 == success, and negative numbers are falsy.

[–] hendrik@palaver.p3x.de 2 points 23 hours ago* (last edited 23 hours ago) (1 children)

The way I make sense of it, is we sometimes return failure (i.e. from main). So 0 is no failure (aka success) and we can use the same thinking. The correct, expressive way to write it is probably use "EXIT_SUCCESS" and skip the ones and zeros. Pretty sure this comes from Unix. And with a lot of the other functions in cstdlib it's the same way as using integers as booleans. For example a "malloc()" will either return your memory or a null pointer and the 0 is the special failure case.

But IMO the programming language shows its age and the context it was used in. More modern programming language design tends to be more strict with the types. Differentiate between interfacing with Unix stuff and other kinds of values. And we got more powerful concepts to deal with errors. So we don't always have to abuse the zero to say we ended up in some special case.

[–] CameronDev@programming.dev 2 points 23 hours ago

Malloc return 0 is a failure, but open return 0 is success. It's just inconsistent, and it's definitely an age and context thing.

Rust's Result api are a pretty great solution. Not sure what other options are out there though.

But I think one has to keep apart return codes that are signals to the OS and the end user, and 1/0 in a true/false context in conditional statements.

[–] OwOarchist@pawb.social 4 points 1 day ago

Okay, now... variableA = 1 (true) and variableB = 42 (true). Suppose we want to compare the values of them to see if they're the same or different. A simple check of if variableA == variableB will return False, because while they're both set to 'true', they're set to different values of 'true'. It could get problematic and become a way to introduce really weird bugs.