#include <stdio.h>
int main() {
int x = 5;
int y = x+++x++;
printf("%d %d ", x, y);
return 0;
}
Correct Answer: Compilation Error
Explanation: The expression `x+++x++` is invalid due to the way the increment operators are sequenced. It’s ambiguous and results in a compilation error.
#include <stdio.h>
int main() {
int a[] = {10, 20, 30, 40, 50};
int *ptr = (int*)(&a + 1);
printf("%d ", *(ptr - 1));
return 0;
}
Correct Answer: 50
Explanation: `&a + 1` gives the address just after the end of array `a`. Casting it to `int*` makes `ptr` point to this address. `*(ptr – 1)` then accesses the last element of `a`, which is `50`.
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
int *ptr1 = &a;
int *ptr2 = &b;
printf("%d ", *ptr1 + *ptr2);
return 0;
}
Correct Answer: 30
Explanation: `*ptr1 + *ptr2` adds the values pointed to by `ptr1` and `ptr2`, resulting in `10 + 20 = 30`.
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
(*ptr)++;
printf("%d ", x);
return 0;
}
Correct Answer: 11
Explanation: `(*ptr)++` increments the value pointed to by `ptr`, which is `x`. So, `printf(“%d “, x);` prints `11`.
#include <stdio.h>
#define SQUARE(x) x * x
int main() {
int y = 3 / SQUARE(3);
printf("%d ", y);
return 0;
}
Correct Answer: 1
Explanation: The macro `SQUARE(x)` expands to `x * x`. So, `3 / SQUARE(3)` expands to `3 / 3 * 3`, which evaluates as `(3 / 3) * 3 = 1 * 3 = 1`.
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
int *ptr = &a;
*ptr += 1;
ptr = &b;
*ptr += 1;
printf("%d %d ", a, b);
return 0;
}
Correct Answer: 6 11
Explanation: Initially, `ptr` points to `a`, so `*ptr += 1` increments `a` to `6`. Then, `ptr` is reassigned to point to `b`, and `*ptr += 1` increments `b` to `11`.
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
printf("%d ", *ptr--);
printf("%d ", *ptr);
return 0;
}
Correct Answer: Compilation Error
Explanation: The expression `*ptr–` is invalid because the decrement operator `–` cannot be applied to a dereferenced pointer in this manner. Hence, it results in a compilation error.
#include <stdio.h>
int main() {
int i = 1;
int *ptr = &i;
printf("%d ", *++ptr);
return 0;
}
Correct Answer: Compilation Error
Explanation: The expression `*++ptr` is invalid because it tries to increment `ptr` before dereferencing it, which is not allowed in this context. Hence, it results in a compilation error.
#include <stdio.h>
int main() {
int x = 5;
printf("%d ", x << 1 + 1);
return 0;
}
Correct Answer: 10
Explanation: Operator precedence makes `x << 1 + 1` equivalent to `x << (1 + 1)`, so `x << 2`. If `x` is `5`, then `5 << 2` is `20`, not `10`
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr1 = (int*)(&arr + 1);
int *ptr2 = (int*)((int)arr + 1);
printf("%d %d ", *(ptr1 - 1), *(ptr2));
return 0;
}
Correct Answer: 5 2
Explanation: `&arr + 1` gives the address just after the end of array `arr`. Casting it to `int*` makes `ptr1` point to this address, and `*(ptr1 - 1)` accesses the last element of `arr`, which is `5`. `(int)arr + 1` increments the address of `arr` by `1`, making `ptr2` point to `arr[1]`, which is `2`.
#include <stdio.h>
int main() {
int a[] = {10, 20, 30, 40, 50};
int *ptr = (int*)(&a + 1);
printf("%d %d ", *(ptr - 1), *(ptr - 2));
return 0;
}
Correct Answer: 50 40
Explanation: `&a + 1` gives the address just after the end of array `a`. Casting it to `int*` makes `ptr` point to this address. `*(ptr - 1)` accesses the last element of `a`, which is `50`. `*(ptr - 2)` accesses the second last element of `a`, which is `40`.
#include <stdio.h>
#define MUL(x, y) x * y
int main() {
int a = 2;
int b = 3;
printf("%d ", MUL(a + 1, b + 1));
return 0;
}
Correct Answer: 10
Explanation: The macro `MUL(x, y)` expands to `x * y`. So, `MUL(a + 1, b + 1)` expands to `a + 1 * b + 1`, which evaluates as `2 + 1 * 3 + 1 = 2 + 3 + 1 = 6`.
#include <stdio.h>
int main() {
int x = 2;
int y = x << 2 + 1;
printf("%d ", y);
return 0;
}
Correct Answer: 9
Explanation: Operator precedence makes `x << 2 + 1` equivalent to `x << (2 + 1)`, so `x << 3`. If `x` is `2`, then `2 << 3` is `16`.
#include <stdio.h>
int main() {
int a = 5;
int b = 2;
int c = a / b * 2;
printf("%d ", c);
return 0;
}
Correct Answer: 6
Explanation: `a / b * 2` evaluates as `(5 / 2) * 2 = 2 * 2 = 4`.
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
*ptr++;
printf("%d ", x);
return 0;
}
Correct Answer: 10
Explanation: `*ptr++` post-increments `ptr`, but does not affect `x` because `*ptr` is not assigned any value. So, `printf("%d ", x);` prints `10`.
#include <stdio.h>
int main() {
int a = 5;
int b = 2;
int c = a++ * --b;
printf("%d %d %d ", a, b, c);
return 0;
}
Correct Answer: 6 1 10
Explanation: `a++ * --b` evaluates as `5 * 1` (after `a` is incremented to `6` and `b` is decremented to `1`), resulting in `5`.
#include <stdio.h>
int main() {
int x = 5;
int *ptr = &x;
printf("%d ", ++*ptr);
printf("%d ", *ptr);
return 0;
}
Correct Answer: 6 5
Explanation: `++*ptr` increments the value pointed to by `ptr` (`x`) before printing it, so `printf("%d ", ++*ptr);` prints `6`. `printf("%d ", *ptr);` then prints `5`.
#include <stdio.h>
int main() {
int x = 5;
int *ptr = &x;
*ptr = *ptr + 1;
printf("%d ", x);
return 0;
}
Correct Answer: 6
Explanation: `*ptr = *ptr + 1;` increments the value pointed to by `ptr` (`x`) to `6`. So, `printf("%d ", x);` prints `6`.
#include <stdio.h>
int main() {
int x = 5;
int *ptr = &x;
(*ptr)++;
printf("%d ", x);
return 0;
}
Correct Answer: 6
Explanation: `(*ptr)++;` increments the value pointed to by `ptr` (`x`) to `6`. So, `printf("%d ", x);` prints `6`.
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3;
int *p1 = &a, *p2 = &b, *p3 = &c;
int *arr[] = {p1, p2, p3};
int **ptr = arr;
printf("%d ", **ptr++);
printf("%d ", **ptr);
return 0;
}
Correct Answer: 1 2
Explanation: `**ptr++` first dereferences `ptr` (pointing to `p1`), printing `1`. After `ptr++`, `ptr` points to `p2`, so `**ptr` prints `2`.
#include <stdio.h>
#define MIN(x, y) (x < y ? x : y)
int main() {
int a = 5, b = 10;
int c = MIN(a++, b++);
printf("%d %d %d", a, b, c);
return 0;
}
Correct Answer: 6 12 5
Explanation: The macro `MIN(x, y)` expands to `(x < y ? x : y)`. In `MIN(a++, b++)`, `a++` and `b++` are evaluated, but their values before incrementation (`5` and `10`) are used for comparison. So, `c` becomes `MIN(5, 10)` which is `5`. After the statement, `a` becomes `6` and `b` becomes `12`.
#include <stdio.h>
int main() {
int arr[3][2] = {{1, 2}, {3, 4}, {5, 6}};
int *ptr = arr[0];
printf("%d ", *(ptr + 1));
return 0;
}
Correct Answer: 2
Explanation: `arr[0]` is the first row of `arr`, which is `{1, 2}`. `ptr` points to `arr[0]`. `*(ptr + 1)` accesses the element at `ptr + 1`, which is `2`.
#include <stdio.h>
int main() {
int x = 10, y = 5;
int *ptr1 = &x;
int *ptr2 = &y;
ptr1 = ptr2;
*ptr1 = 20;
printf("%d %d", x, y);
return 0;
}
Correct Answer: 10 20
Explanation: `ptr1 = ptr2;` makes `ptr1` point to `y` instead of `x`. So, `*ptr1 = 20;` modifies `y` to `20`. Therefore, `printf("%d %d", x, y);` prints `10 20`.
#include <stdio.h>
int main() {
int x = 5;
int *ptr = &x;
*ptr += *ptr + 1;
printf("%d ", x);
return 0;
}
Correct Answer: 12
Explanation: `*ptr += *ptr + 1;` increments the value pointed to by `ptr` (`x`) by `5 + 1 + 1`, resulting in `12`. So, `printf("%d ", x);` prints `12`.
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
*ptr--;
printf("%d ", x);
return 0;
}
Correct Answer: 10
Explanation: `*ptr--;` post-decrements `ptr`, but does not affect `x` because `*ptr` is not assigned any value. So, `printf("%d ", x);` prints `10`.
#include <stdio.h>
int main() {
int x = 5;
int *ptr = &x;
int **pptr = &ptr;
**pptr += 1;
printf("%d ", x);
return 0;
}
Correct Answer: 6
Explanation: `**pptr += 1;` increments the value pointed to by `ptr` (`x`) by `1`, resulting in `6`. So, `printf("%d ", x);` prints `6`.
#include <stdio.h>
int main() {
int a = 2;
int b = 3;
int c = 4;
int *p1 = &a, *p2 = &b, *p3 = &c;
int *arr[] = {p1, p2, p3};
int **ptr = arr;
printf("%d ", **(ptr+1));
printf("%d ", **(ptr+2));
return 0;
}
Correct Answer: 3 4
Explanation: `**(ptr + 1)` accesses `p2`, and `**(ptr + 2)` accesses `p3`, printing `3` and `4` respectively.
#include <stdio.h>
int main() {
int x = 5;
int *ptr = &x;
printf("%d ", *ptr++);
printf("%d ", *ptr);
return 0;
}
Correct Answer: 5 5
Explanation: `*ptr++` post-increments `ptr` after dereferencing it, so it prints `5`. `*ptr` then prints `5` again.
#include <stdio.h>
int main() {
int a = 2;
int b = 3;
int c = 4;
int *p1 = &a, *p2 = &b, *p3 = &c;
int **arr[] = {&p1, &p2, &p3};
int ***ptr = arr[1];
printf("%d ", ***ptr);
return 0;
}
Correct Answer: 3
Explanation: `arr[1]` is `&p2`, so `***ptr` accesses `b`, printing `3`.
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
printf("%d ", *--ptr);
printf("%d ", *ptr);
return 0;
}
Correct Answer: Compilation Error
Explanation: `*--ptr` is invalid because `--ptr` tries to decrement `ptr` before dereferencing it. Hence, it results in a compilation error.