ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
unsigned parse(const char *s) {
    unsigned n = 0;
    while (*s) n = n * 10 + (unsigned)(*s++ - '0');
    return n;
}
int main(void) {
    printf("%u", parse("4294967297"));
}
  1. A. 4294967297
  2. B. 1
  3. C. 0
  4. D. 2

Correct answer: B. 1

unsigned arithmetic wraps modulo 2^32, so 4294967297 becomes 4294967297 - 4294967296 = 1. Any value above 4294967295 wraps.

More C Programming questions

Exams that ask this topic