本帖最后由 virfyf 于 2012-8-6 08:06 编辑
是的,C语言并不简单,让我们来看看下面这些示例: (Ctrl+A看答案)
为什么下面的代码会返回0?(这题应该很简单吧) [mw_shl_code=c,true]int x;
return x == (1 && x);[/mw_shl_code]
本题主要是关于C/C++中变量初始化的问题。
为什么下面的代码会返回0而不是-1? [mw_shl_code=c,true]return ((1 - sizeof(int)) >> 32);[/mw_shl_code]
答案:sizeof 是一个unsigned的类型,所以……
代码作用域是一件很诡异的事,下面这个函数返回值是什么? [mw_shl_code=c,true]int x = 5;
int f() {
int x = 3;
{
extern int x;
return x;
}
}[/mw_shl_code]
答案:5
函数和函数指针可以相互转换。下面的语句哪些是合法的? [mw_shl_code=c,true]int (*pf)(void);
int f(void)
{
pf = &f; // 没问题
pf = ***f; // 取址?
pf(); // 函数指针可以调用?
(****pf)(); // 这又是什么?
(***************f)(); // 这个够变态了吧?
}[/mw_shl_code]
答案:全部合法。
初始化可能是ISO C中最难的部分了。无论是MSVC 还是GCC 都没有完全实现。GCC 可能更接近标准。在下面的代码中,i.nested.y 和i.nested.z的最终值是什么?
[mw_shl_code=c,true]struct {
int x;
struct {
int y, z;
} nested;
} i = { .nested.y = 5, 6, .x = 1, 2 };[/mw_shl_code]
答案:2和6
下面这个示例是C语言的痛,main函数返回值是什么?
[mw_shl_code=c,true]typedef struct
{
char *key;
char *value;
} T1;
typedef struct
{
long type;
char *value;
} T3;
T1 a[] =
{
{
"",
((char *)&((T3) {1, (char *) 1}))
}
};
int main() {
T3 *pt3 = (T3*)a[0].value;
return pt3->value;
}[/mw_shl_code]
答案:1(你知道为什么吗?)
下面这个例就更变态了。在GCC的文档中,这个语法是合法的,但是不知道为什么GCC并没有实现。下面的代码返回 2.
[mw_shl_code=c,true]return ((int []){1,2,3,4})[1];[/mw_shl_code]
在下面的这个示例中,有一个“bar” 函数及其函数指针 “pbar” 的两个拷贝(static 类型一般作用于语句块或文件域)
[mw_shl_code=c,true] int foo() {
static bar();
static (*pbar)() = bar;
}
static bar() {
return 1;
}
static (*pbar)() = 0;[/mw_shl_code]
下面的这个函数返回值是什么?取决于编译器是先处理unsigned long转型,还是负号。 [mw_shl_code=c,true]unsigned long foo() {
return (unsigned long) - 1 / 8;
}[/mw_shl_code]
如果是: ((unsigned long) - 1) / 8,那将是一个很大的数。 如果是: (unsigned long) (- 1 / 8 ), 那将是 0
是的,这样使用C语言可能很奇怪,不过我们可以从另一方面了解C语言的很多我们不常注意的特性。C语言其实并不容易。(From CoolShell.cn) |