ProgrammingVery hardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int v[] = {-1, -2, -3};
    unsigned s = 0;
    for (int i = 0; i < 3; i++) s += (unsigned)v[i];
    printf("%u", s);
}
  1. A. 4294967291
  2. B. 4294967290
  3. C. 4294967289
  4. D. 4294967280

Correct answer: B. 4294967290

Converting each negative value to unsigned adds 2^32 - 1, 2^32 - 2 and 2^32 - 3. The total wraps to 2^32 - 6 = 4294967290.

More C Programming questions

Exams that ask this topic