DSA SheetLesson · no judge

PROGRAMMING FUNDAMENTALSC++ FUNDAMENTALS

Data types decide what survives assignment

Reading · 6 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 23

A variable's type decides what value it can preserve

CPPFive race broadcast values with different destination types.
int laps = 12;
long long replayViews = 3000000000LL;
double lapTimeSeconds = 73.63;
float windSpeed = 2.1f;
char lane = 'B';

The variable name tells you what the value represents, but it does not decide how the value is stored. The data type does that. int is for whole numbers, long long is for whole numbers that may need a larger range, double and float preserve fractional values with different precision, and char stores one character. The type also determines which values can fit without losing information.

The value on the right is converted to the type on the left during initialization. That means the same written value can be preserved, approximated, or changed depending on its destination. A variable does not keep an initializer untouched just because the initializer was written with more detail.

The value 3000000000 needs a whole-number type with enough range

The laps variable needs only the whole number 12, so int is suitable for that value. replayViews needs to preserve the whole number 3000000000, which requires a type with enough range. C++ does not guarantee that an ordinary int can hold this value on every implementation, so long long is the suitable choice here.

The LL suffix on 3000000000LL makes that literal a long long literal. This gives the literal a long long type before it is assigned to replayViews. The destination still matters, because a value must fit the destination type to be preserved correctly.

CHECKPOINT 1Not answered

Which type should replace int in this declaration so replayViews can preserve 3000000000LL?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

Floating-point types preserve fractions with limited precision

lapTimeSeconds contains the fraction .63, so a whole-number type would not preserve all of its information. double is a floating-point type that can store fractional values and generally provides more precision than float. That makes double a suitable choice when the lap time needs more decimal detail.

windSpeed also contains a fraction, but its declaration uses float. The f suffix on 2.1f selects a float literal instead of the default floating-point literal type. Both float and double can approximate many decimal values rather than storing every decimal value exactly, and double generally keeps more significant detail.

A char variable stores exactly one character value

lane is a char variable because the race lane marker is one character, B. The character literal is written with single quotes, as in 'B'. The type and the literal agree: char provides space for one character value, while the single-quoted literal supplies that one character.

CHECKPOINT 2Not answered

Write the declaration that stores the race lane marker B in a char variable.

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The destination type can discard part of the value

The correct lap time declaration uses double because 73.63 contains a fraction. If you write int lapTimeSeconds = 73.63;, C++ converts the initializer to int, so the fractional part is discarded and the stored value becomes 73. The declaration compiles, but it no longer preserves the information written in the initializer.

The same kind of loss can happen in other directions. An undersized whole-number type cannot preserve a value outside its range, and a lower-precision floating-point type may not preserve as many significant digits. In each case, the destination type controls the result rather than the initializer's original spelling.

Choose a type by asking three questions: does the value need to be a whole number or support a fraction, what range must it hold, and how much precision must it preserve? For the race broadcast record, int fits 12, long long fits 3000000000, double suits the precise fractional lap time, float suits the fractional wind speed when its precision is enough, and char fits the single lane marker.

Previous · VariablesNext part · Quiz