C Programming - Pointers - Discussion
Discussion Forum : Pointers - Find Output of Program (Q.No. 15)
15.
What will be the output of the program ?
#include<stdio.h>
int main()
{
printf("%c\n", 7["IndiaBIX"]);
return 0;
}
Discussion:
43 comments Page 2 of 5.
Venky said:
1 decade ago
Actually: num[i]<=>*(num+i)<=>*(i+num)<=>i[num]
So 7 is index and indiabix is string. Thus X of indiabix.
So 7 is index and indiabix is string. Thus X of indiabix.
Raghu said:
1 decade ago
It is just trick of mathematics. a[3] = 3[a] --> they are commutative both are expanded by compiler as *(a+3) and *(3+a) respectively. Even you can assign a variable to 3[a] = "X" which is same as a[3]="X".
Sanketrjain said:
1 decade ago
Thanks raghu and shamini.
Lavanya said:
1 decade ago
Thanks venky, raghu and shamini.
Sindhu said:
1 decade ago
Thanks wikiok.
Mesv said:
1 decade ago
No need to think more.
7["givenstring"] means it take 7th char of that string.
And string starts from 0 index.
Here "r".
7["givenstring"] means it take 7th char of that string.
And string starts from 0 index.
Here "r".
Devanshmody said:
1 decade ago
They have used %c so it will take one character and 7 is given and x is at 7 position.
Varun said:
1 decade ago
No it will not print all the characters after 6 characters. if we keep 7+"IndiaBIX" then we will get output as you told..@narendra
HARSHA said:
1 decade ago
It will print all characters after x in the string since there is only one character left it prints x
Prasad said:
1 decade ago
Suppose take an example...
int a[3]={10,20,30};
printf("\n%d%d%d",0[a],1[a],2[a]);
same way as a[0] a[2]....
in the above example 7 is treated as index.
So it takes 7th character
int a[3]={10,20,30};
printf("\n%d%d%d",0[a],1[a],2[a]);
same way as a[0] a[2]....
in the above example 7 is treated as index.
So it takes 7th character
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers