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);
}
}
#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);
}
}
A. 10 10
B. 10 20
C. 20 20
D. Run time error
Answer: Option C
Solution(By Examveda Team)
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.
Join The Discussion
Comments ( 7 )
Which control structure is used to repeatedly execute a block of code in C?
A. for loop
B. if statement
C. switch case
D. while loop
In C, what is the purpose of the 'break' statement within a loop?
A. Continue to the next iteration
B. Exit the program
C. Terminate the loop and exit it
D. Skip the current iteration
What is the purpose of the 'else' statement in C's 'if-else' control structure?
A. Execute the 'if' block
B. Execute the 'else' block
C. Execute both 'if' and 'else' blocks
D. Skip the 'if' block
Which control structure is used to make a decision between two or more alternatives in C?
A. switch case
B. for loop
C. if statement
D. while loop
i=19 j=19
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
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.
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;
Please Explain comma operator concept
Comma operator executed left to right is correct.
comma operator is executed from left to right so until j