您的位置:首页 > 编程语言 > C语言/C++

谁说C语言很简单?

2016-08-04 03:10 435 查看
http://blog.csdn.net/haoel/article/details/4217950


原文:http://cocre.com/?p=873

 

关于在回复里骂我的朋友们,无所谓哦,呵呵,大家有空可以看看我的另一篇文章《惹恼程序员的十件事》,我们可爱的程序员。


如果你觉得文章有意思,你可以看看我的另一篇文章——《C语言的谜题

 

前两天,Neo写了一篇《语言的歧义》其使用C语言讨论了一些语言的歧义。大家应该也顺便了解了一下C语言中的很多不可思异的东西,可能也是你从未注意到的东西。

是的,C语言并不简单,让我们来看看下面这些示例:
为什么下面的代码会返回0?(这题应该很简单吧)

 

<span style="color: rgb(0, 0, 255);">  int x;
return x == (1 && x);
</span>

本题主要是关于C/C++中变量初始化的问题。

 

为什么下面的代码会返回0而不是-1?
<span style="color: rgb(0, 0, 255);"> return ((1 - sizeof(int)) >> 32);
</span>

答案:sizeof 是一个unsigned的类型,所以……

代码作用域是一件很诡异的事,下面这个函数返回值是什么?

 

<span style="color: rgb(0, 0, 255);">int x = 5;
int f() {
int x = 3;
{
extern int x;
return x;
}
}
</span>

答案:5

函数和函数指针可以相互转换。下面的语句哪些是合法的?

 

<span style="color: rgb(0, 0, 255);">int (*pf)(void);
int f(void)
{

pf = &f; <span style="color: rgb(0, 128, 0);">// 没问题</span>
pf = ***f; <span style="color: rgb(0, 128, 0);">// 取址?</span>
pf(); <span style="color: rgb(0, 128, 0);">// 函数指针可以调用?
</span>   (****pf)();  <span style="color: rgb(0, 128, 0);">// 这又是什么?</span>
(***************f)(); <span style="color: rgb(0, 128, 0);">// 这个够变态了吧?</span>
}
</span>

答案:全部合法。

初始化可能是ISO C中最难的部分了。无论是MSVC 还是GCC 都没有完全实现。GCC 可能更接近标准。在下面的代码中,i.nested.y 和i.nested.z的最终值是什么?
<span style="color: rgb(0, 0, 255);">struct {
int x;
struct {
int y, z;
} nested;
} i = { .nested.y = 5, 6, .x = 1, 2 };
</span>

答案:2和6

下面这个示例是C语言的痛,main函数返回值是什么?
<span style="color: rgb(0, 0, 255);">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;
}
</span>

答案:1(你知道为什么吗?)

下面这个例就更变态了。在GCC的文档中,这个语法是合法的,但是不知道为什么GCC并没有实现。下面的代码返回 2.
<span style="color: rgb(0, 0, 255);"> return ((int []){1,2,3,4})[1];
</span>

 

在下面的这个示例中,有一个“bar” 函数及其函数指针 “pbar” 的两个拷贝(static 类型一般作用于语句块或文件域).
<span style="color: rgb(0, 0, 255);">  int foo() {
static bar();
static (*pbar)() = bar;

}

static bar() {
return 1;
}

static (*pbar)() = 0;
</span>

 

下面的这个函数返回值是什么?取决于编译器是先处理unsigned long转型,还是负号。
<span style="color: rgb(0, 0, 255);">  unsigned long foo() {
return (unsigned long) - 1 / 8;
}
</span>

如果是: ((unsigned long) - 1) / 8,那将是一个很大的数。
如果是: (unsigned long) (- 1 / 8 ), 那将是 0

是的,这样使用C语言可能很奇怪,不过我们可以从另一方面了解C语言的很多我们不常注意的特性。C语言其实并不容易。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: