Is using char as a bool in C bad practice?

后端 未结 4 1084
逝去的感伤
逝去的感伤 2021-02-19 21:35

Is there any downside to using

typedef char bool;
enum boolean { false, true };

in C to provide a semantic boolean type?

4条回答
  •  醉梦人生
    2021-02-19 22:03

    The downside of typedef char bool; is that if you compile with a C99 implementation and happen to include , this ends up as typedef char _Bool;, which is wrong. Also, if you ever tried to compile the code as C++, you'd have issues (that's not necessarily a problem, but it could be).

    It would probably better either to use if your implementation provides one or to use a different name for the type, like BOOL.

提交回复
热议问题