C - 单引号与双引号

时间:2016-07-02 19:54:16

标签: c double-quotes single-quotes

我对这段代码感到好奇:

int a = 'ftyp';          // a == 1718909296
int b = *((int*)"ftyp"); // b == 1887007846

我的问题:为什么a!= b?

1 个答案:

答案 0 :(得分:8)

int a = 'ftyp';          // a == 1718909296

a设置为多字符常量,该常量具有实现定义的值。 a的值未由标准定义。有关详细信息,请参阅Single quotes vs. double quotes in C or C++

int b = *((int*)"ftyp"); // b == 1887007846
由于违反了strict aliasing

导致了未定义的行为。

期望a == b不健全。