codeDom
codeDom

Reputation: 1779

C - single quotes vs double quotes

I'm curious about this code:

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

My question: Why a != b ?

Upvotes: 3

Views: 1502

Answers (1)

R Sahu
R Sahu

Reputation: 206717

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

sets a to the multi-character constant, which has implementation defined value. The value of a is not defined by the standard. See Single quotes vs. double quotes in C or C++ for more details.

int b = *((int*)"ftyp"); // b == 1887007846

is cause for undefined behavior due to violation of strict aliasing.

The expectation that a == b is ill founded.

Upvotes: 8

Related Questions