c - Why is the output reversed along with the first character missing? -
this question has answer here:
- assigning more 1 character in char 2 answers
#include<stdio.h> int main() { char *a; char *temp ='55515'; = &temp; printf("%s ", a); }
the expected output 55515 actual output 5155?
'55515'
multi-character constant converted int
. platform has 32-bit int
s msb byte discarded, , resulting int
(int)0x35353135
. converted pointer char
in implementation-defined manner. platform little-endian platform, , char conversion retains int
value. value of pointer object laid out in memory
temp: | 0x35 | 0x31 | 0x35 | 0x35
or
| 0x35 | 0x31 | 0x35 | 0x35
it cannot deducted whether or not you're using 64-bit or 32-bit platform. make another pointer char *
points first byte of pointer object, i.e. byte 0x35
, printf
string %s
.
depending on platform printf
call has implementation-defined behaviour or possibly undefined behaviour - depends on whether pointers 32 or 64 bits wide - if 32, have undefined behaviour, if 64, you're depending on implementation-defined behaviour. in all, not strictly-conforming program rely on.
Comments
Post a Comment