Operators Description
+ Unary plus needs one operand.
- Unary minus needs one operand.
+ Addition needs two operands.
- Subtraction needs two operands.
* Multiplication needs two operands.
/ Division needs two operands.
(
Floating point operands:
-- floating point result
Integer operands:
-- integer quotient
Mixed operands:
-- floating point result
)
% Modulus
(
remainder from integer division
operands must be integeral
)
++ Increment by one;
can be prefix or postfix;
as postfix has highest precedence
-- Decrement by one;
can be prefix or postfix;
as postfix has highest precedence
Look at the following example, and decide what is written by each of the output statements.
/*************************************************************
*
* The program demonstrates the precedence of the operators.
*
*************************************************************/
#include <iostream>
using namespace std;
int main ()
{
cout << 4 + 3 * 5 << endl;
cout << (4 + 3) * 5 << endl;
cout << 4 * 5 % 3 + 2 << endl;
cout << (4 * (5 % 3) + 2) << endl;
return 0;
}
The answer is:
Type changes can be made explicit by placing the value to be changed in parentheses and placing the name of the new type before it. This is called type casting or type conversion. For example,
intValue = 10.66; andIn summary, we have explicit and implicit data type conversion.
intValue = int(10.66) produce the same result 10.
Type coercion The implicit (automatic) conversion of a value from one data type to another.
Type casting The explicit conversion of a value from one data type to another; also called type conversion.