+ 2

Output question

What is the output for this code? char str[]="%d %c", arr[]="Sololearn"; printf(str, 0[arr], 2[arr+3]); How is the output 83 e?

16th Jan 2021, 6:48 AM
Achintya Raj
Achintya Raj - avatar
3 Respuestas
+ 5
Same answer as Pąľľąvī , but I've explained a bit about what 0[arr] and 2[arr + e] means. arr[0] literally means *(arr + 0) because in C, variables containing array values are actually pointers to the first element of that array. So in the same way, 0[arr] literally means *(0 + arr), which is the same as *(arr + 0), which in turn, is the same as arr[0]. So you are passing arr[0], which is 'S', to the %d format specifier, which prints the ordinal value of 'S' In the same way as explained above 2[arr + 3] is the same as *(arr + 3 +2) = *(arr + 5) = arr[5] So you are passing arr[5], which is 'e', to the %c format specifier. The %c specifier is used to print values of char type to the screen. So it simply prints 'e'.
16th Jan 2021, 7:13 AM
XXX
XXX - avatar
+ 2
Thank you
16th Jan 2021, 7:29 AM
Achintya Raj
Achintya Raj - avatar