ProgrammingModerateGATE CS · Infosys
Assume int is 4 bytes. What is printed?
Assume int is 4 bytes. What is printed?
#include <stdio.h>
int main(void) {
int a[10];
int *p = &a[7], *q = &a[2];
printf("%ld %ld", (long)(p - q), (long)(q - p));
}- A. 7 -2
- B. 5 -5
- C. 4 -4
- D. 6 -6
Correct answer: B. 5 -5
Pointer subtraction gives the number of ints between the two elements, here 7 - 2 = 5, and reversing the operands gives -5. Subtracting the other way round in the printed order is the trap.
More C Programming questions
- Assume a pointer is 8 bytes and int is 4 bytes. What is printed? c #include <stdio.h> void f(int v[]) { printf("%zu ", sizeof v); } int…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { char s[] = "hi0there"; printf("%zu %zu", sizeof s, strlen(s)); }
- What is printed? c #include <stdio.h> int A(int m, int n) { if (m == 0) return n + 1; if (n == 0) return A(m - 1, 1); return A(m - 1, A(m…
- What is printed? c #include <stdio.h> unsigned long long g(unsigned n, unsigned long long acc) { return n == 0 ? acc : g(n - 1, acc 3 +…
- What is printed? c #include <stdio.h> long long memo[50]; int calls = 0; long long f(int n) { calls++; if (n < 2) return 1; if (memo[n])…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { int x = (const int )a, y = (const int )b…