ProgrammingHard

int v = 0; for (const char *p = "-42"; *p >= '0' && *p <= '9'; p++) v = v * 10 + (*p - '0'); printf("%d", v); What is printed?

  1. A. Undefined behaviour
  2. B. 0
  3. C. 42
  4. D. 4

Correct answer: B. 0

The loop stops at the first character that is not a digit. '-' is not a digit, so the body never runs and v stays 0.

More C Programming questions

Exams that ask this topic