[英]Usage of NOT operator in #define directives in C programming
I have defined macros as below. 我定义了如下宏。
#define FALSE 0
#define TRUE (!FALSE)
What will be the data-type of TRUE
and FALSE
? TRUE
和FALSE
的数据类型是什么? What literal value does TRUE
take after preprocessing? 预处理后TRUE
取什么文字值? Is it compiler dependent? 是否依赖编译器? Why? 为什么?
#define
preprocessor directive (macros) are meant to do textual replacement . #define
预处理程序指令(宏)用于执行文本替换 。 It will replace all occurrence of FALSE
to 0
and TRUE
to !0
that essentially gets evaluated to 1
. 它将所有出现的FALSE
替换为0
,将TRUE
替换为!0
,而该值实际上被评估为1
。 So, the resultant data type will be same as 0
and 1
. 因此,结果数据类型将与0
和1
相同。 ie, integer. 即整数。
Regarding the usage of !
关于的用法!
operator, it always produces a result of type int
. 运算符,它总是产生int
类型的结果。
Quoting the C11
standard, chapter §6.5.3.3 ( emphasis mine ) 引用C11
标准,第6.5.3.3节( 重点是我的内容 )
The result of the logical negation operator
!
逻辑否定运算符的结果!
is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has typeint
. 如果其操作数的值比较不等于0,则为0;如果其操作数的值比较等于0,则为1。 结果的类型为int
。 [...] [...]
At the time of preprocessing, macros are replaced with text. 在预处理时,宏将替换为文本。 FALSE
will be replace by 0, and TRUE
will be replaced by 1. FALSE
将替换为0,而TRUE
将替换为1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.