enggresources.com Comedk New!

what is the output of this code?

Post new topic Reply to topic Index -> Programming -> what is the output of this code?
Author Messages
Shyammani
Student
(Sambhram Institute of Technology)



Joined: 09/01/09

Post Posted: 2010-01-09 03:27:00 

#include

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;
}

1. garbage value 1 2 3 4
2. 1 2 3 4 5
3. 0 1 2 3 4
Prashanth Bhat
Student
(Bangalore Institute of Technology)



Joined: 18/10/06

Post Posted: 2010-01-12 03:04:14 

I thnk 1 2 3 4 5 .
Is that correct?
Vikas Singhal
Student
(ACHARAYA INSTITUTE OF TECHNOLOGY )



Joined: 29/09/06

Post Posted: 2010-01-21 11:30:20 

its the option 1 ..

garbage value 1 2 3 4
harsh rathod
Student
(Nirma University of Science & Technology, Ahmedabad)



Joined: 24/01/10

Post Posted: 2010-01-24 21:42:55 

output is

1 2 3 4 5
Anjana
Student
(oxford college of engineering)



Joined: 10/08/07

Post Posted: 2010-01-25 19:52:55 

it s 1 2 3 4 5
Anjana
Student
(oxford college of engineering)



Joined: 10/08/07

Post Posted: 2010-01-25 19:55:31 

actually since u r not incrementing i value in while,it will go into infinite loop.otherwise it is 1 2 3 4 5
sharat
Student
(Sir M. Visvesvaraya Institute of Technology, Bangalore)



Joined: 01/12/09

Post Posted: 2010-01-30 23:02:24 

output is garbage value 1 2 3 4

arr[i]=++i;
Is an mathematical expression. Involves =(assignment operator) and ++increment operator. Among these operators ++ has got highest precedence. Therefore ++ is evaluated first and then the evaluated value is assigned to the array,arr.


TRACE:

Loop 1:
while(i<5) //i=0
arr[i] = ++i; //++i is evaluated first. so i
//value is now 1. after evaluating
//++operator, = will be evaluated
//i.e value is assigned to the
// array,arr.
//arr[1]=1;

similarly,
arr[2]=2
arr[3]=3
arr[4]=4

This means values to the array are assigned from second element(i.e from a[1]). so the first element of array will contain garbage value(a[0]=garbage).
anitha
Student
(Gsss Institute of Engineering and Technology For Women, Myso)



Joined: 22/06/09

Post Posted: 2010-02-02 19:06:19 

The output of the code is 12345 because
a[0]=1// since ++i means increment first n then assign the value
a[1]=2
a[2]=3
a[3]=4
a[4]=5 // the loop fails next as 5<5 is false
siddhu
Student
()



Joined: 06/02/10

Post Posted: 2010-02-06 15:06:21 


Vinayaka.v.m.
Student
(S.J.M.I.T)



Joined: 21/12/08

Post Posted: 2010-02-06 15:22:25 

1 2 3 4 5

The above is the output but in order to see the output we have to press F4 because the program doesn't wait for the user. It returns 0 and terminates. To avoid the immediate termination we can use getch() at the end of the program.
vandana
Student
(Visvesvaraya Technological University, Belgaum)



Joined: 07/10/09

Post Posted: 2010-02-07 12:20:32 

its garbage value 1234
ANANTH
Student
(Cambridge Institute of Technology, Bangalore)



Joined: 13/02/10

Post Posted: 2010-02-13 20:10:58 

it is garbage value 1 2 3 4... becoz the stmt arr[i]=++i; is executed as follws.... i=i+1;(first increment)
arr[i]=i;(then use the value of i)
also since arr[] is not initialised it contains garbage vvalues before which is then overwritten.
Karthik R
Student
(J C Engineering College, Mysore)



Joined: 03/11/08

Post Posted: 2010-03-18 09:47:28 

it is option .1. garbage,1,2,...

reason:
right hand side of the expression will be evaluated first before it sees the left side. (compiler design). parser looks for the higher precedence and associativity to evalute .
since statement a[something]=++i; ++--> is having higher precedence so evaluation start from there . after increment variable i gets the value of '1' instead of '0'. a[0] is not assigned with any value it print s chunk or garbage value to the screen .... next steps the loops keeps assigning the value.
Nikhil
Student
(East Point College of Engg and Tech.)



Joined: 10/12/08

Post Posted: 2010-07-30 22:34:14 

its a garbage value and 1234
Nikhil
Student
(East Point College of Engg and Tech.)



Joined: 10/12/08

Post Posted: 2010-07-30 22:34:43 

its a garbage value and 1234
sadiq
Student
(amc engg collage)



Joined: 06/08/10

Post Posted: 2010-08-25 15:23:49 

its garbage value
naveen kumar suman
Student
(B.N.M.Institute of Technology, Bangalore)



Joined: 10/10/09

Post Posted: 2010-09-05 15:31:48 

the answer is
1 2 3 4 5
Back to top  
Post new topic Reply to topic Index -> Programming -> what is the output of this code?

 
Jump to: