C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 7)
7.
What will be the output of the program in Turb C (under DOS)?
#include<stdio.h>
int main()
{
int arr[5], i=0;
while(i<5)
arr[i]=++i;
for(i=0; i<5; i++)
printf("%d, ", arr[i]);
return 0;
}
Answer: Option
Explanation:
Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the TurboC Compiler (Windows) output.
Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.
Discussion:
38 comments Page 1 of 4.
Rajesh_Reddy said:
7 years ago
@All.
arr[i] = ++i
It means ++ has higher precedence than =.
arr[i] = ++0
arr[i] = 1
Left is incremented than we assign array value to the right side.
arr[1] = 1
and so on arr[4] = 4
Therefore we are not assigning any value for arr[0], so it takes some garbage value
Try executing the below code, hope you will understand everything in detail
#include<stdio.h>
int main()
{
int arr[5], i=0;
while(i<5)
{
printf("%d %d ",i,arr[i]);
arr[i]=++i;
printf(" %d %d\n",i,arr[0]);
}
for(i=0; i<5; i++)
printf("%d, ", arr[i]);
return 0;
}
Correct me, if I am wrong.
arr[i] = ++i
It means ++ has higher precedence than =.
arr[i] = ++0
arr[i] = 1
Left is incremented than we assign array value to the right side.
arr[1] = 1
and so on arr[4] = 4
Therefore we are not assigning any value for arr[0], so it takes some garbage value
Try executing the below code, hope you will understand everything in detail
#include<stdio.h>
int main()
{
int arr[5], i=0;
while(i<5)
{
printf("%d %d ",i,arr[i]);
arr[i]=++i;
printf(" %d %d\n",i,arr[0]);
}
for(i=0; i<5; i++)
printf("%d, ", arr[i]);
return 0;
}
Correct me, if I am wrong.
(1)
Kalyan said:
1 decade ago
It will gives 1, 2, 3, 4, 5 how it will gives garbage value?
Nandhinivijayan said:
1 decade ago
What is meant by garbage value? How it is identified? Any one explain me clearly ASAP?
Pooja dev said:
1 decade ago
But [] has higher precedence over = and ++.
Shreyash said:
10 years ago
It will be option C i.e 1.
0, 1, 2, 3, 4.
0, 1, 2, 3, 4.
Umar said:
9 years ago
Why there is a garbage value?
at this:
"int arr[5], i=0;
while(i<5)
arr[i]=++i;"
The value stored in arr[0]=1 which is (++0) , then why it showed garbage value?
I am not understanding the logic, please someone help me.
at this:
"int arr[5], i=0;
while(i<5)
arr[i]=++i;"
The value stored in arr[0]=1 which is (++0) , then why it showed garbage value?
I am not understanding the logic, please someone help me.
Manoj kumar said:
9 years ago
When an array is partially initialised. Remaining elements will be turn out to be zero. So, I feel option is c.
Dnyanu said:
8 years ago
Yes, Agree @Manoj.
Rishabh Manikpuri said:
8 years ago
Its answer is 1, 2, 3, 4, 5 and how does it showing garbage value in first place?
Rupesh kumar jha said:
8 years ago
It depends upon compiler, the order of execution. In my system, linux (Ubuntu) GCC compiler it is giving output 1, 2, 3, 4, 5.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers