What is the purpose of the assignment operator (=) in C?
The assignment operator (=) is used to store a value into a variable. It copies the value on the right side into the variable on the left side.
Click to reveal answer
beginner
Explain the shorthand assignment operator += with an example.
The operator += adds the right value to the left variable and then assigns the result back to the left variable. For example, x += 3; means x = x + 3;.
Click to reveal answer
beginner
List three common shorthand assignment operators in C.
Three common shorthand assignment operators are: 1. += (add and assign) 2. -= (subtract and assign) 3. *= (multiply and assign)
Click to reveal answer
intermediate
What happens if you write a = b = 5; in C?
This assigns the value 5 to variable b first, then assigns the value of b (which is 5) to variable a. Both a and b will have the value 5.
Click to reveal answer
intermediate
Can assignment operators be used with non-numeric types in C?
Yes, assignment operators can be used with any assignable type, such as characters, pointers, and structs (with some restrictions). The = operator copies the value from right to left.
Click to reveal answer
What does the expression x *= 4; do in C?
ADivides x by 4 and stores the result in x
BAssigns 4 to x
CMultiplies x by 4 and stores the result in x
DAdds 4 to x and stores the result in x
✗ Incorrect
The operator *= multiplies the variable by the right value and assigns the result back to the variable.
Which operator assigns the value on the right to the variable on the left?
A!=
B==
C+=
D=
✗ Incorrect
The single equals sign (=) is the assignment operator in C.
What is the result of int a = 2; a += 3;?
Aa is 5
Ba is 6
Ca is 3
Da is 2
✗ Incorrect
The += operator adds 3 to a (which is 2), so a becomes 5.
What does the statement c = a = 10; do?
AAssigns 10 to a only
BAssigns 10 to both a and c
CAssigns 10 to c only
DCauses a syntax error
✗ Incorrect
Assignment operators can be chained. 10 is assigned to a, then a's value is assigned to c.
Which operator subtracts and assigns in one step?
A-=
B+=
C*=
D/=
✗ Incorrect
The -= operator subtracts the right value from the left variable and assigns the result back.
Explain how shorthand assignment operators work and give two examples.
Think about how operators combine math and assignment in one step.
You got /2 concepts.
Describe what happens when assignment operators are chained in a statement.
Remember that assignment returns a value that can be assigned again.
You got /3 concepts.
Practice
(1/5)
1. Which of the following assignment operators adds the right operand to the left operand and then assigns the result to the left operand?
easy
A. -=
B. +=
C. *=
D. /=
Solution
Step 1: Understand the meaning of += operator
The operator += adds the value on the right to the variable on the left and stores the result back in the left variable.
Step 2: Compare with other operators
Other operators like -= subtract, *= multiply, and /= divide, so they do not add.
Final Answer:
+= -> Option B
Quick Check:
+= means add and assign [OK]
Hint: Remember '+=' means add then assign to left variable [OK]
Common Mistakes:
Confusing '+=' with '=' which just assigns
Mixing '+=' with '-=' or '*='
Thinking '+=' only adds without assignment
2. Which of the following is the correct syntax using the compound assignment operator to multiply variable x by 5 and assign the result back to x?
The correct syntax for multiplying and assigning is 'x *= 5;'. It combines multiplication and assignment.
Step 2: Check other options for syntax errors
x =* 5; 'x =* 5;' is invalid syntax. x = *5; 'x = *5;' is invalid because '*5' is not a valid expression. x = x *5; 'x = x *5;' is correct but not a compound assignment operator.
Final Answer:
x *= 5; -> Option D
Quick Check:
'*=' is multiply and assign operator [OK]
Hint: Use '*=' to multiply and assign in one step [OK]
Common Mistakes:
Writing '=*' instead of '*='
Forgetting space or semicolon
Using 'x = *5;' which is invalid
3. What is the output of the following code?
int a = 10; a += 5; a -= 3; printf("%d", a);
medium
A. 8
B. 15
C. 12
D. 10
Solution
Step 1: Calculate after first operation 'a += 5;'
Starting with a = 10, adding 5 gives a = 15.
Step 2: Calculate after second operation 'a -= 3;'
Subtracting 3 from 15 gives a = 12.
Final Answer:
12 -> Option C
Quick Check:
10 + 5 - 3 = 12 [OK]
Hint: Apply each operator step-by-step to track value [OK]
Common Mistakes:
Adding and subtracting in wrong order
Confusing '+=' and '-=' effects
Ignoring initial value of variable
4. Identify the error in the following code snippet:
int x = 7; x +== 3; printf("%d", x);
medium
A. Syntax error due to '+==' operator
B. Runtime error due to invalid operation
C. No error, output is 10
D. Logical error, output is wrong
Solution
Step 1: Check the operator '+==' in the code
The operator '+==' does not exist in C. The correct operator for addition assignment is '+='.
Step 2: Understand the effect of invalid operator
Using '+==' causes a syntax error and the code will not compile.
Final Answer:
Syntax error due to '+==' operator -> Option A
Quick Check:
Invalid operator '+==' causes syntax error [OK]
Hint: Use '+=' not '+==' for addition assignment [OK]
Common Mistakes:
Typing '+==' instead of '+='
Assuming '+==' works like '=='
Ignoring compiler error messages
5. Given the code below, what is the final value of result?
int result = 20; result /= 4; result *= 3; result %= 5; result += 2; printf("%d", result);