ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
void f(void) {
int x = 5;
x = x++ + ++x;
printf("%d", x);
}
int main(void) { f(); }- A. Undefined behaviour: x is modified more than once between sequence points without a defining intervening sequencing
- B. 11
- C. 13
- D. 12
Correct answer: A. Undefined behaviour: x is modified more than once between sequence points without a defining intervening sequencing
x++ and ++x both modify x within the same expression `x++ + ++x`, with no sequence point separating these modifications (and the outer assignment to x adds yet another unsequenced write to the same object) — this is classic undefined behaviour in C (pre-C++17 rules for C still apply; C has no sequencing guarantee here), not a single well-defined numeric result.
More C Programming questions
- What is printed? c int f(void) { printf("f "); return 1; } int g(void) { printf("g "); return 0; } int main(void) { if (g() && f())…
- What is printed? c int f(void) { printf("f "); return 1; } int g(void) { printf("g "); return 1; } int main(void) { if (f() || g())…
- What is printed? c int globalVar = 100; void modify(void) { int globalVar = 200; globalVar += 5; printf("local:%d ", globalVar); } int…
- What is printed? c int a = 3; int result = a++ 2 + --a; printf("%d %d", a, result);
- What is printed? c // header.h (included by both files) extern int sharedValue; c // a.c #include "header.h" int sharedValue = 42; c //…
- What is printed? c int a = 5, b = 2, c; c = a > b ? a++ : --b; printf("%d %d %d", a, b, c);