Simple demonstration of operator precedence and type casting in C++

Estimated read time 4 min read

The provided C++ code is a simple demonstration of operator precedence and type casting in C++.

Code

// Let's demonstrate how to use operator priority in C++

#include <iostream>
using namespace std;

int main() {
    int num1 = 1;
    int num2 = 2;
    int num3 = 3;
    int num4 = 4;

    double result1 = static_cast<double>(num1 + num2 * num3) / num4;
    double result2 = static_cast<double>((num1 + num2) * num3) / num4;
    double result3 = static_cast<double>((num1 + num2) * (num3 / num4));

    double result4 = static_cast<double>((num1 + num2) * num3) / static_cast<double>(num4);
    double result5 = static_cast<double>((num1 + num2) * num3) / static_cast<double>(num4);
    double result6 = static_cast<double>((num1 + num2) * num3) / static_cast<double>(num4);

    cout << result1 << endl;
    cout << result2 << endl;
    cout << result3 << endl;
    cout << result4 << endl;
    cout << result5 << endl;
    cout << result6 << endl;

    return 0;
}

Explanation

The provided C++ code is a simple demonstration of operator precedence and type casting in C++.

The code begins by declaring four integer variables num1, num2, num3, and num4, each initialized with values from 1 to 4 respectively.

int num1 = 1;
int num2 = 2;
int num3 = 3;
int num4 = 4;

Then, six double variables result1 to result6 are declared. Each of these variables is assigned the result of a mathematical expression involving the previously declared integer variables. The expressions are designed to demonstrate how operator precedence (the order in which operations are performed) can affect the result of a calculation.

For example, result1 is calculated as follows:

double result1 = static_cast<double>(num1 + num2 * num3) / num4;

In this expression, due to operator precedence, multiplication (num2 * num3) is performed before addition (num1 +). The entire expression within the parentheses is then type-casted to a double before division by num4. This ensures that the division operation produces a double result, not an integer.

The other result variables are calculated in a similar manner, but with different arrangements of parentheses to demonstrate how they can be used to override operator precedence.

Finally, the values of all result variables are printed to the console using cout:

cout << result1 << endl;
cout << result2 << endl;
cout << result3 << endl;
cout << result4 << endl;
cout << result5 << endl;
cout << result6 << endl;

This allows the user to see the different results produced by the different expressions, illustrating the effects of operator precedence and type casting in C++.

Output

1.75
2.25
0
2.25
2.25
2.25

Process finished with exit code 0

Operator Precedence Rules

In C++, operators have a specific order in which they are evaluated when an expression has several of them. This is known as operator precedence. Here are some common operator precedence rules in C++, from highest to lowest precedence:

  1. Parentheses (): Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want.
  2. Unary operators ++, --, !, ~, -, +, *, &, sizeof, new, delete: These operators have the next highest precedence after parentheses. They are used with only one operand. For example, the increment (++) and decrement (--) operators.
  3. Multiplicative operators *, /, %: These operators are evaluated next. They perform multiplication, division, and modulus operations.
  4. Additive operators +, -: These operators are used for addition and subtraction operations.
  5. Shift operators <<, >>: These operators are used to shift bits to the left or right.
  6. Relational operators <, <=, >, >=: These operators are used to compare two values.
  7. Equality operators ==, !=: These operators are used to check the equality or inequality of two operands.
  8. Bitwise AND operator &: This operator performs a bitwise AND operation.
  9. Bitwise XOR operator ^: This operator performs a bitwise XOR operation.
  10. Bitwise OR operator |: This operator performs a bitwise OR operation.
  11. Logical AND operator &&: This operator performs a logical AND operation.
  12. Logical OR operator ||: This operator performs a logical OR operation.
  13. Conditional operator ?:: This operator works as a simple if-else statement.
  14. Assignment operators =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=: These operators are used to assign values to variables.
  15. Comma operator ,: This operator is used to link related expressions together.

Remember, when operators have the same precedence, the rule of associativity (left-to-right or right-to-left) is used to determine the order of operations.

İbrahim Korucuoğlu

Yazar, bilişim ve teknoloji alanında derlediği faydalı içerikleri bu blogta paylaşmaktadır.