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

  • hendrik@palaver.p3x.de
    link
    fedilink
    English
    arrow-up
    17
    ·
    edit-2
    10 hours ago

    I think there are several reasons. First, if you do it with an int, you’re probably using up 32bits per value. You’d need 1 but waste the other 31, they needlessly take up storage. (Edit: Though your bool will most likely take 8 bit anyways due to other things.) And then sometimes it’s nice to be expressive. So no one needs to remember if 1 is True or 0, or if True is greater than False, whether 2 or -1 map to True or False. And you end up in situations where either 2 equals 1, or True isn’t always equal True. Or you do weird things to handle values other than 0 and 1 in the operations. All of that can be avoided and the code gets more readable with specific types.

    There’s nothing wrong with using ints, though. You just have to make sure the maths stays consistent.

    • OwOarchist@pawb.social
      link
      fedilink
      English
      arrow-up
      8
      ·
      21 hours ago

      First, if you do it with an int, you’re probably using up 32bits per value.

      One project I worked with stored boolean values as bits in an 8-bit int, and then used binary math to read and write individual bits from that int, with each bit representing a distinct and independent boolean variable.

      Really weird, complicated, and sounds kind of dumb … but it worked, and it was extremely memory-efficient.

    • durinn@programming.devOP
      link
      fedilink
      English
      arrow-up
      3
      ·
      22 hours ago

      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
        link
        fedilink
        arrow-up
        3
        ·
        12 hours ago

        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
          link
          fedilink
          English
          arrow-up
          1
          ·
          edit-2
          10 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
        link
        fedilink
        English
        arrow-up
        4
        ·
        22 hours ago

        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”.

          • hendrik@palaver.p3x.de
            link
            fedilink
            English
            arrow-up
            2
            ·
            edit-2
            11 hours ago

            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
              link
              fedilink
              arrow-up
              2
              ·
              10 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.

          • durinn@programming.devOP
            link
            fedilink
            English
            arrow-up
            4
            ·
            21 hours ago

            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
          link
          fedilink
          English
          arrow-up
          4
          ·
          21 hours 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.

  • lime!@feddit.nu
    link
    fedilink
    arrow-up
    11
    ·
    edit-2
    22 hours ago

    code is read more often that it is written. booleans are actually just integers under the hood but explicitly using booleans communicates your intent. you’re saying to the next guy that “if this is something other than 1 or 0 something has gone wrong”.

    when it comes to C specifically, the bool type was a pretty late addition because as you say, an integer can do the job. but it’s very good for organisation, and it also allows the compiler to make some assumptions about how the variable can be accessed. if you know something can only be in two states, you can shove a whole bunch of them into the same byte of memory.

    • durinn@programming.devOP
      link
      fedilink
      English
      arrow-up
      4
      ·
      22 hours ago

      Thanks! I did indeed see a reference somewhere, saying that it makes the code easier to read too. :)

  • teaHead74@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    22 hours ago

    If u accidentally us the wrong type of variable it outputs en error when compiling. That’s one of the main pros of statically typed languages and why the most reliable scripts are written in them to my knollidge. I’m sure there a languages out there where u can us boolean as a int or float but non i know. Not that i would consider my self a programmer but i asked the myself the same questions when i started.

  • ThotDragon@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    2
    ·
    22 hours ago

    Sometimes you need to store a value that is either true or false and refer to it later or maybe update it. Maybe you have some complicated logic for when a loop needs to end that you can’t really represent in the while condition. You can make the while check your bool variable and then set it to false when you are ready to end. Perhaps you have a function that returns a bool value and you setup your logic so there is a default return value but you do some calculations and sometimes you want to return the other value. it lets you have a single return statement in the function which can be easier to read.

    You use boolean variables in the same way as other variables. The example you share is indeed contrived. In that situation you need no variables at all. That example is merely showing the syntax.

    • durinn@programming.devOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      22 hours ago

      Thank you for the explanation! Perhaps at my novice level, I should just go with it and learn the use of bool as I experience it. :)