今天同MSRA的一个名校博士交流面试经验,他说前不久面一家公司,杯具到了一些C语言语法题上了。然后我问是啥题呢?他给我写了这些:
// Wrong code fragment!
int i = 5;
int z = ++i * ++i * ++i;
cout << z << endl;
// Wrong code fragment!
int count(int *p)
{
++*p;
}
int i = 5;
cout << sizeof(count(&i)) << ++i << endl;
我好久没搞C了,隐约记得当初看过C99标准规范,有说表达式单元在单个表达式中不能改变值。不过由于记忆不清,没有顶住实验派的诱惑。后来在编译器上瞎搞了半天,从反汇编中总结出来了一些所谓的求值规律。晚上回来找来C99标准规范翻了翻,擦!这错误的代码让人求值,不是坑爹么!
下面摘些C99标准规范,免得回头再去翻:
- 表达式求值
- sizeof运算符
但是sizeof是编译时执行,这点还是需要掌握的。最后,我不得不说面试这事儿真心靠人品,碰到一些自己语法没学好的面试官来问些蛋疼的问题就杯具了,这明显是找茬。这种地儿不去也罢!