免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2789 | 回复: 1
打印 上一主题 下一主题

【转】细数那些令人发狂的程序语言的特性 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-11 16:35 |只看该作者 |倒序浏览

【转】细数那些令人发狂的程序语言的特性













1、C语言中的数组
在C/C++中,a[10] 可以写成 10[a]

“Hello World” 也可以写成 i["Hello World"]

2、在Javascript中
'5' + 3 的结果是:'53'

'5' – 3 的结果是:2   

3、C/C++中的Trigraphs

Cpp代码
  1. 1.int main() {      
  2. 2.    cout << "LOL??!";      
  3. 3.}     
  4. 4.  
  5. 5.int main() {   
  6. 6.    cout << "LOL??!";   
  7. 7.}  
  8. int main() {   
  9.     cout << "LOL??!";   
  10. }  

  11. int main() {
  12.     cout << "LOL??!";
  13. }
复制代码
上面的这段程序会输出: “LOL|”,这是因为 ??! 被转成了 | ,关于Trigraphs,下面有个表格:  

??=  #  
??(  [  
??/  \  
??)  ]  
??’ ^  
??<  {  
??!  丨
??>  }  
??-  ~  


4、JavaScript 的条件表
看到下面这个表,不难理解为什么Javascript程序员为什么痛苦了

Js代码
  1. 1.''        ==   '0'          //false      
  2. 2.0         ==   ''           //true      
  3. 3.0         ==   '0'          //true      
  4. 4.false     ==   'false'      //false      
  5. 5.false     ==   '0'          //true      
  6. 6.false     ==   undefined    //false      
  7. 7.false     ==   null         //false      
  8. 8.null      ==   undefined    //true      
  9. 9." \t\r\n" ==   0            //true     
  10. 10.  
  11. 11.''        ==   '0'          //false   
  12. 12.0         ==   ''           //true   
  13. 13.0         ==   '0'          //true   
  14. 14.false     ==   'false'      //false   
  15. 15.false     ==   '0'          //true   
  16. 16.false     ==   undefined    //false   
  17. 17.false     ==   null         //false   
  18. 18.null      ==   undefined    //true   
  19. 19." \t\r\n" ==   0            //true   
  20. ''        ==   '0'          //false   
  21. 0         ==   ''           //true   
  22. 0         ==   '0'          //true   
  23. false     ==   'false'      //false   
  24. false     ==   '0'          //true   
  25. false     ==   undefined    //false   
  26. false     ==   null         //false   
  27. null      ==   undefined    //true   
  28. " \t\r\n" ==   0            //true  

  29. ''        ==   '0'          //false
  30. 0         ==   ''           //true
  31. 0         ==   '0'          //true
  32. false     ==   'false'      //false
  33. false     ==   '0'          //true
  34. false     ==   undefined    //false
  35. false     ==   null         //false
  36. null      ==   undefined    //true
  37. " \t\r\n" ==   0            //true
复制代码
5、Java的Integer cache

Java代码
  1. 1.Integer foo = 1000;      
  2. 2.Integer bar = 1000;      
  3. 3.     
  4. 4.foo <= bar; // true      
  5. 5.foo >= bar; // true      
  6. 6.foo == bar; // false      
  7. 7.     
  8. 8.//然后,如果你的 foo 和 bar 的值在 127 和 -128 之间(包括)      
  9. 9.//那么,其行为则改变了:      
  10. 10.     
  11. 11.Integer foo = 42;      
  12. 12.Integer bar = 42;      
  13. 13.     
  14. 14.foo <= bar; // true      
  15. 15.foo >= bar; // true      
  16. 16.foo == bar; // true     
  17. 17.  
  18. 18.Integer foo = 1000;   
  19. 19.Integer bar = 1000;   
  20. 20.  
  21. 21.foo <= bar; // true   
  22. 22.foo >= bar; // true   
  23. 23.foo == bar; // false   
  24. 24.  
  25. 25.//然后,如果你的 foo 和 bar 的值在 127 和 -128 之间(包括)   
  26. 26.//那么,其行为则改变了:   
  27. 27.  
  28. 28.Integer foo = 42;   
  29. 29.Integer bar = 42;   
  30. 30.  
  31. 31.foo <= bar; // true   
  32. 32.foo >= bar; // true   
  33. 33.foo == bar; // true  
  34. Integer foo = 1000;   
  35. Integer bar = 1000;   
  36.   
  37. foo <= bar; // true   
  38. foo >= bar; // true   
  39. foo == bar; // false   
复制代码
//然后,如果你的 foo 和 bar 的值在 127 和 -128 之间(包括)   
//那么,其行为则改变了:
  1. Integer foo = 42;   
  2. Integer bar = 42;   
  3.   
  4. foo <= bar; // true   
  5. foo >= bar; // true   
  6. foo == bar; // true  

  7. Integer foo = 1000;
  8. Integer bar = 1000;

  9. foo <= bar; // true
  10. foo >= bar; // true
  11. foo == bar; // false
复制代码
//然后,如果你的 foo 和 bar 的值在 127 和 -128 之间(包括)
//那么,其行为则改变了:
  1. Integer foo = 42;
  2. Integer bar = 42;

  3. foo <= bar; // true
  4. foo >= bar; // true
  5. foo == bar; // true
复制代码
为什么会这样呢?你需要了解一下Java Interger Cache,下面是相关的程序,注意其中的注释

Java代码
  1. 1./**  
  2. 2.
  3. 3.     * Returns a <tt>Integer</tt> instance representing the specified  
  4. 4.
  5. 5.     * <tt>int</tt> value.  
  6. 6.
  7. 7.     * If a new <tt>Integer</tt> instance is not required, this method  
  8. 8.
  9. 9.     * should generally be used in preference to the constructor  
  10. 10.     * <a href="mailto:{@link">{@link</a> #Integer(int)}, as this method is likely to yield  
  11. 11.     * significantly better space and time performance by caching  
  12. 12.     * frequently requested values.  
  13. 13.     *  
  14. 14.     * @param  i an <code>int</code> value.  
  15. 15.     * @return a <tt>Integer</tt> instance representing <tt>i</tt>.  
  16. 16.     * @since  1.5  
  17. 17.     */  
  18. 18.    public static Integer valueOf(int i) {   
  19. 19.        if(i >= -128 && i <= IntegerCache.high)   
  20. 20.            return IntegerCache.cache[i + 128];   
  21. 21.        else  
  22. 22.            return new Integer(i);   
  23. 23.    }  
  24. /**

  25.      * Returns a <tt>Integer</tt> instance representing the specified

  26.      * <tt>int</tt> value.

  27.      * If a new <tt>Integer</tt> instance is not required, this method

  28.      * should generally be used in preference to the constructor
  29.      * <a href="mailto:{@link">{@link</a> #Integer(int)}, as this method is likely to yield
  30.      * significantly better space and time performance by caching
  31.      * frequently requested values.
  32.      *
  33.      * @param  i an <code>int</code> value.
  34.      * @return a <tt>Integer</tt> instance representing <tt>i</tt>.
  35.      * @since  1.5
  36.      */
  37.     public static Integer valueOf(int i) {
  38.         if(i >= -128 && i <= IntegerCache.high)
  39.             return IntegerCache.cache[i + 128];
  40.         else
  41.             return new Integer(i);
  42.     }
复制代码
5、Perl的那些奇怪的变量

Perl代码
  1. 1.$.      
  2. 2.$_     
  3. 3.$_#      
  4. 4.$$      
  5. 5.$[      
  6. 6.@_   
  7. $.   
  8. $_  
  9. $_#   
  10. $$   
  11. $[   
  12. @_
复制代码
其所有的这些怪异的变量请参看:http://www.kichwa.com/quik_ref/spec_variables.html

6、Java的异常返回
请看下面这段程序,你觉得其返回true还是false?  

Java代码
  1. 1.try {      
  2. 2.    return true;      
  3. 3.} finally {      
  4. 4.    return false;      
  5. 5.}     
  6. 6.  
  7. 7.try {   
  8. 8.    return true;   
  9. 9.} finally {   
  10. 10.    return false;   
  11. 11.}  
  12. try {   
  13.     return true;   
  14. } finally {   
  15.     return false;   
  16. }  

  17. try {
  18.     return true;
  19. } finally {
  20.     return false;
  21. }
复制代码
在 javascript 和python下,其行为和Java的是一样的。  

7、C语言中的Duff device
下面的这段程序你能看得懂吗?这就是所谓的Duff Device,相当的怪异。

C代码
  1. 1.void duff_memcpy( char* to, char* from, size_t count ) {      
  2. 2.    size_t n = (count+7)/8;      
  3. 3.    switch( count%8 ) {      
  4. 4.    case 0: do{ *to++ = *from++;      
  5. 5.    case 7:     *to++ = *from++;      
  6. 6.    case 6:     *to++ = *from++;      
  7. 7.    case 5:     *to++ = *from++;      
  8. 8.    case 4:     *to++ = *from++;      
  9. 9.    case 3:     *to++ = *from++;      
  10. 10.    case 2:     *to++ = *from++;      
  11. 11.    case 1:     *to++ = *from++;      
  12. 12.            }while(--n>0);      
  13. 13.    }      
  14. 14.}      
  15. 15.  
  16. 16.void duff_memcpy( char* to, char* from, size_t count ) {   
  17. 17.    size_t n = (count+7)/8;   
  18. 18.    switch( count%8 ) {   
  19. 19.    case 0: do{ *to++ = *from++;   
  20. 20.    case 7:     *to++ = *from++;   
  21. 21.    case 6:     *to++ = *from++;   
  22. 22.    case 5:     *to++ = *from++;   
  23. 23.    case 4:     *to++ = *from++;   
  24. 24.    case 3:     *to++ = *from++;   
  25. 25.    case 2:     *to++ = *from++;   
  26. 26.    case 1:     *to++ = *from++;   
  27. 27.            }while(--n>0);   
  28. 28.    }   
  29. 29.}   
  30. void duff_memcpy( char* to, char* from, size_t count ) {   
  31.     size_t n = (count+7)/8;   
  32.     switch( count%8 ) {   
  33.     case 0: do{ *to++ = *from++;   
  34.     case 7:     *to++ = *from++;   
  35.     case 6:     *to++ = *from++;   
  36.     case 5:     *to++ = *from++;   
  37.     case 4:     *to++ = *from++;   
  38.     case 3:     *to++ = *from++;   
  39.     case 2:     *to++ = *from++;   
  40.     case 1:     *to++ = *from++;   
  41.             }while(--n>0);   
  42.     }   
  43. }   

  44. void duff_memcpy( char* to, char* from, size_t count ) {
  45.     size_t n = (count+7)/8;
  46.     switch( count%8 ) {
  47.     case 0: do{ *to++ = *from++;
  48.     case 7:     *to++ = *from++;
  49.     case 6:     *to++ = *from++;
  50.     case 5:     *to++ = *from++;
  51.     case 4:     *to++ = *from++;
  52.     case 3:     *to++ = *from++;
  53.     case 2:     *to++ = *from++;
  54.     case 1:     *to++ = *from++;
  55.             }while(--n>0);
  56.     }
  57. }
复制代码
8、PHP中的字符串当函数用
PHP中的某些用法也是很怪异的

Php代码
  1. 1.$x = "foo";      
  2. 2.function foo(){ echo "wtf"; }      
  3. 3.$x();     
  4. 4.  
  5. 5.$x = "foo";   
  6. 6.function foo(){ echo "wtf"; }   
  7. 7.$x();  
  8. $x = "foo";   
  9. function foo(){ echo "wtf"; }   
  10. $x();  

  11. $x = "foo";
  12. function foo(){ echo "wtf"; }
  13. $x();
复制代码
9、在C++中,你可以使用空指针调用静态函数

Cpp代码
  1. 1.class Foo {      
  2. 2.  public:      
  3. 3.    static void bar() {      
  4. 4.      std::cout << "bar()" << std::endl;      
  5. 5.    }      
  6. 6.};     
  7. 7.  
  8. 8.class Foo {   
  9. 9.  public:   
  10. 10.    static void bar() {   
  11. 11.      std::cout << "bar()" << std::endl;   
  12. 12.    }   
  13. 13.};  
  14. class Foo {   
  15.   public:   
  16.     static void bar() {   
  17.       std::cout << "bar()" << std::endl;   
  18.     }   
  19. };  

  20. class Foo {
  21.   public:
  22.     static void bar() {
  23.       std::cout << "bar()" << std::endl;
  24.     }
  25. };
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-19 23:47 |只看该作者
很有意思哦
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP