ProgrammingHardTCS NQT · Product company OA
What is printed?
What is printed?
#define CUBE(x) ((x) * (x) * (x))
int n = 0;
int f(void) { n++; return 2; }
int main(void) {
int r = CUBE(f());
printf("%d %d", r, n);
}- A. 27 3
- B. 6 3
- C. 8 1
- D. 8 3
Correct answer: D. 8 3
Full parentheses protect precedence but not side effects: the argument text f() is pasted three times, so f runs three times. Each call returns 2, so r = 8 and n = 3. A function cube(f()) would call f once.
More C Programming questions
- What is printed? c unsigned char rotl(unsigned char v, int k) { return (unsigned char)(v << k | v >> (8 - k)); } int main(void) {…
- With int a[3], p = a; which statement fails to compile?
- Assume 4-byte int. What is printed? c struct Msg { int len; char data[]; }; struct Msg m = malloc(sizeof m + 10); printf("%zu", sizeof m);
- What is printed? c printf("%zu %zu", sizeof("ab" "cd"), strlen("ab0cd"));
- What is printed? c int n = printf("%d", printf("%s", "abc")); printf(" %d", n);
- What is printed? c int f(int n, int b) { return n < b ? 1 : 1 + f(n / b, b); } int main(void) { printf("%d %d %d", f(1000, 2), f(1000…