C Programming - Arrays - Discussion

Discussion Forum : Arrays - Find Output of Program (Q.No. 8)
8.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    int arr[1]={10};
    printf("%d\n", 0[arr]);
    return 0;
}
1
10
0
6
Answer: Option
Explanation:

Step 1: int arr[1]={10}; The variable arr[1] is declared as an integer array with size '2' and it's first element is initialized to value '10'(means arr[0]=10)

Step 2: printf("%d\n", 0[arr]); It prints the first element value of the variable arr.

Hence the output of the program is 10.

Discussion:
17 comments Page 1 of 2.

Soumya K.K said:   2 decades ago
How is this possible?

#include<stdio.h>

int main()
{
int arr[1]={10};
printf("%d\n", 0[arr]);
return 0;
}


Someone plz clarify.

Krutika said:   1 decade ago
It is the same as declaring an array of a number of variables. Here it is an array of size 1. The usual syntax is used for initialising the array.

Vikarm G said:   1 decade ago
Yes, the array is of size '1'

Geetha said:   1 decade ago
Yes. If it is arr[1]=10; (without brackets) means arr[0]=0 and arr[1]=10.

Rupinder said:   1 decade ago
Here 0 acts as a pointer pointing to zeroth element of an array named arr.

Murthy said:   1 decade ago
for(i=o;i<n;i++)
printf("%d",a[i]);
and
for(i=0;i<n;i++)
printf("d",i[a]);
both gives the same value...
Someone please clarify this problem with how address calculation happens?

Arup said:   1 decade ago
arr[i] = i[arr].
So it is printing 10
Here it is given arr[1] = {10} that means arr[1] = {10,0}
So arr[0] = 10 and arr[1] = 0;

Satish said:   1 decade ago
arr[1]=10 means {0,10},
or arr[1]=10 means {10,0};
which one is true?
and arr[1] has length 1 or 2;

Amit agarwal said:   1 decade ago
The first thing is that array is of size 1.

arr[i]=*(arr+i)

i[arr]=*(i+arr)

So, 0[arr]=arr[0]

That's why answer is 10.
(1)

Moon said:   1 decade ago
#include<stdio.h>
int main()
{
int arr[7] = {1,2,3,4,5,6,7};
printf("%d\n",7[arr]);
return(0);
}

why it return's 2?


Post your comments here:

Your comments will be displayed after verification.