+ 1

int main() { char str[]="%d %c ",arr[]="sololearn"; Printf (str,0[arr],2[arr+3]); }

Why answer is 115 e

23rd Jul 2022, 5:00 AM
Always Learn More😎
Always Learn More😎 - avatar
5 odpowiedzi
+ 3
Always Learn More😎 the C compiler translates array syntax into pointer references. E.g., array[5] becomes *(array+5). In the posted code the array syntax has been swapped, but it makes no difference to the compiler, since addition can be done in either order. So 0[arr], which becomes *(0+arr) during compilation, would be the same as arr[0] or *(arr+0). That would be the letter 's' in "sololearn", which has ASCII numeric value of 115. It gets printed as digits because of the %d formatting that is specified in str. Next, 2[arr+3] translates to *(2+(arr+3)). This simplifies to *(arr+5), or arr[5]. Look in index position 5 of "sololearn". Starting with 0, count to 5 and you come to the 6th character. It is the letter 'e'. The letter prints with character formatting (%c) as specified by str so it outputs 'e'.
23rd Jul 2022, 8:04 AM
Brian
Brian - avatar
+ 2
Thanks I understood
23rd Jul 2022, 8:41 AM
Always Learn More😎
Always Learn More😎 - avatar
+ 1
Please move snippet into post Description and use the title section for writing the question. https://www.sololearn.com/Discuss/333866/?ref=app
23rd Jul 2022, 6:19 AM
Ipang
+ 1
Thanks
23rd Jul 2022, 6:34 AM
Always Learn More😎
Always Learn More😎 - avatar
0
To understand how the output came to be, you first need to understand what format specifiers are for. You can get to know them from this page. http://www.cplusplus.com/reference/cstdio/printf/
23rd Jul 2022, 7:56 AM
Ipang