ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int global = 100;
int *getGlobal() {
return &global;
}
int main() {
int *p = getGlobal();
*p = 200;
printf("%d %d", global, *getGlobal());
}- A. 200 200
- B. 100 100
- C. 200 100
- D. 100 200
Correct answer: A. 200 200
Unlike a local variable, global has static storage duration and lives for the whole program, so returning its address is always safe. Modifying *p changes global itself (200), and a fresh call to getGlobal() still returns the same address, so *getGlobal() also reads 200.
More C Programming questions
- What is printed? c void swap(int a, int b) { int temp = a; a = b; b = temp; } int main() { int x = 1, y = 2; int p = &x, q = &y; swap(&p…
- Assume 64-bit LP64, natural alignment. What is printed? c struct A { int i; char c; }; struct B { char c; int i; }; printf("%zu %zu"…
- Assume 64-bit LP64, natural alignment. What is printed? c struct S { double d; int i; }; struct S arr[3]; printf("%zu %zu", sizeof(struct…
- Assume 64-bit LP64. What is printed? c struct Node { int val; struct Node next; }; printf("%zu", sizeof(struct Node));
- Assume 64-bit LP64, natural alignment, and #pragma pack(1) applied. What is printed? c #pragma pack(1) struct Packed { char c; int i; char…
- Assume 64-bit LP64, natural alignment. What is printed? c struct A { int arr[3]; char c; }; printf("%zu", sizeof(struct A));