Quiz- Assignment Operators Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Tutorial

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 5 · LESSON 5.4 · PRACTICE

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.

Section 1: Multiple Choice Questions — Assignment, ++/–, ?: and sizeof

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?

2. With int i = 5;, what does int a = ++i; leave in a and i?

3. What is x after int x; x = (1, 2, 3);?

4. What is the value of y after int y; y = 1, 2;?

5. Which of these is undefined behavior?

6. What type does c ? 1 : 2.5 have?

7. What does a += b * 2 mean?

8. Which has the LOWEST precedence?

9. Which is the correct postfix/prefix precedence relation to * in *p++?

10. What does (*p)++ do?

11. What does sizeof return for an int a[10] on STM32F407?

12. Inside void f(int a[10]), sizeof(a) equals:

13. Which expression is evaluated twice in #define MAX(a,b) ((a)>(b)?(a):(b)) when called as MAX(x++, y)?

14. What does while (n--) do with n = 3?

15. Which declaration is a legal use of the comma operator?

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:

2. Write a statement that shifts reg right by 3 using a compound operator:

3. Write a statement that clears bit 4 of reg with a compound operator:

4. Write a one-line statement that puts the larger of a and b in m:

5. int i = 7; int r = i-- + 1; gives r =

6. int i = 7; int r = --i + 1; gives r =

7. After int i = 7; int r = i-- + 1;, i =

8. sizeof(uint16_t[6]) = bytes

9. int x = 10; x %= 4; gives x =

10. int x = 3; x <<= 2; gives x =

11. int a = (4, 9); gives a =

12. 5 > 3 ? 10 : 20 =

Section 3: Which Compound Operator Fits?

Pick the operator from the options, as in assignment operators interview questions about operator choice.

TaskYour answerResult
Add the sample to a running total+=-=*=%=&=|=^=>>=
Mask off the upper bits+=-=*=%=&=|=^=>>=
Set a flag bit+=-=*=%=&=|=^=>>=
Flip an LED state bit+=-=*=%=&=|=^=>>=
Divide by 8 for an unsigned value>=” style=”border:2px solid #0ea5e9;border-radius:6px;padding:5px 8px;max-width:210px;”>+=-=*=%=&=|=^=>>=>= 3.” style=”padding:10px 14px;border:1px solid #e2e8f0;font-size:0.9em;”>
Wrap an index to a buffer length+=-=*=%=&=|=^=>>=
Scale a reading by 5+=-=*=%=&=|=^=>>=
Subtract a fixed offset+=-=*=%=&=|=^=>>=

Section 4: True or False

Statements typical of assignment operators interview questions.

1. a = b = 5; is legal and assigns 5 to both.

2. i++ and ++i are identical when used as a standalone statement.

3. The expression i = i++; is well defined and leaves i unchanged.

4. The two branches of ?: are both always evaluated.

5. sizeof is a function that runs at run time.

6. In for (i = 0, j = 5; ...) the comma is the comma operator.

7. x += y may evaluate x only once, while x = x + y evaluates it twice.

8. if (x = 5) compares x with 5.

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); return 0; }

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); return 0; }

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); return 0; }

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.
Challenge 4 — Output?: Type of the conditional
#include <stdio.h> int main(void) { int c = 0; printf(“%zu %zu\n”, sizeof(c ? 1 : 2.5), sizeof(c ? 1 : 2)); return 0; }

What are the two sizes on this PC?

Output:
8 4
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
#include <stdio.h> int main(void) { char s[] = “ABC”; char *p = s; printf(“%c”, *p++); printf(“%c”, *p); printf(“%c”, (*p)++); printf(“%c\n”, *p); return 0; }

What is the printed string?

Output:
ABBC
*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); return 0; }

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); return 0; }
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+
ExpressionMeaningEffect
*p++*(p++)Use the value at p, then advance the pointer
(*p)++Increment the pointeePointer 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.

sizeof_iq.c
#include <stdio.h> #include <stdint.h> static size_t f(uint8_t a[16]) { return sizeof a; } int main(void) { uint8_t buf[16]; printf(“%zu %zu\n”, sizeof buf, f(buf)); return 0; }
Output (verified by compiling and running)
16 8
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+
countof_iq.c
#include <stdio.h> #include <stdint.h> #define COUNT_OF(a) (sizeof(a) / sizeof((a)[0])) int main(void) { uint32_t t[7]; printf(“%zu\n”, COUNT_OF(t)); return 0; }
Output (verified by compiling and running)
7

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”); return 0; }
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); return 0; }
Output (verified by compiling and running)
4

Study Plan: Using Your Score on These Assignment Operators Interview Questions

ScoreMeaningNext step
90–100%ExcellentContinue to Module 6: control flow, starting with if and else.
70–89%GoodRedo Examples 2, 3 and 4 of this lesson.
Below 70%ReviseRe-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.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *