ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int pc(unsigned n) {
return n ? (int)(n & 1u) + pc(n >> 1) : 0;
}
int main(void) {
printf("%d", pc(1023));
}- A. 9
- B. 11
- C. 10
- D. 1023
Correct answer: C. 10
Each call adds the low bit and recurses on n >> 1. The value 1023 has ten 1 bits.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (13 << 1) + 13); }
- How many numbers from 1 to 100 are multiples of 16? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 1u << 31; x <<= 1; printf("%u", x); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'n' == 10 && '0' == 0); }
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 0x1F; uint8t r =…
- What is printed? c #include <stdio.h> #include <stdlib.h> int ds(int n) { int s = 0; while (n) { s += n % 10; n /= 10; } return s; } int…