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.
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)
Bins Emmanuel said:
1 decade ago
We can write a[i] = i[a].
Here they given second case,
Element[address]; i.e. When we are initializing a variable, it is mapping with address.
Eg: a ="indiabix".
a is stored with the base address of "indiabix" in code section.
So here,
7["indiabix"] = "indiabix"[7].
i.e assume address of indiabix stored in code section is 1000.
Then 1000[7].
1000[7] = *(1000+7) = *(1007) = x;
Note: The basic datatype of string is char, & ending with null char.
Here they given second case,
Element[address]; i.e. When we are initializing a variable, it is mapping with address.
Eg: a ="indiabix".
a is stored with the base address of "indiabix" in code section.
So here,
7["indiabix"] = "indiabix"[7].
i.e assume address of indiabix stored in code section is 1000.
Then 1000[7].
1000[7] = *(1000+7) = *(1007) = x;
Note: The basic datatype of string is char, & ending with null char.
(1)
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.
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)].
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".
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
Shabana said:
1 decade ago
Here in the program.
I - 0.
n - 1.
d - 2.
i - 3.
a - 4.
B - 5.
I - 6.
X - 7.
The compiler will skip all 6 character and it will 7th character X only because of 7["IndiaBIX"].
I - 0.
n - 1.
d - 2.
i - 3.
a - 4.
B - 5.
I - 6.
X - 7.
The compiler will skip all 6 character and it will 7th character X only because of 7["IndiaBIX"].
Manoj said:
1 decade ago
Hey program is good, because whenever complier can't define any data type or user define %c means char or you can modify this program or define %f,
%d then you will get satisfied answer.
%d then you will get satisfied answer.
Shamini said:
2 decades ago
printf("%c\n", 7["IndiaBIX"]);
It is printing the 7th character of the string "IndiaBIX".
I-0,
n-1,
d-2,
i-3,
a-4,
B-5,
I-6,
X-7.
Krishna said:
10 years ago
If I write,
printf("%c,%c \n", "infocom"[5][3]);
It show error:
Subscripted value is neither array nor pointer.
Why this is some one can explain me?
printf("%c,%c \n", "infocom"[5][3]);
It show error:
Subscripted value is neither array nor pointer.
Why this is some one can explain me?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers