C++ TRUE 和 FALSE 宏的奇怪定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34062595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Strange definitions of TRUE and FALSE macros
提问by Keshava GN
I have seen the following macro definitions in a coding book.
我在编码书中看到了以下宏定义。
#define TRUE '/'/'/'
#define FALSE '-'-'-'
There was no explanation there.
那里没有任何解释。
Please explain to me how these will work as TRUE
and FALSE
.
请向我解释这些将如何作为TRUE
和工作FALSE
。
回答by Jay
Let's see: '/' / '/'
means the char
literal /
, divided by the char
literal '/'
itself. The result is one, which sounds reasonable for TRUE
.
让我们看看:'/' / '/'
表示char
文字/
,除以char
文字'/'
本身。结果是 1,这对于 来说听起来很合理TRUE
。
And '-' - '-'
means the char
literal '-'
, subtracted from itself. This is zero (FALSE
).
并'-' - '-'
表示从自身减去的char
字面'-'
量。这是零 ( FALSE
)。
There are two problems with this: first, it's not readable. Using 1
and 0
is absolutely better. Also, as TartanLlama and KerrekSB have pointed out, if you are ever going to use that definition, please do add parentheses around them so you won't have any surprises:
这有两个问题:首先,它不可读。使用1
和0
绝对更好。此外,正如 TartanLlama 和 KerrekSB 指出的那样,如果您打算使用该定义,请务必在它们周围添加括号,以免有任何意外:
#include <stdio.h>
#define TRUE '/'/'/'
#define FALSE '-'-'-'
int main() {
printf ("%d\n", 2 * FALSE);
return 0;
}
This will print the value of the char
literal '-'
(45 on my system).
这将打印char
文字的值'-'
(在我的系统上为 45)。
With parentheses:
带括号:
#define TRUE ('/'/'/')
#define FALSE ('-'-'-')
the program correctly prints zero, even though it doesn't make much sense to multiply a truth value by an integer, but it's just an example of the kind of unexpected bugs that could bite you if you don't parenthesize your macros.
程序正确打印零,即使将真值乘以整数没有多大意义,但这只是一个例子,如果您不将宏括起来,可能会咬到您的意外错误。
回答by BlackDwarf
It's just another way of writing
这只是另一种写作方式
#define TRUE 1
#define FALSE 0
The expression '/'/'/'
will divide the char value of '/'
by itself, which will give 1 as a result.
该表达式'/'/'/'
将除以'/'
自身的 char 值,结果为 1。
The expression '-'-'-'
will substract the char value of '-'
from itself, which will give 0 as a result.
该表达式将从自身中'-'-'-'
减去 的 char 值,'-'
结果为 0。
Brackets around the whole define
expressions are missing though, which can lead to errors in the code using these macros. Jay's answeradresses that pretty well.
define
但是,缺少整个表达式周围的括号,这可能会导致使用这些宏的代码出错。杰伊的回答很好地解决了这个问题。
An example of "real-life" scenario where forgetting the brackets can be harmful is the combined use of these macros with a C-style cast operator. If someone decides to cast these expressions to bool
in C++ for instance:
忘记括号可能有害的“现实生活”场景的一个示例是将这些宏与 C 风格的强制转换运算符结合使用。例如,如果有人决定将这些表达式转换bool
为 C++:
#include <iostream>
#define TRUE '/'/'/'
#define FALSE '-'-'-'
int main() {
std::cout << "True: " << (bool) TRUE << std::endl;
std::cout << "False: " << (bool) FALSE << std::endl;
return 0;
}
Here's what we get:
这是我们得到的:
True: 0
False: -44
So (bool) TRUE
would actually evaluate to false
, and (bool) FALSE
would evaluate to true
.
所以(bool) TRUE
实际上会评估为false
,并且(bool) FALSE
会评估为true
。
回答by 0605002
It is equivalent to writing
相当于写
#define TRUE 1
#define FALSE 0
What the expression '/'/'/'
actually does is dividing the character /
(whatever its numeric value is) by itself, so it becomes 1
.
表达式'/'/'/'
实际所做的是将字符/
(无论其数值是什么)除以自身,因此它变为1
.
Similarly, the expression '-'-'-'
subtracts the character -
from itself and evaluates to 0
.
类似地,表达式从自身中'-'-'-'
减去字符-
并计算为0
。
It would be better to write
最好写
#define TRUE ('/'/'/')
#define FALSE ('-'-'-')
to avoid accidental change of values when used with other higher-precedence operators.
避免在与其他更高优先级运算符一起使用时意外更改值。
回答by ouah
Jayalready answered why the values of these expressions are 0
and 1
.
Jay已经回答了为什么这些表达式的值是0
和 1
。
For history sake, these expressions '/'/'/'
and '-'-'-'
come from one of the entries of 1st International Obfuscated C Code Contest in 1984:
由于历史原因,这些表达式'/'/'/'
和'-'-'-'
来自的条目之一在1984年第一次国际C语言混乱代码大赛:
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
(Link to the program here, there is a hint of what this program does in the IOCCC page above.)
(链接到这里的程序,在上面的 IOCCC 页面中有一个关于这个程序做什么的提示。)
Also if I remember correctly these expressions as obfuscated macros for TRUE
and FALSE
were also covered in "Obfuscated C and Other Mysteries"book by Don Libes (1993).
此外,如果我没记错的话,这些表达式是用于混淆的宏,TRUE
并且FALSE
也在Don Libes (1993) 的“Obfuscated C and Other Mysteries”一书中进行了介绍。
回答by anand
It hilarious way for writing macros for True
and False
.
这是为True
和编写宏的有趣方式False
。
As many explanations have been provided /
means a 1 byte number(as per ASCII) when divided by itself it give you 1
which will be treat as True
and likewise -
is again a byte number when subtracted the same value it give you 0
which will be interpreted as false
由于提供/
了许多解释,这意味着一个 1 字节的数字(按照 ASCII)当被自身除以它时,它会给你1
这将被视为True
,同样地, -
当减去它给你的相同值时,它又是一个字节数0
,它将被解释为false
#define TRUE '/'/'/'
#define FALSE '-'-'-'
hence we can replace /
or -
with any char we like, for example:
因此我们可以替换/
或-
任何我们喜欢的字符,例如:
#define TRUE '!'/'!'
#define FALSE 'o'-'o'
Will keep the same meaning as the original expression.
将保持与原始表达式相同的含义。
回答by Fabien
Let's start with true. You can read it as '/' / '/'
, which means "character '/' divided by character '/'". Since each character, in C, is a numeric value (on one byte), it can be read as "the ASCII value of character '/' divided by the ASCII value of that same character", which means 1 (because, obviously, x/x is 1). Hence, TRUE
is 1.
让我们从真实开始。您可以将其读作'/' / '/'
,意思是“字符 '/' 除以字符 '/'”。由于 C 中的每个字符都是一个数值(在一个字节上),因此可以将其读作“字符 '/' 的 ASCII 值除以同一字符的 ASCII 值”,即 1(因为,显然, x/x 是 1)。因此,TRUE
是 1。
For FALSE
, its the same reasoning: '-'-'-'
reads '-' - '-'
, i.e "the ASCII value of '-' minus the ASCII value of '-'", which is 0. Hence, FALSE
is 0.
对于FALSE
,其推理相同:'-'-'-'
reads '-' - '-'
,即“'-' 的 ASCII 值减去 '-' 的 ASCII 值”,即为 0。因此,FALSE
为 0。
This is a nasty way to state the obvious.
这是陈述显而易见的事情的一种令人讨厌的方式。