Search code examples
cembeddedstdbool

Usage of "stdbool.h" in Embedded systems


I am reading about the C-coding style for embedded systems and I found the following statement:

Do not use stdbool.h library. Use 1 or 0 for true or false respectively

This statement was written in the following source: https://github.com/MaJerle/c-code-style

However, there is no explanation why to use it or any advantage/ disadvantage in adopting such style.

I would like to know if there is any advantage in using 1 or 0 instead of true or false.


Solution

  • What can be said about stdbool.h specifically:

    • It is one of the mandatory headers required to be supported by all C compilers, including freestanding implementations (embedded compilers). Source: ISO 9899:2018 ch.4 §6.
    • It is not supported by long-since obsolete C90 ("ANSI C"). The main reason for not using the header and resorting to 1/0 would therefore be backwards compatibility with C90.
    • Upcoming C23 makes stdbool.h unnecessary (but not formally deprecated yet), because in C23 bool, true and false are finally made proper keywords. _Bool is still available too.

    Also the coding style has the following rules:

    • Comments starting with // are not allowed.

      This also suggests that C90 compatibility is required, because that's the only reason why you would not allow such comments (they were introduced in C99).

    • Do not declare variable after first executable statement

      Similarly, this is also required for C90 compatibility, which otherwise doesn't make any sense.

    So I would conclude that a lot of these rules were added for C90 compatibility, or otherwise there is no making sense of them.