Is using char as a bool in C bad practice?

后端 未结 4 1083
逝去的感伤
逝去的感伤 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 21:48

    I would suggest using a bit to represent true or false, rather than a character. A character uses 8 bits, We can set 1 for true and 0 for false with just 1 bit. That will be more memory efficient and also satisfies the purpose. (e.g) char flag:1;

    Reference : http://en.wikipedia.org/wiki/Bit_field

提交回复
热议问题