【转】细数那些令人发狂的程序语言的特性
1、C语言中的数组
在C/C++中,a[10] 可以写成 10[a]
“Hello World”[i] 也可以写成 i["Hello World"]
2、在Javascript中
'5' + 3 的结果是:'53'
'5' – 3 的结果是:2
3、C/C++中的Trigraphs
int main() { cout << "LOL??!"; } int main() { cout << "LOL??!";} 上面的这段程序会输出: “LOL|”,这是因为 ??! 被转成了 | ,关于Trigraphs,下面有个表格: Integer foo = 1000; Integer bar = 1000; foo <= bar; // true foo >= bar; // true foo == bar; // false //然后,如果你的 foo 和 bar 的值在 127 和 -128 之间(包括) //那么,其行为则改变了: Integer foo = 42; Integer bar = 42; foo <= bar; // true foo >= bar; // true foo == bar; // true Integer foo = 1000;Integer bar = 1000;foo <= bar; // truefoo >= bar; // truefoo == bar; // false//然后,如果你的 foo 和 bar 的值在 127 和 -128 之间(包括)//那么,其行为则改变了:Integer foo = 42;Integer bar = 42;foo <= bar; // truefoo >= bar; // truefoo == bar; // true
/** * Returns a <tt>Integer</tt> instance representing the specified * <tt>int</tt> value. * If a new <tt>Integer</tt> instance is not required, this method * should generally be used in preference to the constructor * <a href="mailto:{@link">{@link</a> #Integer(int)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param i an <code>int</code> value. * @return a <tt>Integer</tt> instance representing <tt>i</tt>. * @since 1.5 */ public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); }$. $_ $_# $$ $[ @_
try { return true; } finally { return false; } try { return true;} finally { return false;} 在 javascript 和python下,其行为和Java的是一样的。 void duff_memcpy( char* to, char* from, size_t count ) { size_t n = (count+7)/8; switch( count%8 ) { case 0: do{ *to++ = *from++; case 7: *to++ = *from++; case 6: *to++ = *from++; case 5: *to++ = *from++; case 4: *to++ = *from++; case 3: *to++ = *from++; case 2: *to++ = *from++; case 1: *to++ = *from++; }while(--n>0); } } void duff_memcpy( char* to, char* from, size_t count ) { size_t n = (count+7)/8; switch( count%8 ) { case 0: do{ *to++ = *from++; case 7: *to++ = *from++; case 6: *to++ = *from++; case 5: *to++ = *from++; case 4: *to++ = *from++; case 3: *to++ = *from++; case 2: *to++ = *from++; case 1: *to++ = *from++; }while(--n>0); }} $x = "foo"; function foo(){ echo "wtf"; } $x(); $x = "foo";function foo(){ echo "wtf"; }$x();class Foo { public: static void bar() { std::cout << "bar()" << std::endl; } }; class Foo { public: static void bar() { std::cout << "bar()" << std::endl; }};