Thread: Why Boolean data type not used

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    35

    Why Boolean data type not used

    I find that most of programmers do not use the boolean data type. If i know that my data can be 0 or 1, if i use boolean i will save memory but still it is not used much. What are the disadvantages?

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Clearner123 View Post
    I find that most of programmers do not use the boolean data type. If i know that my data can be 0 or 1, if i use boolean i will save memory but still it is not used much. What are the disadvantages?
    Why would you ask such an absurd question?

    Why would you think that a bool would save memory?

    In stdbool.h, true and false are defined as #define constants, and the size of a bool variable is 1 byte, or the same size of a char!

    _Bool or bool, variables should NOT be used to store the numerical values of 1 or 0. That is what a char or int is for!

    Use data types as they are designed to be used!

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    140
    You are correct that using bool type will generally lead to a savings in memory versus int.


    There are several things to consider. First, bool is a "new" type. It was not added to the original ANSI standard, and so as a result there are many compilers out there which never supported it. Worse, Microsoft for a number of years refused to fully implement/support C99, so the standard didn't have the popularity that might have forced other vendors to upgrade their own offerings. So until C11, there wasn't much pressure on those vendors to support anything past C89.

    This might not seem like much of a justification, since ... 40 years ... but people writing code for microcontrollers in the 1990's and 2000's did their design based on what their microcontroller vendor was willing to pay to have developed, compiler-wise. And mostly that was "as little as possible." So even in the 2000's, there were CPUs being released with C89-only compilers (and at least one that was still K&R).

    Next is the notion of "standard integer promotions." When calling a function, small integer values get widened to larger values. In general, if a parameter is going to use an entire register, it is easier to widen the parameter to the same width as the register than to try and "mask" the unused bits all the time. Likewise, when returning a value it is easier to allocate the entire register than try to mask off unused bits after returning to the caller.

    For these reasons, bool tends to convert to int when calling and when returning. If you look at the isXXX functions, or ferror(), or other functions from the standard that are used to indicate a Boolean condition, they are mostly defined to return an integer. So even today bool is not receiving the respect it might deserve.

    Finally, there is the byte barrier. You can store boolean values in a single bit, if you have access to a single bit. But creating a simple type, as the committee did, prevented them from breaking the byte barrier because the C memory model won't let pointers go below that level, and because bitfields are poorly defined.

    So while any competent C programmer can stuff 8 boolean values into a single char member, the bool type reserves an entire byte for every single value. Thus, bool is too small (from an integer promotion viewpoint) and also too large (from an entropy viewpoint) at the same time.

    In general, yes, you can use bool to store yes/no values. But you only save memory when you have a single bool value. As soon as you have 2 or more, you're better off using a bitfield or bitwise masking. I would say the greatest value of bool lies in the ability to document your intent. By using bool you are telling future readers that you intend for this to be a yes/no field, nothing more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  2. Replies: 3
    Last Post: 09-12-2005, 09:08 AM
  3. Boolean data type
    By NewGuy100 in forum C Programming
    Replies: 5
    Last Post: 07-28-2005, 07:00 PM
  4. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM
  5. define a boolean type
    By Krem in forum C Programming
    Replies: 6
    Last Post: 10-04-2002, 11:54 AM

Tags for this Thread