Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Language%20in%20C%20Interview%20Questions%20and%20Answers

Question: How come the output for both the programs is different when the logic is same?

main( )
{
int i, j ;

for ( i = 1, j = 1 ; i <= 5, j <= 100 ; i++, j++ )
{
gotoxy ( 1, 1, ) ;
printf ( "%d %d", i, j ) ;
}
}

main( )
{
int i, j ;

for ( i =1, j = 1; j <= 100, i <= 5; i++, j++ )
{
gotoxy ( 1, 1 ) ;
printf ( "%d %d", i, j ) ;
}
}

Output -> 5 5
Answer: Even if logic of both the programs is same the output of the first program comes out to be 100, 100, but of the second program it is 5, 5. The comma operator plays a vital role inside the for loop. It always considers the value of the latest variable. So, at the time of testing the condition in for loop, the value of j will be considered in the first program and value of i in the second.
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook