4

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.

10
  • 3
    Overall it seems like a mostly reasonable coding standard, though some things are clearly taken out of the blue with no rationale. The only reason I can think of why you'd want to avoid stdbool.h is backwards-compatibility with C90. But that doesn't seem to be a requirement here? Commented Oct 18, 2023 at 8:36
  • Since this is a rule of a specific project, you need to ask its developers about reasons. If you ask in general, StackOverflow is not well suited, and your question is subject to closure because it asks for opinions. Commented Oct 18, 2023 at 8:38
  • 5
    Dubious value. Inconsistent. char a; char b; /* Wrong */. char a, b; /* OK */... Then, when it comes to pointers, "always align asterisk to type" leading to char* a, b; when b should be a pointer. Rules, eh? Commented Oct 18, 2023 at 8:49
  • 2
    @thebusybee That stdbool.h is suitable for embedded systems isn't an opinion, it's a fact. It is one of the mandatory headers that even "freestanding" systems must support, so if it isn't there, the C compiler is non-conforming. Commented Oct 18, 2023 at 8:49
  • 1
    @Fe2O3 Also I could almost swear there's a MISRA C rule forbidding several declarations like that on a single line, but I can't find it. MISRA C explicitly being not a subjective style guide, but a safe subset banning dangerous practices. Commented Oct 18, 2023 at 9:02

2 Answers 2

6

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

There isn't. Using stdbool.h in embedded system is fine.

Note that stdbool.h might not be available on older compilers, but that cannot be the reason because guide you linked also recommended stdint.h types (which in itself is a good advice). And besides, you shouldn't be using outdated compilers.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.