Você está na página 1de 4

Operators and Expressions

We will discuss 5 types of operators in this section 1.Arithmetic 2.Unary 3.Relational and logical 4.Assignment 5.Conditional

Arithmetic Operators: Four basic arithmetic operators you already know are 1.addition 2.subtraction 3.multiplication 4.division Now we define a new arithmetic operator called modulus operator (%) in fact modulus operator is not new it is your traditional remainder but with a new name. Note1: The division of an integer by another integer always results in an integer. So what happens when j= 5/3 ? j will be 1 instead of 1.6666 ie the decimal portion of the number is dropped than what happens when we use ve numbers that is when j=-5/3? This depends on the compiler so remind me about this in the lab , we can write a short code and test. Note2 For modulo division the sign of the result is always the sign of the first operand ie 10%3 =2 10%-3=2 -10%3=-2 -10%-3=-2

Note3: real operands give real results ie 24.0/10.0 is 2.4 Remember that if at least one operand is real ie float the result will be a float ie 24/10.0 =2.4 where as 24/10 is 2 Note4 Modulo (%) works only on integers it does not work for floats Note5 If one operand is long int and the other is int the result will be in int similarly for floats Exercise what is cast?

Arithmetic Operators Precedence Priority of operations : the operations are divided into two groups. First priority */% Second priority +So in any given expression we evaluate the first priority operators first followed by second

Rule 1 all the sub expressions within the parenthesis are evaluated first. And
nested parenthesized subexpressions are evaluated inside out ie expressions inside the inner most parenthesis are evaluated first

Rule 2- Operators in the same expression with the same priority are evaluated
from left to right Ex: 15*7/(2-3*5/7+4)-7*9%4

Unary Operators Unary operators are a class of operators that act upon a single operand to produce a new value. As of now we shall discuss three basic unary operators 1. Unary minus (-) In C all numeric constants are +ve hence a symbol followed by a constant is an expression.

Note unary symbol is totally different from the arithmetic subtraction which has the same symbol 2. ++ 3. The above two unary symbols are called increment and decrement operators respectively. Note The associativity of this group is from right to left ie when two unary operators occur in an expression than we start evaluating from right to left. This is in contrast to arithmetic operators where we evaluate from left to right. But as of now you would very rarely encounter such an expression with two unary operators.

Post increment and pre increment x is 11 and a is 11 for post increment ++a, Y is 10 and b is 11 for pre increment b++

Exercise: What is the difference between post increment and pre increment operator?

Relational and Logical Operators There are four relational operators in C < <= > >= These four operators form a precedence group which is just below the arithmetic operators. The associativity of this group is from left to right (which means that when these operators occur in an expression we evaluate them from left to right, which is similar to arithmetic operators precedence group) Just below this precedence group we have two other relational operators called as equality operators == (equal to) != (not equal to)

An expression involving a relational operator(any of the above 6) is known as a relational expression. The result of these expressions will be of integer type, since in C, true is represented by 1 and false by 0 Note: Dont use = for testing equality. The operator for testing equality is ==

Logical Operators The two logical operators in C are && (and) || (or)

1&&1=1 , 0&&1=0, 0&&0=0.

1||1=1, 1||0=1, 0||0=0 ! is also a logical operator which denotes logical not. This has the highest precedence among the logical and relational operators.

Precedence of Relational and Logical operators


1. !logical not 2. < ,<= ,> , >= 3. = =, != 4. && 5. | |

Você também pode gostar