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.
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.
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)
Rajendra Singh said:
1 decade ago
The output depends upon compiler to compiler.
Under Turbo C output will "Gabage, 1, 2, 3, 4,"
Under Dev-C++ output will "1, 2, 3, 4, 5,"
But in statement "arr[i]=++i;" operator '++' has higher precedence than operator '='. So first pre-increment will take place then assignment operator. So the assignment will be as following.
arr[i]=++i; --> arr[1]=1;
arr[i]=++i; --> arr[2]=2;
arr[i]=++i; --> arr[3]=3;
arr[i]=++i; --> arr[4]=4;
arr[i]=++i; --> arr[5]=5;
But at arr[0], there will be garbage value. and arr[5] is out of bound.
Under Turbo C output will "Gabage, 1, 2, 3, 4,"
Under Dev-C++ output will "1, 2, 3, 4, 5,"
But in statement "arr[i]=++i;" operator '++' has higher precedence than operator '='. So first pre-increment will take place then assignment operator. So the assignment will be as following.
arr[i]=++i; --> arr[1]=1;
arr[i]=++i; --> arr[2]=2;
arr[i]=++i; --> arr[3]=3;
arr[i]=++i; --> arr[4]=4;
arr[i]=++i; --> arr[5]=5;
But at arr[0], there will be garbage value. and arr[5] is out of bound.
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.
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.
Hotcpu said:
1 decade ago
I try the online gcc compiler by adding the following code,
while(i<5)
{
arr[i]=++i;
printf("i=%d arr[%d]=%d,%d \n",i,i,arr[i],arr[i-1]);
}
The interesting thing is that in the first step,
it actually did: arr[0]=++0;
So, here is the question should ++ operator affect both side of "="?
while(i<5)
{
arr[i]=++i;
printf("i=%d arr[%d]=%d,%d \n",i,i,arr[i],arr[i-1]);
}
The interesting thing is that in the first step,
it actually did: arr[0]=++0;
So, here is the question should ++ operator affect both side of "="?
Sundar said:
1 decade ago
I have tested the above program in Turbo C and GCC.
The output of the program in Turbo C (DOS 16 bit OS):
816, 1, 2, 3, 4,
[Note: Here 816 is a garbage value.]
The output of the program in GCC (Linux 32 bit OS):
1, 2, 3, 4, 5,
I hope this will help you. Have a nice day!
The output of the program in Turbo C (DOS 16 bit OS):
816, 1, 2, 3, 4,
[Note: Here 816 is a garbage value.]
The output of the program in GCC (Linux 32 bit OS):
1, 2, 3, 4, 5,
I hope this will help you. Have a nice day!
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.
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?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers