ProgrammingModerate
struct K { int k; }; struct K list[3] = {{3}, {1}, {2}}; int sum = 0; for (int i = 0; i < 3; i++) sum = sum * 10 + list[i].k; printf("%d", sum); What is printed?
- A. 123
- B. 312
- C. 321
- D. 6
Correct answer: B. 312
Each pass shifts sum one decimal place and appends list[i].k in array order 3, 1, 2: 3, 31, 312. 123 assumes the structs are sorted, 321 builds the digits in reverse, and 6 just adds the keys.
More C Programming questions
- struct Acc { int bal; }; void add(struct Acc a, int x) { a.bal += x; } struct Acc acc = {10}; add(acc, 5); printf("%d", acc.bal); What is…
- struct Acc { int bal; }; void add(struct Acc a, int x) { a->bal += x; } struct Acc acc = {20}; add(&acc, 7); printf("%d", acc.bal); What…
- struct Node { int v; struct Node next; }; struct Node head = NULL; for (int i = 1; i <= 3; i++) { struct Node n = malloc(sizeof n); n->v =…
- struct Pair { int a; int b; }; struct Pair p = {.b = 4}; printf("%d %d", p.a, p.b); What is printed?
- struct Cfg { int depth; int width; }; struct Cfg cfg[2] = {{1, 2}, {3, 4}}; struct Cfg cp = &cfg[1]; cp->width = 10; printf("%d"…
- struct Entry { char key[4]; int count; }; struct Entry e[2] = {{"ab", 1}, {"cd", 2}}; printf("%s %d", e[1].key, e[1].count); What is…