ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int main(void) {
    int arr[3] = {1, 2, 3};
    int *p = arr;
    for (int i = 0; i < 3; i++, p++) {
        printf("%d ", *p);
    }
}
  1. A. 3 2 1
  2. B. 1 1 1
  3. C. 1 2 3
  4. D. 2 3 4

Correct answer: C. 1 2 3

The comma in the for-loop's increment clause `i++, p++` executes both expressions each iteration (both i and p advance together); p starts at arr and walks alongside i, printing arr[0], arr[1], arr[2] in order: 1 2 3.

More C Programming questions

Exams that ask this topic