ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
char s[] = "7392";
int n = 0;
for (int i = 0; s[i]; i++) n = n * 10 + (s[i] - '0');
printf("%d %d", n % 1000, n / 100 % 10);
  1. A. 392 9
  2. B. 392 3
  3. C. 92 3
  4. D. 7392 3

Correct answer: B. 392 3

The loop builds the number 7392. n % 1000 keeps the last three digits, 392. / and % have equal precedence and group left to right, so n / 100 % 10 = 73 % 10 = 3, the hundreds digit.

More C Programming questions

Exams that ask this topic