Problem Statement: Predict the output of following C program: Pick ONE option
MathWorks technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Problem Statement:**
Predict the output of following C program:
#include<stdio.h>
void main()
{
int i=6;
printf("%d %d %d %d %d\n", i1, i++, i*3, i--, i*i*2) ;
}
Pick ONE option
Show answer & explanation
The code contains undefined behavior. The expression i1 is interpreted as a variable name (not defined, so uninitialized—typically 0 in many environments). The remaining arguments use post-increment (i++), multiplication (i*3), post-decrement (i--), and multiplication (i*i*2). Due to unspecified evaluation order in printf's argument list, the exact sequence depends on the compiler, but the most common behavior yields: i1 (undefined, often 0), then i++ (6, post-incremented to 7), i*3 (7×3=21, but OCR/typo suggests 10 in options), i-- (7, post-decremented to 6), and i*i*2 (6×6×2=72, but option shows 6). This question exhibits undefined behavior and is non-portable.
Step-by-step Derivation:
Step-by-step trace with common compiler behavior:
- Initial: i=6
i1→ undefined variable, typically evaluates to 0i++→ returns 6, then i becomes 7i*3→ i is now 7, so 7×3=21 (option shows 10, suggesting OCR error or typo in problem)i--→ returns 7, then i becomes 6i*i*2→ i is now 6, so 6×6×2=72 (option shows 6, another discrepancy)
Note: The provided options contain apparent OCR errors or intentional distractors. Option A (0 7 10 7 6) is selected as the 'best match' but the code exhibits undefined behavior due to unspecified evaluation order and use of an undefined variable i1. This is a flawed question.