Arithmetic Operators and Expressions in C++
C++ arithmetic depends on operand types, not merely the destination variable's type. Integral promotions and usual arithmetic conversions determine the types used in a calculation.
Summary
C++ arithmetic depends on operand types, not merely the destination variable's type. Integral promotions and usual arithmetic conversions determine the types used in a calculation. Assigning an integer-division result to a floating-point variable does not recover its discarded fractional part; convert an operand before dividing when a fractional result is needed.
| Operation | Operand requirements | Result behavior |
|---|---|---|
| Integer division | Integral operands | Quotient truncates toward zero |
| Floating-point division | An operand converted to a floating-point type | Avoids integer-division truncation |
| Remainder | Integral operands only | A nonzero remainder has the dividend's sign |
When integer division is defined, the quotient and remainder satisfy , where is the truncated quotient.
🧠 Key Concepts
- Integer division
- Floating-point conversion
- Integer remainder
- Integral promotions
- Usual arithmetic conversions
- Operator precedence
- Left-to-right associativity
- Undefined behavior
- Unsigned conversions
🧠 Quick Check
See what you remember from the summary.
An expression divides the integer literal by the integer literal , then assigns the result to a double variable. What value is stored?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Arithmetic Operators and Expressions in C++
📘 Overview C++ arithmetic results depend on operand types as well as the operator. Integer division, type conversions, precedence, and representability determine how an expression is evaluated.
🧠 Key Idea The operand types determine the arithmetic performed before the result is assigned. Assigning an integer-division result to a floating-point variable does not restore the discarded fractional part.
⚔️ Core Details: - The binary operators +, -, *, /, and % perform addition, subtraction, multiplication, division, and integer remainder, respectively. - Division between integral operands truncates the quotient toward zero. - The remainder operator % requires integral operands and produces a nonzero result with the same sign as the dividend. - Built-in arithmetic applies integral promotions and usual arithmetic conversions to determine a common operand type. - Multiplication, division, and remainder have higher precedence than addition and subtraction. - Integer division by zero, an unrepresentable integer quotient, and signed integer overflow have undefined behavior.
🎯 Why It Matters: - A floating-point conversion before division prevents accidental truncation in averages, ratios, and percentage calculations. - Unsigned operands can change the meaning of calculations involving negative values through implicit conversions. - Parentheses make intended grouping explicit and reduce errors in compound calculations. - Checking divisors and numeric ranges prevents arithmetic failures that a compiler is not required to diagnose.
🧠 Quick Recall: - Integer division - 7 / 2 produces the integer result
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Science notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.