ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int g = 1;
int f(int n) {
static int s = 10;
int g = n;
s += g;
{ extern int g; g += s; }
return s;
}
int main(void) {
int a = f(1);
int b = f(2);
printf("%d %d %d", a, b, g);
}- A. 11 12 1
- B. 11 13 25
- C. 11 13 1
- D. 11 12 24
Correct answer: B. 11 13 25
s is static, so it is initialised once and keeps its value: 11 after f(1), 13 after f(2). The local g shadows the global inside f, but the inner block re-declares g with extern, which refers to the file-scope object (external linkage), so that block updates the GLOBAL: 1 + 11 = 12, then 12 + 13 = 25. '11 13 1' assumes the inner block modified the local g; '11 12 …' assumes s is re-initialised on every call.
More C Programming questions
- What is printed? c int i = 0, j = 5, k = 0; int r = (i++ || j++) && (k++, i + j > 1) ? i++ : (j += 2, k); printf("%d %d %d %d", r, i, j, k);
- What is printed? c const char w[] = {"heap", "stack", "queue", "trie"}; const char p = w; printf("%c", ++p); printf("%c", ++p)…
- Assume 8-bit unsigned char. What is printed? c unsigned int count = 0; for (unsigned char i = 250; i != 4; i += 3) count++; printf("%u"…
- Assume 32-bit unsigned int and 64-bit unsigned long long. What is printed? c unsigned int a = 0x80000001u; unsigned int b = a 2; int c =…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c char s[] = "a0bc"; char p = s…
- What is printed? c int inc(int x) { return x + 1; } int dbl(int x) { return x 2; } int sqr(int x) { return x x; } int apply(int…