Skip to content
GATEiit

GATE CSE 2026 Session 1 Q63: Number of printf calls with fork, continue and break

Question

Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always successful in creating a process.

int main () {
   int i;
   for (i = 0; i < 3; i++){
      if (fork() == 0){
        continue;
      }
      break;
   }
   printf("Hello!");
   return 0;
}

The total number of times that the printf statement gets executed is ________. (answer in integer)

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 4 (official GATE 2026 final answer key)

Concept used: After fork, the parent (non-zero return) breaks out of the loop while only the child keeps looping.

First step: Trace: each iteration the current process forks once and then exits the loop, while its child carries on; count the processes created plus the original.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2026 Session 1 CS question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key