由于ANSI C99存在_Bool或bool通过stdbool.h.但是printfbool 还有一个格式说明符吗?
我的意思是伪代码:
bool x = true;
printf("%B\n", x);
哪个会打印:
true
在一个项目中,我在C++和一个使用stdbool.h的C库之间进行接口.
#ifndef _STDBOOL_H
#define _STDBOOL_H
/* C99 Boolean types for compilers without C99 support */
/* http://www.opengroup.org/onlinepubs/009695399/basedefs/stdbool.h.html */
#if !defined(__cplusplus)
#if !defined(__GNUC__)
/* _Bool builtin type is included in GCC */
typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
#endif
#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1
#endif
#endif
一些结构有bool成员.因此,如果我将这些结构中的一个定义为C++函数中的局部变量并将其传递给C函数,则C++和C之间的大小不一致,因为bool在C++中是一个再见,在C中是4个.
有没有人有任何建议如何克服这个问题而不诉诸我目前的解决方案
//#define bool _Bool
#define bool unsigned char