ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int id_generator(void) {
    static int next_id = 100;
    return next_id++;
}
int main(void) {
    int a = id_generator();
    int b = id_generator();
    int c = id_generator();
    printf("%d %d %d", a, b, c);
}
  1. A. 101 102 103
  2. B. 100 101 101
  3. C. 100 101 102
  4. D. 100 100 100

Correct answer: C. 100 101 102

next_id++ returns the OLD value then increments: first call returns 100 (next_id becomes 101), second returns 101 (becomes 102), third returns 102 (becomes 103). a=100, b=101, c=102.

More C Programming questions

Exams that ask this topic