Reputation: 23
Consider the following:
char abc[14] = "C Programming"; printf("%s", abc + abc[3] - abc[4]);
The output of the above printf statement is "rogramming". I can't seem to figure how this output is obtained.
Upvotes: 2
Views: 197
Reputation: 106012
abc
is an array. When used in expression, in most cases, it converted to pointer to its first element. abc[3]
is a char
which is 'r'
. abc[4]
is 'o'
. abc[3] - abc[4]
= 'r' - 'o' = 3
. abc + 3
= &abc[3]
.
So, the expression abc + abc[3] - abc[4]
is equivalent to pointer to 3rd charater of string "C Programming"
.
Upvotes: 4
Reputation: 7996
Because chars are a form of integers.
abc + abc[3] - abc[4]
==> abc + 'r' - 'o'
==> abc + 3
And thus you print the string abc starting at index 3.
Upvotes: 5