ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
#define INCR(x) (++x)
int arr[3] = {10, 20, 30};
int i = 0;
printf("%d", arr[INCR(i)]);- A. 20
- B. 10
- C. Undefined behaviour
- D. 30
Correct answer: A. 20
INCR(i) expands to (++i), which increments i to 1 and evaluates to 1 (no double-use of i here since the macro only references x once), so arr[INCR(i)] becomes arr[(++i)] = arr[1] = 20. There is no undefined behaviour since i is modified exactly once in this expression.
More C Programming questions
- What is printed? c #define ISEVEN(n) (((n) % 2) == 0) int main(void) { int x = 7; if (ISEVEN(x)) printf("even"); else printf("odd"); }
- What happens when this runs? c int x; printf("%d", x);
- What happens when this runs? c char p; { char buf[20] = "hello"; p = buf; } printf("%s", p);
- What happens when this runs? c int a[5] = {1, 2, 3, 4, 5}; a[5] = 100; printf("%d", a[5]);
- What happens when this runs? c int p = NULL; printf("%d", p);
- What happens when this runs? c int p = malloc(sizeof(int)); free(p); printf("%d", p);