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.
Sam said:
1 decade ago
Thanx Devi...
Saisree said:
1 decade ago
When i becomes 4 the while condition becomes true..but inside the loop... i increments to 5.
Then doesn't the statement a[i]=++i; results in an error as the array size is exceeded ?
Then doesn't the statement a[i]=++i; results in an error as the array size is exceeded ?
(1)
Vimal Dhadul said:
1 decade ago
Saisree! That doesn't necessary to generate error but that's occupy value of appropriate address but if that address in use or in future that address will use so data may be crash.
Abcd said:
1 decade ago
Not convinced.
a[i]=++i;// when i=-1
//will be a[-1]=0 or wil be a[-1]=-1 first execution and then increment as per you guys say ?
a[i]=++i;// when i=-1
//will be a[-1]=0 or wil be a[-1]=-1 first execution and then increment as per you guys say ?
Sarath P C said:
1 decade ago
"Array Index" indicates the distance from the first element hence it must always begin with '0' since the first element is located at zero distance from itself.
This is a C Standard followed by all compilers.
Had the for loop been initialized with -1 the O/P would be :A since that is not the case I suggest the O/P is C :1, 2, 3, 4, 5.
Moreover setting FOR LOOP as for(i=-1;i<5;i++) generates an O/P :0, 0, 1, 2, 3, 4 in GCC compiler.
This is a C Standard followed by all compilers.
Had the for loop been initialized with -1 the O/P would be :A since that is not the case I suggest the O/P is C :1, 2, 3, 4, 5.
Moreover setting FOR LOOP as for(i=-1;i<5;i++) generates an O/P :0, 0, 1, 2, 3, 4 in GCC compiler.
Pankaj pandey said:
1 decade ago
In this i = -1.
After that arr[1] = ++i (i is prefix it increase 1 and i will -1+1=0).
And start value oi with 0.
So arr[0]=0.
arr[1]=1.
arr[2]=2.
arr[3]=3.
arr[4]=4.
And at arr[5] condition will be false and loop will terminate output printf.
So value is 0,1,2,3,4.
After that arr[1] = ++i (i is prefix it increase 1 and i will -1+1=0).
And start value oi with 0.
So arr[0]=0.
arr[1]=1.
arr[2]=2.
arr[3]=3.
arr[4]=4.
And at arr[5] condition will be false and loop will terminate output printf.
So value is 0,1,2,3,4.
Mathan kumar said:
1 decade ago
Someone please explain program clearly (step-by-step).
Sai Ram said:
8 years ago
Well said @Devi.
Aditya Dixit said:
8 years ago
In this Question, The Answer is 1,2,3,4,5 in GCC but in Turbo C it is 0,1,2,3,4 .. Soo the answer should depend on the Compiler and Output vary from compiler to compiler. If anyone from the website is reading this please change the options. Thank you.
(1)
Matan said:
7 years ago
There is a problem with the last iteratia of the loop.
I equals 4, while the condition is TRUE and then you want to access arr[5], that's impossible and the error I get is;
"stack around the variable arr was corrupted".
I equals 4, while the condition is TRUE and then you want to access arr[5], that's impossible and the error I get is;
"stack around the variable arr was corrupted".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers