Chapter4 Basic C Operators
Chapter4 Basic C Operators
Assignment operators Equalities and relational operators Logical operators Conditional operator
Arithmetic Operators I
In C, we have the following operators (note that all these example are using 9 as the value of its first operand)
Arithmetic Operators II
There are 2 types of arithmetic operators in C:
unary operators
operators that require only one operand.
binary operators.
operators that require two operands.
Unary Operator
C Operation Positive Negative Increment Decrement Operator + ++ -Example a = +3 b = -a i++ i--
The first assigns positive 3 to a The second assigns the negative value of a to b. i++ is equivalent to i = i + 1 i-- is equivalent to i = i-1
PRE- / POST-Increment
It is also possible to use ++i and --i instead of i++ and i-However, the two forms have a slightly yet important difference. Consider this example: int a = 9; printf(%d\n, a++); printf(%d, a); The output would be: 9 10
Principles of Programming - NI July2005 5
The following table illustrates the difference between the prefix and postfix modes of the increment and decrement operator.
Equivalent Statements R = count; count = count + 1 count = count + 1; R = count; R = count; count = count 1; Count = count 1; R = count;
R value
Count value 11
11 9 9
10
11 10 9
Binary Operators
C Operation Addition Subtraction Multiplication Division Modulus Operator + * / % Example: a+3 a-6 a*b a/c a%x
The division of variables of type int will always produce a variable of type int as the result. You could only use modulus (%) operation on int variables.
Principles of Programming - NI July2005 8
Assignment Operators
Assignment operators are used to combine the '=' operator with one of the binary arithmetic operators In the following slide, All operations starting from c=9
Operator Example Equivalent Statement Results
+= -= *= /= %=
Principles of Programming - NI July2005
c += 7 c -= 8 c *= 10 c /= 5 c %= 5
Precedence Rules
Precedence rules come into play when there is a mixed of arithmetic operators in one statement. For example: x = 3 * a - ++b%3; The rules specify which of the operators will be evaluated first. Precedence Level 1 (highest) 2 3 4 5 (lowest)
Principles of Programming - NI July2005
Operator () unary * / % + = += -= *= /= %=
Associativity left to right right to left left to right left to right right to left
10
If we intend to have the statement evaluated differently from the way specified by the precedence rules, we need to specify it using parentheses ( ) Using parenthesis, we will have x = 3 * ((a - ++b)%3); The expression inside a parentheses will be evaluated first. The inner parentheses will be evaluated earlier compared to the outer parentheses.
Principles of Programming - NI July2005 11
Relational Operators:
Operator > < >= <= Example x>y x<y x >= y x <= y Meaning x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y
12
Logical Operators
Logical operators are useful when we want to test multiple conditions.
There are 3 types of logical operators and they work the same way as the boolean AND, OR and NOT operators. && - Logical AND All the conditions must be true for the whole expression to be true. Example: if (a == 10 && b == 9 && d == 1) means the if statement is only true when a == 10 and b == 9 and d == 1.
Principles of Programming - NI July2005 13
Conditional Operator
The conditional operator (?:) is used to simplify an if/else statement. Syntax:
Condition ? Expression1 : Expression2
15
OR
grade = total > 60 ? P: F;
Principles of Programming - NI July2005 16
SUMMARY
This chapter exposed you the operators used in C
Arithmetic operators Assignment operators Equalities and relational operators Logical operators Conditional operator
Precedence levels come into play when there is a mixed of arithmetic operators in one statement. Pre/post fix - effects the result of statement
Principles of Programming - NI July2005 18