ExamVeda
Login
Home
This question belongs to C Program Control Structures
Control Structures
?

What will be the value of i and j after execution of following program?
#include<stdio.h>
void main()
{
	int i, j;
	for(i=0,j=0;i<10,j<20;i++,j++){
		printf("i=%d %t j=%d", i, j);
       }
}

Answer & Solution
Correct Answer: Option C

comma operator is executed from left to right so until j<20 for loop statement is true, so both i and j are incremented.
So, i = 20 and j = 20.

Examveda
Question posted by Examveda
Community

Join the Discussion

7 Comments
Test Mail
Test Mail 4 years ago
i=19 j=19
Sudheer Sudheer
Sudheer Sudheer 6 years ago
When we use The Comma Operator compiler will choose the right most value....(Associativity is Left to Right)....So though when i=10 the condition in the for loop i
Sudheer Sudheer
Sudheer Sudheer 6 years ago
when i=20 and j=20 the condition becomes file... when i run this snippet in my editor , it gave me i=19 and j=19 ...So the answer D is incorrect in my opinion.
Shiva Gupta
Shiva Gupta 6 years ago
What would be value of j after the following is executed? k=17; j=6; if (k < 10) j=8; j=j+1; j=j+2;
Sanchita Akole
Sanchita Akole 8 years ago
Please Explain comma operator concept
Rohit More
Rohit More 8 years ago
Comma operator executed left to right is correct.
Abhijeet Chimankar
Abhijeet Chimankar 10 years ago
comma operator is executed from left to right so until j