最为奇怪的程序语言的特性
2010-11-26 00:53
204 查看
这些最为奇怪的程序语言的特性,来自stackoverflow.com,原贴在这里。我摘选了一些例子,的确是比较怪异,让我们一个一个来看看。
1、C语言中的数组
在C/C++中,a[10]可以写成10[a]
“HelloWorld”[i]也可以写成i["HelloWorld"]
这样的特性是不是很怪异?如果你想知道为什么的话,你可以看看本站的这篇文章——《C语言的谜题》中的第12题。
2、在Javascript中
’5′+3的结果是:’53′
’5′–3的结果是:2
3、C/C++中的Trigraphs
上面的这段程序会输出:“LOL|”,这是因为??!被转成了|,关于Trigraphs,下面有个表格:
4、JavaScript的条件表
看到下面这个表,不难理解为什么Javascript程序员为什么痛苦了——《Javascript程序员嘴最脏??》
5、Java的Integercache
为什么会这样呢?你需要了解一下JavaIntergerCache,下面是相关的程序,注意其中的注释
5、Perl的那些奇怪的变量
其所有的这些怪异的变量请参看:http://www.kichwa.com/quik_ref/spec_variables.html
6、Java的异常返回
请看下面这段程序,你觉得其返回true还是false?
在javascript和python下,其行为和Java的是一样的。
7、C语言中的Duffdevice
下面的这段程序你能看得懂吗?这就是所谓的DuffDevice,相当的怪异。
8、PHP中的字符串当函数用
PHP中的某些用法也是很怪异的
9、在C++中,你可以使用空指针调用静态函数
呵呵。的确是挺怪异的。
http://coolshell.cn/articles/2053.html
1、C语言中的数组
在C/C++中,a[10]可以写成10[a]
“HelloWorld”[i]也可以写成i["HelloWorld"]
这样的特性是不是很怪异?如果你想知道为什么的话,你可以看看本站的这篇文章——《
2、在Javascript中
’5′+3的结果是:’53′
’5′–3的结果是:2
3、C/C++中的Trigraphs
123 | int main(){ cout<< "LOL??!" ; } |
??= | # |
??( | [ |
??/ | \ |
??) | ] |
??’ | ^ |
??< | { |
??! | | |
??> | } |
??- | ~ |
看到下面这个表,不难理解为什么Javascript程序员为什么痛苦了——《
123456789 | '' == '0' //false 0== '' //true 0== '0' //true false == 'false' //false false == '0' //true false ==undefined //false false == null //false null ==undefined //true "\t\r\n" ==0 //true |
12345678910111213141516 | Integerfoo= 1000 ; Integerbar= 1000 ; foo<=bar; //true foo>=bar; //true foo==bar; //false //然后,如果你的foo和bar的值在127和-128之间(包括) //那么,其行为则改变了: Integerfoo= 42 ; Integerbar= 42 ; foo<=bar; //true foo>=bar; //true foo==bar; //true |
12345678910111213141516171819 | /** *Returnsa<tt>Integer</tt>instancerepresentingthespecified *<tt>int</tt>value. *Ifanew<tt>Integer</tt>instanceisnotrequired,thismethod *shouldgenerallybeusedinpreferencetotheconstructor *<ahref="mailto:{@link">{@link</a>#Integer(int)},asthismethodislikelytoyield *significantlybetterspaceandtimeperformancebycaching *frequentlyrequestedvalues. * *@paramian<code>int</code>value. *@returna<tt>Integer</tt>instancerepresenting<tt>i</tt>. *@since1.5 */ public static IntegervalueOf( int i){ if (i>=- 128 &&i<=IntegerCache.high) return IntegerCache.cache[i+ 128 ]; else return new Integer(i); } |
123456 | $. $_ $_ # $$ $[ @_ |
6、Java的异常返回
请看下面这段程序,你觉得其返回true还是false?
12345 | try { return true ; } finally { return false ; } |
7、C语言中的Duffdevice
下面的这段程序你能看得懂吗?这就是所谓的DuffDevice,相当的怪异。
1234567891011121314 | 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); } } |
PHP中的某些用法也是很怪异的
123 | $x = "foo" ; function foo(){ echo "wtf" ;} $x (); |
123456789101112 | class Foo{ public : static void bar(){ std::cout<< "bar()" <<std::endl; } }; int main( void ){ Foo*foo=NULL; foo->bar(); //=>WTF!? return 0; //Ok! } |
相关文章推荐
- 最为奇怪的程序语言的特性
- [转]最为奇怪的程序语言的特性
- <<汇编语言>>---王爽 (实验 8)分析一个奇怪的程序
- 新Orcas语言特性:扩展方法 为程序扩展方法
- 汇编语言---王爽--实验8-分析一个奇怪的程序
- 汇编语言(王爽)》(第九章、实验8分析一个奇怪的程序)学习笔记
- awk用法(二) ---程序语言特性
- C**语言特性整理以及三个程序例子
- 王爽汇编语言 一个奇怪的程序
- [go语言]channel的一个“奇怪”特性
- Js语言的奇怪特性
- 我想要的程序开发语言特性——之“面向对象”——之“退化”
- 汇编语言:实验8分析一个奇怪的程序
- 语言程序中语言超酷特性 之 --- 中语言标准库
- Hao语言--词法分析程序
- C#3.5语言新特性(二)-隐含类型局部变量
- 读取程序中的Text内容(根据语言)
- javaSE_8系列博客——Java语言的特性(三)--类和对象(2)--类(宏观概述)
- 选择学习“下一个”程序语言
- LINQ体验(3)——C# 3.0新语言特性和改进(下篇)