Concepts / Basic Types in C++

Basic Types in C++

Mixing types is a supported language feature. The key question is not whether two types can be mixed, but whether the particular combination is meaningful for the calculation you are performing.

  • Programming

A Cost Calculation with Two Types

Imagine a small program that calculates what a set of items costs. The number of items is a whole number, while the price of each item may include a fractional part such as cents. In a real program these two values often live in variables of different basic types, and you want to combine them in one arithmetic expression. The question this article answers is simple: when you use an arithmetic operator on values of different types, what does C++ do?

What do you think happens?

The variable items holds the whole number 3, and the variable price holds the value 2.5. What does items * price produce?

  • 3

  • 7

  • 7.5

Reveal answer
Answer:

7.5

The whole number is combined with the fractional price, so the product keeps the fractional part. Keep this result in mind, because the rest of the article explains how C++ arrives at it.

Following Values Through One Expression

When an int and a double meet in one expression, C++ does not refuse the combination. It brings the two operands into agreement so the operation can proceed. In the cost example, the whole number 3 is treated as the floating-point value 3.0 before the multiplication happens, and the multiplication is then carried out on two floating-point values. The diagram traces that sequence from the two original variables to the final result.

items = 3 (int)3 becomes 3.03.0 * 2.57.5 (double)price = 2.5(double)promoted before operator runsoperandoperandresult
When an int and a double meet in one multiplication, which operand is converted, to what type, and before or after the operator runs?

Notice that the conversion happens before the multiplication runs. The operator then sees two values of a compatible type, and the result is a double. The learner should read the diagram as a rule of the process: the operands are brought into agreement first, then the operation is performed on the agreed type.

Why Mixed Types Are Allowed

Definition

Arithmetic operators can be used with appropriate combinations of basic types. Addition, subtraction, multiplication, and division are not tied to one single type. They work with the basic types, and with combinations of those types where the combination makes sense for the calculation.

The reason a mixed expression can work at all is that C++ performs all meaningful conversions between the basic types so that they can be mixed freely. The conversions occur in two settings. The first is arithmetic, where values are combined with an operator. The second is assignment, where a value is stored into a variable. In both settings the language brings values of different basic types into agreement so that the operation or the storing step can proceed. Because of this, both the operands and the receiving variable matter to the final outcome.

Key point

Mixing types is a supported language feature. The key question is not whether two types can be mixed, but whether the particular combination is meaningful for the calculation you are performing.

Operators Need Meaningful Combinations

The word appropriate in the source's wording does real work. Some operators are defined for certain kinds of operands and not others. The remainder operator % is the standard illustration: it asks for the leftover after integer division, so its operands must be whole numbers. Applying it to a double does not produce a sensible remainder, and the compiler rejects the expression rather than guessing what you meant.

cpp
Output
1
Notes on the code
  • 7 % 2 uses two integer operands and yields the remainder 1
  • The commented line applies % to a double and would not compile

Division Depends on Operand Types

Division shows clearly how the types of the operands change the result. When both operands are integers, the division is carried out on whole numbers. When one operand is a double, the operation is carried out in floating-point arithmetic, and the fractional part survives. The comparison below places the two cases side by side.

7 / 2result 37.0 / 2result 3.5
Why does 7 / 2 produce 3 while 7.0 / 2 produces 3.5, and how does the operand type change the result?
cpp
Output
3
3.5
Notes on the code
  • 7 / 2 has two int operands, so the result is the whole number 3
  • 7.0 / 2 has a double operand, so the 2 is converted and the result keeps the fraction

The two lines use the same operator and nearly identical text. The only difference is the type of one literal, and that single difference changes the answer. Reading the operand types before reading the operator is therefore a habit worth building early.

Assignment Stores a Converted Value

The second setting where conversion happens is assignment. When a value of one basic type is assigned to a variable of another basic type, C++ converts the value so the assignment can happen. The next program isolates this case, with no arithmetic involved, so you can watch a value move from one type into another. Storing a double such as 3.9 into an int leaves a whole number in the destination, and the fractional part does not survive into it.

cpp
Output
3
Notes on the code
  • measured holds the double 3.9
  • The assignment converts the value to the int type of stored
  • The stored value is the whole number 3; the fractional part is not kept

The lesson of this program is that conversion is about putting a value into a form the destination can hold. The stored quantity is expressed in the destination's type, which is why the output looks like a whole number rather than a fraction. The same idea applies to the cost example: the result of a mixed expression is stored in a variable whose type also shapes what is kept.

Worked Example: Totaling a Purchase

Worked example

Totaling a Purchase with Mixed Types

A shopper buys 4 items, each priced at 1.25. Predict the total and explain which types are involved at each step.

  1. 1
    Identify the operand types

    The count 4 is an int. The price 1.25 is a double.

  2. 2
    Apply the promotion rule

    Because one operand is a double, the int 4 is converted to the floating-point value 4.0 before the multiplication runs.

  3. 3
    Perform the multiplication

    The expression 4.0 * 1.25 is evaluated in floating-point arithmetic.

  4. 4
    Store the result

    If the total is stored in a double, the fractional result is kept. If it were stored in an int, the fractional part would be dropped by the assignment conversion.

Result

The total is 5.0 when stored in a double. Storing it in an int would leave 5, which is the same quantity in a type that cannot hold the fraction.

Mistakes from Assuming Every Combination Behaves the Same

  • Common mistake

    Assuming an integer division result is the exact quotient

    What it looks like
    std::cout << 7 / 2;
    Why it fails

    Both operands are int, so the division is carried out on whole numbers and the result is 3, not 3.5.

    Do this instead

    Give one operand a floating-point type, such as 7.0 / 2, when the fractional part matters.

  • Common mistake

    Assuming an operator works for any pair of types

    What it looks like
    double bad = 7.0 % 2;
    Why it fails

    The % operator is defined for integer operands, so a double operand is not a meaningful combination for it.

    Do this instead

    Use % only with integer operands, and choose a different calculation when the values are fractional.

  • Common mistake

    Expecting the stored value to keep every digit of a double

    What it looks like
    double measured = 3.9; int stored = measured;
    Why it fails

    The assignment converts the value to the destination type, and the fractional part is not kept in an int.

    Do this instead

    Store the value in a double if the fraction is needed, or make the narrowing explicit and deliberate.

  • Common mistake

    Forgetting that the receiving variable's type shapes the result, not only the operands' types

Practice Predicting Mixed Results

MEDIUM

Given int a = 7; and double b = 2.0;, predict the value of a / b. Then predict what the same division produces if both operands are int.

Hints
  • Look at the type of each operand before you look at the operator.
  • Because b is a double, the int a is converted before the division runs.
  • With both operands int, the division produces a whole number.
EASY

A program stores double total = 10.75; and then assigns int whole = total;. What value does whole hold, and which part of total is lost?

Hints
  • The assignment converts the double to the int type of whole.
  • Think about which digits a whole number can represent.

Key Takeaways

  1. 1
    Arithmetic operators work with appropriate combinations of basic types, not with every combination by default.
  2. 2
    C++ converts values between basic types in both arithmetic and assignment, so mixing types is a supported feature.
  3. 3
    In a mixed expression, operands are brought into agreement before the operator runs, and the result type follows from that agreement.
  4. 4
    Integer division and floating-point division can give different answers for the same written expression, depending on the operand types.
  5. 5
    Before writing an expression, check both the operand types and the receiving variable's type.

At a glance

  • Arithmetic operators work with appropriate combinations of basic types, not with every combination by default.
  • C++ performs meaningful conversions between basic types so they can be mixed, in both arithmetic and assignment.
  • Operand types determine the result: 7 / 2 gives 3 while 7.0 / 2 gives 3.5.
  • Assigning a double to an int keeps only the whole-number part, so check the receiving variable's type.
  • Before predicting a result, identify the type of every operand and of the variable that stores the answer.

Related concepts

More concepts starting with B