What is the value of the expression 7 / 2 in C?
Both operands are int, so C performs integer division and discards the fraction (truncating toward zero). Make one operand floating – 7 / 2.0 – to get 3.5.
Operators and Expressions · Exercise 1 of 4
Twelve multiple-choice questions on arithmetic, increment and decrement, precedence and associativity, relational and logical operators, the comma operator, sizeof and undefined expressions. Pick an option to check your answer instantly.
What is the value of the expression 7 / 2 in C?
Both operands are int, so C performs integer division and discards the fraction (truncating toward zero). Make one operand floating – 7 / 2.0 – to get 3.5.
What is the value of -7 % 3?
Since C99, / truncates toward zero (−7 / 3 = −2), and % is defined so that (a / b) * b + a % b == a: −2 × 3 + (−1) = −7. The remainder takes the sign of the left operand.
What is 2 + 3 * 4?
* has higher precedence than +, so it is evaluated as 2 + (3 * 4) = 14. Brackets change it: (2 + 3) * 4 = 20.
How is a = b = c = 0; grouped?
Assignment is right-associative, and its value is the value assigned. So c is set to 0, that value is assigned to b, then to a.
What is the value of i after int i = 5; i = i++;?
The assignment and the increment both modify i, and C does not order them. Some compilers give 5, others 6, and optimisers may do anything else. Clang warns "multiple unsequenced modifications to i".
What is the type and value of (5 > 3)?
Relational, equality and logical operators always produce an int: exactly 1 or 0. That remains true in C23, even though bool is now a keyword.
Which statement about sizeof is true?
sizeof x needs no brackets for an expression (only for a type name, sizeof(int)). Its result is a size_t, and an operand such as i++ is never evaluated.
What is the value of x after int x = (4, 7, 9);?
The comma operator evaluates operands from left to right, discards all but the last, and yields the last one. The brackets matter: the comma has lower precedence than =.
How does C group a && b || c?
&& has higher precedence than ||. Brackets are still recommended – compilers warn about mixing them without parentheses.
After int x = 5; int y = x++ + 1;, what are x and y?
x++ contributes the old value 5, so y = 5 + 1 = 6, and x is incremented to 6. (Only one modification of x, so this is well-defined.)
Timed online tests with instant scores and explanations.