ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
#define IS_EVEN(n) (((n) % 2) == 0)
int main(void) {
int x = 7;
if (IS_EVEN(x))
printf("even");
else
printf("odd");
}- A. even
- B. odd
- C. Compile-time error: IS_EVEN is not a valid condition
- D. Undefined behaviour
Correct answer: B. odd
IS_EVEN(x) textually expands to (((x) % 2) == 0), which for x=7 is (7 % 2) == 0, i.e. 1 == 0, which is false, so the else branch runs, printing odd.
More C Programming questions
- 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);
- What happens when this runs? c int f(void) { int x = 5; return x; } int g(void) { int y = 10; return &y; } int main(void) { int p = g()…