在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
PHP对字符串中数值的处理。详见之前对另一个问题的回答,但简而言之:
"01a4" != "001a4"
如果你有两个包含不同数量字符的字符串,它们不能被认为是相等的。前导零很重要,因为它们是字符串而不是数字。
"01e4" == "001e4"
PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000" as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.
其他回答
C的多个名称空间:
typedef int i;
void foo()
{
struct i {i i;} i;
i: i.i = 3;
printf( "%i\n", i.i);
}
或与字符:
typedef char c;
void foo()
{
struct c {c c;} c;
c: c.c = 'c';
printf( "%c\n", c.c);
}
Perl:
可以编写一个完全由标点符号组成的程序。
这是怎么回事?!
在Java中(实际上,我最近在不同的SO帖子上写过这个):
int x = 1 + + + + + + + + + + + + + 1;
System.out.println(x);
在Perl中(没有“使用严格”或“使用警告”):
if(true==undef)
{
print "True\n";
}
else{
print "False\n";
}
if(undef)
{
print "True\n";
}
else{
print "False\n";
}
if(true)
{
print "True\n";
}
else{
print "False\n";
}
打印:
True
False
True
在C语言中,
int x = 1;
int y = x++ + ++x;
printf("%d", y);
是模棱两可的,打印什么取决于编译器。编译器可以在计算++x之前或在语句的末尾存储x++的新值。