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.
Prakhar said:
10 years ago
7[IndiaBIX] is same as IndiaBIX[7] and printf("%c", IndiaBIX[7]) will print character at 7th index of IndiaBIX.
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?
Krishna said:
10 years ago
Here 7["indiabix"].
7th no of char should be printed.
But I want to more than one char in given string.
How it possible?
7th no of char should be printed.
But I want to more than one char in given string.
How it possible?
Nagesh said:
9 years ago
Can anyone tell me if 7 is not mentioned in the code. Then what will be the output?
Thanks in advance.
Thanks in advance.
Manali said:
1 decade ago
char a[]="indiaBIX"
printf("%c \n",7[a]);
You will get same output.
printf("%c \n",7[a]);
You will get same output.
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.
Rajesh said:
9 years ago
Is it work for multi-dimensional array and what is syntax?
Please explain me.
Please explain me.
Bavya said:
8 years ago
Thanks for the explanation @Shabana.
Rhi said:
8 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].
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)].
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers