Assignment Operators Interview Questions for Embedded C: Quiz and Code Challenges
61 questions: multiple choice, write-the-statement blanks, a compound-operator table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for assignment operators interview questions.
61
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0 Correct
0 Wrong
0 Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.
These assignment operators interview questions in multiple-choice form are typical of written screening tests. Click an answer; explanations always appear.
1. With int i = 5;, what does int a = i++; leave in a and i?
Postfix yields the old value, then increments.
2. With int i = 5;, what does int a = ++i; leave in a and i?
Prefix increments first and yields the new value.
3. What is x after int x; x = (1, 2, 3);?
The comma operator yields its last operand.
4. What is the value of y after int y; y = 1, 2;?
= has higher precedence than the comma: (y = 1), 2.
5. Which of these is undefined behavior?
i is modified twice without a sequence point.
6. What type does c ? 1 : 2.5 have?
Usual arithmetic conversions give both branches the type double, whatever c is.
7. What does a += b * 2 mean?
The whole right side is evaluated first.
8. Which has the LOWEST precedence?
The comma operator is lowest of all.
9. Which is the correct postfix/prefix precedence relation to * in *p++?
Postfix ++ binds tighter than *: it is *(p++), and the old address is dereferenced.
10. What does (*p)++ do?
The parentheses force the dereference first.
11. What does sizeof return for an int a[10] on STM32F407?
10 elements of 4 bytes.
12. Inside void f(int a[10]), sizeof(a) equals:
An array parameter decays to a pointer.
13. Which expression is evaluated twice in #define MAX(a,b) ((a)>(b)?(a):(b)) when called as MAX(x++, y)?
The macro pastes the argument text more than once.
14. What does while (n--) do with n = 3?
The test uses the old value; after the final false test n has been decremented to -1 (for a signed type).
15. Which declaration is a legal use of the comma operator?
The first and third are separators, not operators.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the statement for the first four items (spacing and the semicolon do not matter) and a short value for the rest.
1. Write a statement that doubles x using a compound operator:
x *= 2;
2. Write a statement that shifts reg right by 3 using a compound operator:
reg >>= 3;
3. Write a statement that clears bit 4 of reg with a compound operator:
reg &= ~(1u << 4);
4. Write a one-line statement that puts the larger of a and b in m:
m = (a > b) ? a : b;
5. int i = 7; int r = i-- + 1; gives r =
The old value 7 is used.
6. int i = 7; int r = --i + 1; gives r =
–i is 6, plus 1.
7. After int i = 7; int r = i-- + 1;, i =
Decremented after use.
8. sizeof(uint16_t[6]) = bytes
6 × 2.
9. int x = 10; x %= 4; gives x =
10 mod 4.
10. int x = 3; x <<= 2; gives x =
3 × 4.
11. int a = (4, 9); gives a =
Last operand.
12. 5 > 3 ? 10 : 20 =
The condition is true.
Section 3: Which Compound Operator Fits?
Pick the operator from the options, as in assignment operators interview questions about operator choice.
Statements typical of assignment operators interview questions.
1. a = b = 5; is legal and assigns 5 to both.
Assignment is right-associative and yields the assigned value.
2. i++ and ++i are identical when used as a standalone statement.
The yielded value is discarded, so only the side effect remains.
3. The expression i = i++; is well defined and leaves i unchanged.
It is undefined behavior in C.
4. The two branches of ?: are both always evaluated.
Only the selected branch is evaluated.
5. sizeof is a function that runs at run time.
It is a compile-time operator (except for variable-length arrays).
6. In for (i = 0, j = 5; ...) the comma is the comma operator.
It evaluates both initialisations in order.
7. x += y may evaluate x only once, while x = x + y evaluates it twice.
This matters when x has side effects such as a[i++] += 2.
8. if (x = 5) compares x with 5.
It assigns 5 and tests the result, which is always true.
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of assignment operators interview questions.
Challenge 1 — Output?: Two modifications of one object
#include <stdio.h>int main(void)
{
int i = 3;
int a = i++ + ++i;
printf(“%d\n”, i);
return0;
}
What does this print, and is it reliable?
Output (GCC on this host):
5
The expression modifies i twice without a sequence point, so the behavior is undefined. Another compiler or optimisation level may print something else. Never write such code.
Challenge 2 — Output?: Post then pre
#include <stdio.h>int main(void)
{
int i = 2;
int a = i++ * 3;
int b = ++i * 3;
printf(“%d %d %d\n”, a, b, i);
return0;
}
What are a, b and i?
Output:
6 12 4
a = 2 × 3 = 6 (i becomes 3). Then i becomes 4 and b = 4 × 3 = 12.
Challenge 3 — Find the bug: Assignment in a condition
#include <stdio.h>int main(void)
{
unsigned x = 5;
if (x = 0)
printf(“zero\n”);
else
printf(“not zero\n”);
printf(“x=%u\n”, x);
return0;
}
What is printed and what was probably intended?
Output:
not zero
x=0
x = 0 assigns 0, and the value 0 is false. The intended test was x == 0. Compile with -Wall and write constants on the left (0 == x) to catch this.
The first conditional has type double (8 bytes) even though the condition is false and the int branch would be chosen; the second has type int (4 bytes).
Challenge 5 — Output?: Pointer, postfix and parentheses
*p++ prints A and moves p to B. *p prints B. (*p)++ prints B (old value) and changes it to C. The last *p prints C.
Challenge 6 — Output?: Comma between compound assignments
#include <stdio.h>int main(void)
{
int y = 8;
y -= 3, y *= 2;
int z = (y > 8) ? y / 2 : y * 2;
printf(“%d %d\n”, y, z);
return0;
}
What are y and z?
Output:
10 5
The comma sequences the two statements: y = 8 – 3 = 5, then 5 × 2 = 10. Since 10 > 8, z = 10 / 2 = 5.
Section 6: Assignment Operators Interview Questions for Embedded C (with Detailed Answers)
Assignment operators interview questions test whether you understand side effects, precedence and undefined behavior, which cause real field failures. Click a question for a model answer.
Q1. What is the difference between ++i and i++?Basic+
Both increment i. ++i yields the new value, i++ yields the old value. As a standalone statement they behave the same; in an expression they differ. For built-in types a modern compiler generates identical code when the result is unused.
prepost_iq.c
#include <stdio.h>int main(void)
{
int i = 5, a = i++, b = ++i;
printf(“a=%d b=%d i=%d\n”, a, b, i);
return0;
}
Output (verified by compiling and running)
a=5 b=7 i=7
Q2. What does a += b do and why use it?Basic+
It is equivalent to a = a + b except that a is evaluated only once. It is shorter and less error-prone, and when a has side effects, such as arr[i++] += 2, it is the only version that is well defined.
Q3. Explain *p++, (*p)++ and *++p.Intermediate+
Expression
Meaning
Effect
*p++
*(p++)
Use the value at p, then advance the pointer
(*p)++
Increment the pointee
Pointer unchanged
*++p
*(++p)
Advance first, then use the new target
++*p
++(*p)
Increment the pointee, yield the new value
Postfix operators and the prefix unary operators share high precedence; prefix operators associate right-to-left while postfix bind to their operand first.
Q4. What is a sequence point and why does i = i++ + ++i; fail?Intermediate+
A sequence point is a place where all side effects of earlier evaluation are complete. Between two sequence points an object may be modified at most once, and its prior value may be read only to determine the new value. i = i++ + ++i modifies i several times in one expression, so the behavior is undefined: any result, including a crash, is allowed. Modern C describes this through the “sequenced before” relation, with the same practical rule: keep one side effect per object per statement.
Q5. When would you use the ternary operator and when avoid it?Intermediate+
Use it for short, side-effect-free selections: clamps, min/max, choosing a string or an initial value. Avoid it when the branches are long, have side effects, or when it is nested more than one level: an if/else is clearer, and MISRA C advises against complex nested use. Remember that it is an expression with a single result type, and that a macro version can evaluate arguments twice.
Q6. Why is sizeof(arr)/sizeof(arr[0]) wrong in a function parameter?Intermediate+
Array parameters decay to pointers, so sizeof(arr) is the pointer size (4 bytes on Cortex-M4). Pass the element count as a parameter, or wrap the array in a struct. GCC warns with -Wsizeof-array-argument.
Q7. What does the comma operator do and where is it legitimately used?Intermediate+
It evaluates its left operand, discards the value, evaluates its right operand (after a sequence point) and yields the right value. It has the lowest precedence. The legitimate uses are the init and increment parts of a for loop with two variables, and macros that must yield a value after a side effect. It is not the separator in declarations or function arguments.
Q8. Write a macro to compute the array length safely.Intermediate+
GNU C can additionally verify that the argument is an array, using __builtin_types_compatible_p, so that passing a pointer is a compile error.
Q9. Predict: int x = 5; x = x++ + x++;Advanced+
Undefined behavior: x is modified twice without an intervening sequence point. Compilers may produce 10, 11, or something else, and different optimisation levels can change the result. The expected answer is to identify the undefined behavior rather than to compute a number.
Q10. What is the result type of cond ? 1 : 2u and what trap does that create?Advanced+
The operands are an int and an unsigned int, so the usual arithmetic conversions apply and the result type is unsigned int. If the code then writes (cond ? -1 : 2u) < 0 the -1 is converted to a large unsigned number and the test is false. Cast one branch explicitly or use the same type in both.
Q11. A loop for (unsigned i = n; i >= 0; i--) never ends. Why, and how do you fix it?Advanced+
An unsigned value is always >= 0, so the test is always true, and when i is 0 the decrement wraps to 4294967295. Use the idiom for (unsigned i = n; i-- > 0; ) or loop with a signed type, or test i != 0 before decrementing.
unsigned_loop_iq.c
#include <stdio.h>int main(void)
{
for (unsigned i = 3; i– > 0; )
printf(“%u “, i);
printf(“\n”);
return0;
}
Output (verified by compiling and running)
2 1 0
Q12. How do compound assignment and integer promotion interact for uint8_t?Advanced+
In u8 += 300; the operands are promoted to int, added, and the result is converted back to uint8_t, which keeps only the low 8 bits (implementation-defined conversion in theory, modulo 256 in GCC). So 250 + 10 gives 4, not 260. With -Wconversion the compiler warns about the narrowing. For saturating behavior test the sum in a wider type before storing.
u8_promote_iq.c
#include <stdio.h>#include <stdint.h>int main(void)
{
uint8_t u = 250;
u += 10;
printf(“%u\n”, u);
return0;
}
Output (verified by compiling and running)
4
Study Plan: Using Your Score on These Assignment Operators Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Module 6: control flow, starting with if and else.
70–89%
Good
Redo Examples 2, 3 and 4 of this lesson.
Below 70%
Revise
Re-read the theory sections on sequence points and precedence, then retake the quiz.
Habit to build: Compile every practice file with -Wall -Wextra; GCC reports most of the undefined expressions and assignment-in-condition bugs from this lesson.
Module 5 complete!
Next up: Module 6, control flow with if, else and switch.
2 Comments