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 1 of 5.
Aju said:
6 years ago
@Bins Emmanuel.
Nice explanation, Thanks.
Nice explanation, Thanks.
Mounisha said:
6 years ago
["IndiabiX"].
0-I
1-n
2-d
3-i
4-a
5-b
6-i
7-X
7["IndiabiX"]= it prints X.
0-I
1-n
2-d
3-i
4-a
5-b
6-i
7-X
7["IndiabiX"]= it prints X.
(7)
Chandana said:
7 years ago
Thanks everyone for explaining.
(1)
Kishanth said:
7 years ago
Thank you all for explaining it.
Sandesh H said:
7 years ago
int main()
{
printf("%c\n", 7["IndiaBIX"]);//output=X. 7 is index. I=0, n=1,...X=7
printf("%c\n", "IndiaBIX"[7]);//output=X. both printf are same
printf("%d\n", 7["IndiaBIX"]);//output=88. prints ASCII value of X
printf("%c\n", *("IndiaBIX"+7));//output=X
printf("%c\n", *(7+"IndiaBIX"));//output=X
return 0;
}
For more details:
If "a" is an array and "i" is index.
a[i]<=>[i]a<=>*(a+i)<=>*(i+a) // all are same and in problem they have used 2nd case.
{
printf("%c\n", 7["IndiaBIX"]);//output=X. 7 is index. I=0, n=1,...X=7
printf("%c\n", "IndiaBIX"[7]);//output=X. both printf are same
printf("%d\n", 7["IndiaBIX"]);//output=88. prints ASCII value of X
printf("%c\n", *("IndiaBIX"+7));//output=X
printf("%c\n", *(7+"IndiaBIX"));//output=X
return 0;
}
For more details:
If "a" is an array and "i" is index.
a[i]<=>[i]a<=>*(a+i)<=>*(i+a) // all are same and in problem they have used 2nd case.
(2)
Rohan said:
7 years ago
@Rhi.
a [i] translates to *(a+i) but you have declared it as a array and i is its size.
So, if you declare i as an array and a its size i,e, , i [a]
Then only you can say that but they are not equal definitely like you have explained [*(a+i)=*(i+a)].
a [i] translates to *(a+i) but you have declared it as a array and i is its size.
So, if you declare i as an array and a its size i,e, , i [a]
Then only you can say that but they are not equal definitely like you have explained [*(a+i)=*(i+a)].
Rhi said:
7 years ago
Well in C, a[i] always gets translated to *(a + i)
So *(a + i) = *(i+ a)
a[i] = i[a].
So *(a + i) = *(i+ a)
a[i] = i[a].
Bavya said:
8 years ago
Thanks for the explanation @Shabana.
Rajesh said:
8 years ago
Is it work for multi-dimensional array and what is syntax?
Please explain me.
Please explain me.
Vinay Rao said:
9 years ago
The string "IndiaBIX" returns its base address.
Let us assume base address as 1000.
That is, an array name 'arr' also holds the base address of array arr.
We know that arr[7] == 7[arr] == * (arr + (7 * size of character 1 byte)).
Then in the above example,
7["IndiaBIX"] is calculated as below:
* (1000 + (7 * 1)) //where 1 is the size of char.
=> * (1007).
=> character stored in the location 1007.
=> X.
Hence X is the output.
Let us assume base address as 1000.
That is, an array name 'arr' also holds the base address of array arr.
We know that arr[7] == 7[arr] == * (arr + (7 * size of character 1 byte)).
Then in the above example,
7["IndiaBIX"] is calculated as below:
* (1000 + (7 * 1)) //where 1 is the size of char.
=> * (1007).
=> character stored in the location 1007.
=> X.
Hence X is the output.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers