C Programming - Arrays - Discussion
Discussion Forum : Arrays - Find Output of Program (Q.No. 11)
11.
What will be the output of the program in Turbo-C ?
#include<stdio.h>
int main()
{
int arr[5], i=-1, z;
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 Turbo-C Compiler (under DOS) output.
Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.
Discussion:
20 comments Page 2 of 2.
Srikanth said:
2 decades ago
The output for the above program
In Turbo C :
0, 1, 2, 3, 4,
In GCC :
1,2,3,4,5,
In Turbo C :
0, 1, 2, 3, 4,
In GCC :
1,2,3,4,5,
Govindarasu.N said:
1 decade ago
In the program, we are using int main() so that we must use return 0, otherwise if you use void main then not neccessary to use return 0.
Abhishek Kamboj said:
1 decade ago
Assignment operator has Right to left associativity, that is the reason why increment operation at arr[i]=++i; will take first.
Venkateswarareddy said:
1 decade ago
What about return (0) ;
Shyamkumar said:
1 decade ago
Thank you devi.
Smriti said:
1 decade ago
Thanks devi.
Devi said:
1 decade ago
In Turbo C, Answer is 0,1,2,3,4.
In the expression arr[i]=++i, increment operation will be done first and then only assignment will be done.
Initially i value is -1.
-1 is incremented to 0 when ++i is executed and hence now the value of i will be 0 and arr[0]=0. similarly arr[1]=1 and so on. :-)
In the expression arr[i]=++i, increment operation will be done first and then only assignment will be done.
Initially i value is -1.
-1 is incremented to 0 when ++i is executed and hence now the value of i will be 0 and arr[0]=0. similarly arr[1]=1 and so on. :-)
Gaurav said:
1 decade ago
How can answer be 0 1 2 3 4.. it seems wrong :-(
but it is right... i dont know how !!! wish someone could explain.
but it is right... i dont know how !!! wish someone could explain.
Ankit said:
1 decade ago
How could array index be -1 ?
Braj said:
1 decade ago
Its output is :.
1 2 3, 4, 5.
1 2 3, 4, 5.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers