C++ Fundamentals: Data Types, Operators and Control Structures
C++ Fundamentals
This domain tests data types, operators, expressions, type conversion and control structures. Fundamental types include int, char, float, double, bool and void, with modifiers short, long, signed and unsigned. char stores one character, bool stores true or false, and void means no value.
Operators and control
- Arithmetic: + - * / %; integer division truncates toward zero, and % needs integer operands.
- Relational: < <= > >= == !=; logical: && || !; assignment: = and compound forms.
- Increment/decrement: prefix changes then uses; postfix uses then changes.
- Conditional: ?:; sizeof gives size in bytes; precedence: * / % before + -, relational before logical.
- Selection: if, if-else, switch (integral or enum, break prevents fall-through). Iteration: for, while, do-while (runs at least once). break exits a loop; continue skips to the next iteration.
Worked example
int a = 5, b = 2; float c = a / b; gives c = 2.0, not 2.5, because both operands are integers and division is integer division. Use float c = (float)a / b; or a / 2.0 to force floating-point division. Then 5 + 3 * 2 is 11, because * has higher precedence than +.
Traps
= assigns; == compares. switch does not accept float or strings. A do-while always executes once. % cannot be used with float. Uninitialized local variables contain indeterminate values.