ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint8_t a[] = {200, 100, 50};
    uint32_t s = 0;
    uint8_t t = 0;
    for (int i = 0; i < 3; i++) {
        s += a[i];
        t += a[i];
    }
    printf("%u %d", s, t);
}
  1. A. 350 94
  2. B. 350 44
  3. C. 300 44
  4. D. 94 94

Correct answer: A. 350 94

s is a 32-bit sum that holds 350, but t wraps modulo 256 at each step: 200, 300 - 256 = 44, then 94. Stopping at the second element gives 350 44.

More C Programming questions

Exams that ask this topic