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 2 of 4.
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.
Rishabh Manikpuri said:
8 years ago
Its answer is 1, 2, 3, 4, 5 and how does it showing garbage value in first place?
Dnyanu said:
8 years ago
Yes, Agree @Manoj.
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.
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.
Shreyash said:
10 years ago
It will be option C i.e 1.
0, 1, 2, 3, 4.
0, 1, 2, 3, 4.
Pooja dev said:
1 decade ago
But [] has higher precedence over = and ++.
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?
Azhar said:
1 decade ago
a++ and ++a is same when we are not assigning any variable to it.
Suppose given int a=1;
Then a++ = 2.
& ++a = also 2.
But give int a = 1, b = 1;
Find.
a = b++.
a = ++b.
Here b++ is post increment and ++b is pre increment, in ++b first increment the value of b by 1 then evaluate it & in b++ (post increment) first evaluate then increment it.
Eg: Pre increment value of b is 1 after increment it becomes 2, now evaluate, evaluate means a=b, the value of b is assign to a, so a=2.
So o/p is 1 = 2 b = 2.
Eg: For post increment.
First evaluate the value i.e a = b then increment the b value. O/p a = 1, b = 2.
Same for decrement.
Note: The value will change a/c to compiler.
Suppose given int a=1;
Then a++ = 2.
& ++a = also 2.
But give int a = 1, b = 1;
Find.
a = b++.
a = ++b.
Here b++ is post increment and ++b is pre increment, in ++b first increment the value of b by 1 then evaluate it & in b++ (post increment) first evaluate then increment it.
Eg: Pre increment value of b is 1 after increment it becomes 2, now evaluate, evaluate means a=b, the value of b is assign to a, so a=2.
So o/p is 1 = 2 b = 2.
Eg: For post increment.
First evaluate the value i.e a = b then increment the b value. O/p a = 1, b = 2.
Same for decrement.
Note: The value will change a/c to compiler.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers