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.
Roshni Rangrej said:
5 years ago
well explained @Rajesh_Reddy.
printf("%d %d ",i,arr[i]);
this will give output --> 0,arr[0]
1, arr[1] ......
While,
arr[i]=++i;
printf(" %d %d\n",i,arr[0]);
This will give output --> a[1]=1
a[2]=2.....
for(i=0; i<5; i++)
printf("%d, ", arr[i]);
This for loop will ask for arr[0], which has not been assign therefore it will print garbage value.
printf("%d %d ",i,arr[i]);
this will give output --> 0,arr[0]
1, arr[1] ......
While,
arr[i]=++i;
printf(" %d %d\n",i,arr[0]);
This will give output --> a[1]=1
a[2]=2.....
for(i=0; i<5; i++)
printf("%d, ", arr[i]);
This for loop will ask for arr[0], which has not been assign therefore it will print garbage value.
Shelly said:
5 years ago
Wonderfully explained. Thanks @Dada.
Seenu said:
5 years ago
Explain please.
Jai Shree Krishna said:
5 years ago
i=0;
while(i<5)
arr[i]=++i;
1) while(0<5) (bcoz i=0)
arr[i]=1 (bcoz ++0) --> arr[1] =1
2) while(1<5) (bcoz i=1)
arr[i]=2 (bcoz ++1) --> arr[2]=2
In the same way arr[3]=3 , arr[4]=4,
But arr[0] is not assigned any value bcoz of pre-increment, so it will give garbage value.
while(i<5)
arr[i]=++i;
1) while(0<5) (bcoz i=0)
arr[i]=1 (bcoz ++0) --> arr[1] =1
2) while(1<5) (bcoz i=1)
arr[i]=2 (bcoz ++1) --> arr[2]=2
In the same way arr[3]=3 , arr[4]=4,
But arr[0] is not assigned any value bcoz of pre-increment, so it will give garbage value.
Dada said:
6 years ago
1) As ++ have higher precidence than =
2) 1st. ++i will be done, so i become 1
3) so arr(i) =arr(1)
4) equation is=arr(1)=1
So no value assigned to arr(0).
So its garbage value.
2) 1st. ++i will be done, so i become 1
3) so arr(i) =arr(1)
4) equation is=arr(1)=1
So no value assigned to arr(0).
So its garbage value.
Mohan said:
6 years ago
I think operation takes from right to left.
Shravan venugopal said:
6 years ago
#include<stdio.h>
int main()
{
char s1[]="abcd",s2[]="abcd";
if(s1==s2)
{
printf("equal");
}
else
{
printf("not equal");
}
}
Can anyone please explain this?
int main()
{
char s1[]="abcd",s2[]="abcd";
if(s1==s2)
{
printf("equal");
}
else
{
printf("not equal");
}
}
Can anyone please explain this?
Shravan said:
6 years ago
Can anyone please explain this?
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)
Rohit said:
8 years ago
How the first value is a gargbage. I think output should be 1, 2, 3, 4, 5.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers