ProgrammingVery hardTCS NQT · Infosys

What is printed? Assume unsigned int is 32 bits.

What is printed? Assume unsigned int is 32 bits.
#include <stdio.h>
int main(void) {
    unsigned s = 0;
    for (unsigned i = 1; i <= 100000; i++)
        s += i;
    printf("%u", s);
}
  1. A. 705082704
  2. B. 5000050000
  3. C. 705082703
  4. D. 4294967295

Correct answer: A. 705082704

The true sum 5000050000 wraps modulo 2^32, so the stored value is 5000050000 - 4294967296 = 705082704. An unsigned long long accumulator would print 5000050000.

More C Programming questions

Exams that ask this topic