I accidentally stumbled upon this question which says find the output of the following code-
char abc[14] = "C Programming";
printf("%s", abc + abc[3] - abc[4]);
I am seriously not understanding how the output is coming- rogamming can somebody please explain me this?
It is very subtle, but the code exhibits undefined behavior. This is because the addition is done first, resulting in a pointer outside of the bounds of abc, which is undefined behavior, even if it is not dereferenced. The "correct" way would be to do the subtraction first.
char abc[14] = "C Programming";
printf("%s", abc + (abc[3] - abc[4]));
Even then, the result wouldn't be set in stone, as there is no guarantee that ASCII encoding is used for chars. But as ASCII is ubiquitous used we will ignore this.
Other answers deal explain the black magic behind the code, so I won't repeat it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With