Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 350
codeDom Avatar asked Feb 06 '23 15:02

codeDom


1 Answers

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.

like image 138
R Sahu Avatar answered Feb 16 '23 03:02

R Sahu