ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
int a = 5;
int b = a++ + ++a;
printf("%d %d", a, b);- A. Undefined behaviour: a is modified by both a++ and ++a within the same expression with no sequencing between them
- B. 12 7
- C. 11 12
- D. 7 12
Correct answer: A. Undefined behaviour: a is modified by both a++ and ++a within the same expression with no sequencing between them
a++ and ++a both modify the same object a within the single expression `a++ + ++a`, and + does not sequence its operands — two unsequenced modifications of the same scalar is undefined behaviour, so neither a nor b has a single portably guaranteed value.
More C Programming questions
- What is printed? c int a = 0; int b = (a = 5) == 5; printf("%d %d", a, b);
- What is printed? c #define ISEVEN(x) ((x) % 2 == 0) #define ISODD(x) (!ISEVEN(x)) int main(void) { int n = 7; printf("%d %d", ISEVEN(n)…
- What is printed? c int x = 7; int result = x % 2 == 0 ? x / 2 : x 3 + 1; printf("%d", result);
- What is printed? c #define PRINT(x) printf(#x " = %dn", x) int main(void) { int count = 5; PRINT(count); }
- What is printed? c #define MULTILINE(x) do { x = x 2; x = x + 1; } while (0) int main(void) { int v = 5; MULTILINE(v); printf("%d", v); }
- What is printed? c #define DEBUGPRINT(fmt, ...) printf(fmt, VAARGS) int main(void) { DEBUGPRINT("%d and %dn", 1, 2); }