ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
#define INCREMENT(x) x = x + 1
int main(void) {
int a = 5, b = 10;
if (a > 0)
INCREMENT(a);
else
INCREMENT(b);
printf("%d %d", a, b);
}- A. 6 10
- B. 5 11
- C. 6 11
- D. 5 10
Correct answer: A. 6 10
a > 0 is true, so INCREMENT(a) expands to `a = a + 1;`, making a=6; the else branch (which would have incremented b) is skipped entirely, so b remains 10.
More C Programming questions
- What happens? c int getDangling(void) { int local = 42; return &local; } int main(void) { int p = getDangling(); printf("%d", p); }
- What happens? (Signed integer overflow.) c #include <limits.h> int main(void) { int x = INTMAX; int y = x + 1; printf("%d", y); }
- What happens? c int p = malloc(sizeof(int)); p = 10; free(p); printf("%d", p);
- What happens? c int divide(int a, int b) { return a / b; } int main(void) { printf("%d", divide(10, 0)); }
- What is printed? c #define DEBUG 0 int main(void) { #if DEBUG printf("debug moden"); #else printf("release moden"); #endif return 0; }
- Two files are compiled and linked together. What happens? c / file1.c / static int counter = 0; void inc(void) { counter++; } / file2.c /…