UNIT 2 2.8 Operators Flashcards
is a symbol that tells the compiler to perform specific mathematical, relational or
logical manipulations.
Operator
Three General Classes of C Operators
- Arithmetic
- Relational
- Logical
Arithmetic Operators: * (asterisk) / (slash) \+ (plus) - (minus) - (unary minus) % (modulus) \++ (plus plus) - - (minus minus) ( ) (parenthesis)
* = multiplication / = division \+ = addition - = subtraction - = negation % = modulus division \++ = increment operator - - = decrement operator ( ) = parenthesis
Important Notes:
1. The operators +, -, * , and / all work the same way in C as they do in most computer
languages.
2+2 = 4 ganon
Important Notes:
2. When / is applied to an integer or character, any remainder will be truncated.
Example 11/2 = 5, 10/3 = 3, 13/6 = 2, 3/4 = 0, -34/3 = -11
Important Notes:
3. The modulus division operator, % yields the remainder of an integer division. % cannot
be used on type float and double.
Example 11 % 2 = 1 5 % 3 = 2 147 % 20 = 7 6 % 3 = 0 -25 % 2 = -1
Important Notes:
4. When an increment or decrement operator precedes its operand, C performs the
increment or decrement operation prior to using the operand’s value. If the operator
follows its operand, C uses the operand’s value before incrementing or decrementing it.
Example
X = 10;
Y = ++X;
X is 11; Y is 11
X = 10;
Y = X++;
X is 11; Y is 10
is to add exactly one to the value of a variable.
Increment
++ increment operator
X++ post increment
is to subtract exactly one from the value of a variable
Decrement
– decrement operator
++X pre increment
Increment and Decrement Examples:
Example
1. X=X + 1 or X+=1 is the same with X++
- Y=10, X=3
Z=Y + X++
Z=Y+X; 13
X=X + 1; 4
Z=Y+ ++X
X=X + 1; 4
Z=Y + X; 14
Z=Y + X–
Z=Y + X; 13
X=X – 1; 2
Z=Y+ –X
X=X – 1; 2
Z=Y + X; 12
It require only a single operand like unary plus and unary minus.
Unary Operators
Example \+X, -Y, - unary plus operator (+) – causes no change to the quantity which follows.
- unary minus operator (-)
– causes the sign of the following quantity to be
changed.
It requires two operands
Binary Operators
Example
+ (addition) X + Y
* (multiplication) X – 3
is C’s “shorthand” operation.
Combined Operators
Example
X = X + Y;
using shorthand can be expressed as X+ = Y;
There are combined operators for all of C’s Binary Operators.
Example Long Hand Notation X=X + Y; X=X – Y; X= X * Y; X=X/Y; X=X % Y;
(Combined) Shorthand X+ = Y; X - = Y; X* = Y; X/ = Y; X% = Y;
refers to the relationship values can have with one another. It is used to determine
the relationship of one quantity to another.
Relational
Relational Operators Recognized by C
Symbols
> greater than
:a > b
<
less than
:a < b
> =
greater than or equal to
:ave >= 90
<=
less than or equal to
:ave <=90
= =
equal to
:a = = b
! =
not equal to
:a != 100
refers to the ways the relationships can be connected together using the rules of formal
logic.
Logical
Logical Operators Recognized by C
&& and
|| or
! not
Note:
In C, true is any value other than 0; false is 0
1 - true
0 - false
Example: N = 10 > 5 && !(10 <9) || 3 <= 4 1 && !( 0 ) || 1 1 && 1 || 1 = 1