GATE Exam  >  GATE Questions  >  What will the output of the following C code ... Start Learning for Free
What will the output of the following C code ?
#include <stdio.h>
int main()
{ int i=2, j=2;
while (i+1 ? --i : j++)
printf("%d",i);
return 0;
}
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'A'. Can you explain this answer?
Verified Answer
What will the output of the following C code ?#include <stdio.h>...
Consider the while loop condition => i + 1 ? --i : j++
In first iteration : i + 1 = 3 (true)
So ternary operator will return --i i.e. 1, condition part is 1 means true
so while condition is true.
Hence printf statement will print 1 In second iteration : i + 1 =2 (true)
So ternary operator will return --i i.e 0, condition part is 0 means false
so while condition is false.
Hence program control will come out of the while loop.
View all questions of this test
Most Upvoted Answer
What will the output of the following C code ?#include <stdio.h>...

Explanation:

- The code provided is a C program that uses a while loop to iterate until a certain condition is met.
- Initially, the variables i and j are both set to 2.
- In the while loop condition, the expression i+1 ? --i : j++ is evaluated.
- If i+1 is true (non-zero), then --i is executed, which decrements the value of i by 1.
- If i+1 is false (zero), then j++ is executed, which increments the value of j by 1.
- The printf statement inside the loop prints the value of i after each iteration.

Execution:

- In the first iteration, i+1 is equal to 3 (true), so --i is executed, decrementing i to 1.
- In the second iteration, i+1 is equal to 2 (true), so --i is executed again, decrementing i to 0.
- In the third iteration, i+1 is equal to 1 (true), so --i is executed again, decrementing i to -1.
- Since i+1 is now false (zero), the loop exits.

Output:

- The output of the program will be 1, as the loop iterates and decrements the value of i until the condition i+1 is false.
Explore Courses for GATE exam
What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer?
Question Description
What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer?.
Solutions for What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer?, a detailed solution for What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer? has been provided alongside types of What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What will the output of the following C code ?#include <stdio.h>int main(){ int i=2, j=2;while (i+1 ? --i : j++)printf("%d",i);return 0;}a)1b)2c)3d)4Correct answer is option 'A'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev