Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Multiplication operator *

The binary * operator calculates the product of two values. The result type depends on the operand types and the usual arithmetic conversions, not on the destination variable's type. Learn multiplication precedence and the pitfalls of overflow.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

lhs * rhs

The binary * operator in lhs * rhs calculates a product. Its built-in form accepts operands of arithmetic or unscoped enumeration types. The usual arithmetic conversions determine a common operand and result type; these include integral promotions where applicable. For example, 3 * 2.5 has type double. There is no built-in pointer multiplication.

Multiplication has higher precedence than + and -: 2 + 3 * 4 means 2 + (3 * 4). The operators *, /, and % share a precedence level and group left-to-right: a * b / c means (a * b) / c. This grouping does not guarantee left-to-right operand evaluation. Parentheses let you change the grouping.

Overflow of a multiplication result in a signed integer type causes undefined behavior. Arithmetic in the resulting unsigned integer type operates modulo 2^N, where N is that type's width. Assigning the product to a wider type does not widen the earlier calculation. In 1LL * a * b, the usual arithmetic conversions apply separately to each multiplication. If a has type int and b has type unsigned long long, the second multiplication uses unsigned long long even if its result fits in long long. Floating-point multiplication may require rounding.

Do not confuse binary a * b with unary *p, which performs indirection. For user-defined types, operator* can be overloaded, so the arithmetic rules above describe the built-in form. The referenced website contains an evolving standard draft rather than a frozen C++23 text; the example is intended for local verification with -std=c++23.

Examples

#include <iostream>

int main() {
    constexpr int count = 6;
    constexpr int price = 7;
    constexpr int total = count * price;
    static_assert(total == 42);

    std::cout << total << '\n';
    std::cout << 2 + 3 * 4 << '\n';
    std::cout << (2 + 3) * 4 << '\n';
}

Result

42
14
20

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources