惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
MyScale Blog
MyScale Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
Y
Y Combinator Blog
月光博客
月光博客
IT之家
IT之家
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
L
LangChain Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
博客园 - Franky
WordPress大学
WordPress大学
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
V
Visual Studio Blog
小众软件
小众软件
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium

酷 壳 – CoolShell

感染新冠的经历 | 酷 壳 - CoolShell 从一次经历谈 TIME_WAIT 的那些事 | 酷 壳 - CoolShell 网络数字身份认证术 | 酷 壳 - CoolShell 我做系统架构的一些原则 | 酷 壳 - CoolShell 源代码特洛伊木马攻击 | 酷 壳 - CoolShell Go编程模式 : 泛型编程 | 酷 壳 - CoolShell 如何做一个有质量的技术分享 | 酷 壳 - CoolShell Go 编程模式:k8s Visitor 模式 | 酷 壳 - CoolShell Go编程模式:Pipeline | 酷 壳 - CoolShell Go编程模式:委托和反转控制 | 酷 壳 - CoolShell Go 编程模式:Go Generation | 酷 壳 - CoolShell Go编程模式:Map-Reduce | 酷 壳 - CoolShell Go 编程模式:错误处理 | 酷 壳 - CoolShell Go编程模式:切片,接口,时间和性能 | 酷 壳 - CoolShell 百度为什么掉队了 | 酷 壳 - CoolShell 程序员如何把控自己的职业 | 酷 壳 - CoolShell 计时攻击 Timing Attacks | 酷 壳 - CoolShell Rust语言的编程范式 | 酷 壳 - CoolShell HTTP的前世今生 | 酷 壳 - CoolShell 记一次Kubernetes/Docker网络排障 | 酷 壳 - CoolShell 可视化编程 | 酷 壳 - CoolShell 程序的本质复杂性和元语言抽象 | 酷 壳 - CoolShell 伙伴分配器的一个极简实现 | 酷 壳 - CoolShell C++11的Lambda使用一例:华容道求解 | 酷 壳 - CoolShell C++面试中string类的一种正确写法 | 酷 壳 - CoolShell C++模板”>>”编译问题与词法消歧设计 | 酷 壳 - CoolShell 数据即代码:元驱动编程 | 酷 壳 - CoolShell 数据的游戏:冰与火 | 酷 壳 - CoolShell 7个示例科普CPU Cache | 酷 壳 - CoolShell 加班与效率 | 酷 壳 - CoolShell
最为奇怪的程序语言的特性 | 酷 壳 - CoolShell
陈皓 · 2010-01-21 · via 酷 壳 – CoolShell

这些最为奇怪的程序语言的特性,来自stackoverflow.com,原贴在这里。我摘选了一些例子,的确是比较怪异,让我们一个一个来看看。 

1、C语言中的数组 

在C/C++中,a[10] 可以写成 10[a] 

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

这样的特性是不是很怪异?如果你想知道为什么的话,你可以看看本站的这篇文章——《C语言的谜题》中的第12题。 

2、在Javascript中 

 ‘5’ + 3 的结果是:’53’
 ‘5’ – 3 的结果是:2 

3、C/C++中的Trigraphs 

int main() {
   cout << "LOL??!";
}

上面的这段程序会输出: “LOL|”,这是因为 ??! 被转成了 | ,关于Trigraphs,下面有个表格: 

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

4、JavaScript 的条件表 

看到下面这个表,不难理解为什么Javascript程序员为什么痛苦了——《Javascript程序员嘴最脏??》 

[javascript]”        ==   ‘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[/javascript] 

5、Java的Integer cache 

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

为什么会这样呢?你需要了解一下Java Interger Cache,下面是相关的程序,注意其中的注释

/**
     * 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);
    }

5、Perl的那些奇怪的变量

[perl]$.
$_
$_#
$$
$[
@_[/perl]

其所有的这些怪异的变量请参看:http://www.kichwa.com/quik_ref/spec_variables.html 

6、Java的异常返回

请看下面这段程序,你觉得其返回true还是false?

try {
    return true;
} finally {
    return false;
}

在 javascript 和python下,其行为和Java的是一样的。 

7、C语言中的Duff device

下面的这段程序你能看得懂吗?这就是所谓的Duff Device,相当的怪异。

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);
    }
} 

8、PHP中的字符串当函数用

PHP中的某些用法也是很怪异的

$x = "foo";
function foo(){ echo "wtf"; }
$x();

9、在C++中,你可以使用空指针调用静态函数

class Foo {
  public:
    static void bar() {
      std::cout << "bar()" << std::endl;
    }
};
 
int main(void) {
  Foo * foo = NULL;
  foo->bar(); //=> WTF!?
  return 0; // Ok!
}

呵呵。的确是挺怪异的。

Loading...