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;
}
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.
OmarTarek said:
5 years ago
arr[1]={10};
Is the same as
arr[3]={5,69};
The first example is saved in memory like this;
arr[0]=10 and arr[1]=0 why equal to zero this is referred to the basics of the array you initialize the array in the same line of declaration the non initialized elements will be initialized by def to zero
The second example will be saved in memory like this;
arr[0]=5, arr[1]=69 and arr[2]= 0
And also to print contents of any array using for loop as a example
for (int i=0;i<3;i++)
printf("%d\n", arr[i]); //or printf("%d\n", i[arr]);
I hope you understand my simple explanation
Is the same as
arr[3]={5,69};
The first example is saved in memory like this;
arr[0]=10 and arr[1]=0 why equal to zero this is referred to the basics of the array you initialize the array in the same line of declaration the non initialized elements will be initialized by def to zero
The second example will be saved in memory like this;
arr[0]=5, arr[1]=69 and arr[2]= 0
And also to print contents of any array using for loop as a example
for (int i=0;i<3;i++)
printf("%d\n", arr[i]); //or printf("%d\n", i[arr]);
I hope you understand my simple explanation
Nitin singh chouhan said:
7 years ago
I am getting this output is 1010 why?
Noel said:
7 years ago
I think there is an error, arr[1], initializes an array with only 1 element, not two.
Prakash said:
8 years ago
Yes, arr[i] and i[arr] is same.
Raji said:
8 years ago
Is arr[i] and i[arr] same?
ASHISH BURMAN said:
9 years ago
Here, arr[1]=10;
It means that the value 10 is initialized to an array arr[] with index 1. The values arr[0] and arr[2] is not initialized if we want to access those elements then it will give the garbage values.
It means that the value 10 is initialized to an array arr[] with index 1. The values arr[0] and arr[2] is not initialized if we want to access those elements then it will give the garbage values.
(1)
Payal said:
9 years ago
@MOON. It will be 6 if you will give 7 then it will print garbage value.
int main()
{
int arr[7] = {1,2,3,4,5,6,7};
printf("%d\n",6[arr]);
return(0);
}
Output: 7
int main()
{
int arr[7] = {1,2,3,4,5,6,7};
printf("%d\n",6[arr]);
return(0);
}
Output: 7
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?
int main()
{
int arr[7] = {1,2,3,4,5,6,7};
printf("%d\n",7[arr]);
return(0);
}
why it return's 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.
arr[i]=*(arr+i)
i[arr]=*(i+arr)
So, 0[arr]=arr[0]
That's why answer is 10.
(1)
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;
or arr[1]=10 means {10,0};
which one is true?
and arr[1] has length 1 or 2;
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers