What is a variable in C++?
A variable is a named object that stores a value during program execution. Its type determines representable values, storage requirements, and valid operations.
Study 2 Variables and Data Types with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What is a variable in C++?
A variable is a named object that stores a value during program execution. Its type determines representable values, storage requirements, and valid operations.
What does a variable declaration specify?
A declaration introduces a variable’s name and type. An initializer gives the variable its first value, as in int count = 20;.
What rules apply to C++ identifiers?
An identifier may contain letters, digits, and underscores, but it cannot begin with a digit or be a reserved keyword. C++ identifiers are case-sensitive.
How do initialization and assignment differ?
Initialization creates an object and supplies its first value. Assignment replaces the value of an existing object, such as width = 25; after int width{10};.
Why should local variables be initialized?
An uninitialized local variable may contain an indeterminate value. Reading it can produce incorrect results or undefined behavior.
What values does bool represent?
A bool represents a logical condition and stores true or false. In conditions, zero converts to false and a nonzero number converts to true.
How do signed and unsigned integers differ?
Signed integer types represent negative and positive values. Unsigned types represent only nonnegative values and can wrap when subtraction would produce a negative result.
Why can floating-point equality be unreliable?
Floating-point types represent real-number approximations, so values such as 0.1 may not be exact. Exact equality comparisons can therefore behave unexpectedly.
When should C++ use const or constexpr?
Use const for an object that must not be modified after initialization. Use constexpr when a value is required or intended to be computable at compile time.
What does x += y mean?
The compound assignment x += y is equivalent to x = x + y. Related forms include -=, *=, /=, and %=; they combine an operation with assignment.
What is the result of integer division 7 / 2?
When both operands of / are integers, C++ performs integer division and discards the fractional part. Thus, 7 / 2 produces 3, while 7.0 / 2 produces 3.5.
How does short-circuit evaluation work?
For A && B, B is evaluated only when A is true. For A || B, B is evaluated only when A is false; this can guard operations such as division.