When writing complex expressions with several operands, we may have some doubts about which operand is
evaluated first and which later. For example, in this expression:
a = 5 + 7 % 2
we may doubt if it really means:
a = 5 + (7 % 2) // with a result of 6, or
a = (5 + 7) % 2 // with a result of 0
The correct answer is the first of the two expressions, with a result of 6. There is an established order with the
priority of each operator, and not only the arithmetic ones (those whose preference come from mathematics) but
for all the operators which can appear in C++. From greatest to lowest priority, the priority order is as follows:
Level Operator Description Grouping
1 :: scope
Left-toright
2
() [] . -> ++ -- dynamic_cast static_cast
reinterpret_cast const_cast typeid postfix
Left-toright
3
++ -- ~ ! sizeof new delete unary (prefix)
Right-toleft
* &
indirection and reference
(pointers)
+ - unary sign operator
4 (type) type casting
Right-toleft
5 .* ->* pointer-to-member
Left-toright
6 * / % multiplicative
Left-toright
7 + - additive
Left-toright
8 << >> shift
Left-toright
9 < > <= >= relational
Left-toright
10 == != equality
Left-toright
11 & bitwise AND
Left-toright
12 ^ bitwise XOR
Left-toright
13 | bitwise OR
Left-toright
14 && logical AND
Left-toright
15 || logical OR
Left-toright
16 ?: conditional
Right-toleft
17 = *= /= %= += -= >>= <<= &= ^= |= assignment
Right-toleft
18 , comma
Left-toright
Grouping defines the precedence order in which operators are evaluated in the case that there are several
operators of the same level in an expression.
All these precedence levels for operators can be manipulated or become more legible by removing possible
ambiguities using parentheses signs ( and ), as in this example:
a = 5 + 7 % 2;
might be written either as:
a = 5 + (7 % 2);
or
a = (5 + 7) % 2;
depending on the operation that we want to perform.
So if you want to write complicated expressions and you are not completely sure of the precedence levels, always
include parentheses. It will also become a code easier to read.
evaluated first and which later. For example, in this expression:
a = 5 + 7 % 2
we may doubt if it really means:
a = 5 + (7 % 2) // with a result of 6, or
a = (5 + 7) % 2 // with a result of 0
The correct answer is the first of the two expressions, with a result of 6. There is an established order with the
priority of each operator, and not only the arithmetic ones (those whose preference come from mathematics) but
for all the operators which can appear in C++. From greatest to lowest priority, the priority order is as follows:
Level Operator Description Grouping
1 :: scope
Left-toright
2
() [] . -> ++ -- dynamic_cast static_cast
reinterpret_cast const_cast typeid postfix
Left-toright
3
++ -- ~ ! sizeof new delete unary (prefix)
Right-toleft
* &
indirection and reference
(pointers)
+ - unary sign operator
4 (type) type casting
Right-toleft
5 .* ->* pointer-to-member
Left-toright
6 * / % multiplicative
Left-toright
7 + - additive
Left-toright
8 << >> shift
Left-toright
9 < > <= >= relational
Left-toright
10 == != equality
Left-toright
11 & bitwise AND
Left-toright
12 ^ bitwise XOR
Left-toright
13 | bitwise OR
Left-toright
14 && logical AND
Left-toright
15 || logical OR
Left-toright
16 ?: conditional
Right-toleft
17 = *= /= %= += -= >>= <<= &= ^= |= assignment
Right-toleft
18 , comma
Left-toright
Grouping defines the precedence order in which operators are evaluated in the case that there are several
operators of the same level in an expression.
All these precedence levels for operators can be manipulated or become more legible by removing possible
ambiguities using parentheses signs ( and ), as in this example:
a = 5 + 7 % 2;
might be written either as:
a = 5 + (7 % 2);
or
a = (5 + 7) % 2;
depending on the operation that we want to perform.
So if you want to write complicated expressions and you are not completely sure of the precedence levels, always
include parentheses. It will also become a code easier to read.
Learn More :
Turbo C
- string.h Definitions for memory and string functions
- stdlib.h Definitions for common types, variables, and functions
- stdio.h Definitions for stream input/output
- stddef.h Definitions for common types, NULL, and errno
- stdarg.h Definitions for accessing parameters in functions that accept a variable number of arguments
- stat.h Definitions used for file status functions
- signal.h Definitions for ANSI defined signaling capability
- share.h File sharing mode for use with sopen
- setjmp.h Defines typedef and functions for setjmp/longjmp.
- process.h Symbols and structures for process management
- mem.h Memory manipulation functions
- MCOMMAND Programming in C
- MCINPUT Programming in C
- MCDISPLY Programming in C
- MCALC Turbo C Programming
- matherr - user-modifiable math error handler
- math.h Definitions for the math floating point package.
- alloc.h memory management functions and variables
- #include In C++
- Structure of a program in C++ Learn
- getopt.c -- Turbo C
MSDOS
- errno.h Defines the system error variable errno and the erro numbers set by system calls. Errors which exist in Unix(tm) but not MSDOS have value -1.
- dos.h Defines structs, unions, macros, and functions for dealing with MSDOS and the Intel iAPX86 microprocessor family.
- conio.h Direct MSDOS console input/output.
Microprosessor
Operators
- Operating with variables in C++
- Assignment operator (=) in C++
- Arithmetic operators ( +, -, , , % ) in C++
- Increase and decrease (++, --) operators in C++
- Relational and equality operators ( ==, !=, >, <, >=, <= ) in C++
- Logical operators ( !, &&, || ) in C++
- Conditional operator ( ? ) in C++
- Comma operator ( , ) in C++
- Bitwise Operators ( &, |, ^, ~, <<, >> )