0

What is the output? (Explain)

/* Can someone give me a better solution and explain me how to get the output for this source code? */ #include <stdio.h> int main(void) { char *p = "\0\2\1\3\4"; printf("%d\n", p[ p[2] ] + *(p+1) + p[0] ); return 0; }

6th Apr 2020, 8:11 AM
Isuru Harischandra
Isuru Harischandra - avatar
1 Antwort
+ 2
This code is nonsense. It just tries to be hard to read but has no practical use. And it also ignores the type system of C. The first line in int main() is the same as int8_t p = [0, 2, 1, 3, 4]; (char is equal to a 8-bit integer so you can use int8_t from the stdint.h header as well). The \ escapes the characters so the string just contains the numeral values. *(p+1) is the same as p[1]. The rest is pretty self explanatory.
6th Apr 2020, 9:06 AM
Aaron Eberhardt
Aaron Eberhardt - avatar