Professional Documents
Culture Documents
Table of Contents
Classes of C Operators 3
1. Arithmetic operators 3
2. Relational Operators 5
3. Logical Operators 6
4. Assignment Operators(=) 8
5. Increment & Decrement Operators 9
6. Conditional Operator ( ?: ) 10
7. Bitwise Operators 10
8. Special Operators: Comma (,) & sizeof 13
Arithmetic Expressions 13
Expression Evaluation 14
Precedence of Arithmetic Operators: 15
Rules for evaluation of expression 16
Operator
An operator is a symbol that tells the computer to perform some mathematical or logical operation (E.g. +,-,*,/,%,
<, > etc....)
Classes of C Operators
1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Conditional Operators
7. Bitwise Operators
8. Special Operators
1. Arithmetic operators
Operator Meaning
+ Addition
– Subtraction
* Multiplication
/ Division
% Modulus Operator
3%10==> 3
5%7 ==> 5
0%5 ==> 0
In general : (0,1,....n-1) % n ==> (0,1,....n-1)
-10%3 ==> -1
-10%-3 ==> -1
10%-3 ==> 1
Note: Sign of the result is the sign of the numerator (The first operand)
2. Relational Operators
❑ Used to compare two data values for taking decisions.
< 🡺 less than
<= 🡺 less than or equal to
> 🡺 greater than
>= 🡺 greater than or equal to
== 🡺 equal to
!= 🡺 not equal to
❑ Two possible outcomes for when we apply relational operators on two numeric operands.
1. True 🡺 Represented by value 1
2. False 🡺 Represented by value 0
int main()
{
int age = 25;
if(age > 18)
{
printf("Person can vote");
}
else
{
printf("Person cannot vote");
}
return 0;
}
3. Logical Operators
Logical operators in C.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
❑ Mainly used to check more than one condition to take decisions (&& and ||).
LOGICAL AND operator(&&)
Usage: Exp1 && Exp2
❑ If Both LHS and RHS expressions are individually True, then only the final outcome is True. In all
other cases, the final outcome is False.
❑ Example usage:
int age = 25,salary = 10000;
if(age < 30 && salary>25000)
{
//If both conditions are true, this section is executed
printf(“Person is well paid”);
}
else
{
printf(“Person is under-paid”);
}
NZ ==> Non-zero
In summary, for TRUE(1) output, both expressions must be NON-ZERO
LOGICAL OR operator(||)
Usage: Eg: Exp1 || Exp2
If both LHS and RHS expressions are False, the final outcome is False. In All other cases the final
outcome is True.
int age = 70;
if(age < 13 || age >60)
{
//If any 1 condition is true, this section is executed
printf(“Need special care\n”);
}
else
{
// If both conditions are false. This section executed
printf(“No need of special care\n”);
}
NZ ==> Non-zero
In summary, Result is TRUE, if either of the expressions or both expressions are NON-ZERO (True).
FALSE only when both exp1 and exp2 are False.
❑ Prefix ++(--)used in expression first variable incremented(decremented), the new value is used in
evaluation of expression,
❑ Precedence and associativity of ++ and -- are same as that of unary + and unary –
6. Conditional Operator ( ?: )
It is a ternary operator in C. I.e, it has got three operands.
Example 1:
❑ a = 10; b=15;
❑ x = (a>b)?a:b; // x ⇐ 15; i.e The expression finds larger of two numbers.
❑ The above line has the same effect as the following.
if(a>b)
x = a;
else
x = b;
❑ Conditional Operator is a concise way of doing if-else condition.
❑ It can reduce the number of lines of code in a program. In the above example four lines of code are
reduced to one line by use of conditional operator.
7. Bitwise Operators
❑ Act on bit level in a data item(integer variable, int constant etc.)
❑ Can’t work on floating point number(real numbers)
❑ Operators are:
Bitwise OR Operator( | )
Examples(Bitwise OR:|): (Assume integer variable is 1 byte size)
❑ int a = 5,b = 6,c;
❑ a’s Bit Sequence in Memory⇒
❑ c = a | b;
❑ c‘s Bit Sequence in Memory⇒
❑ c = a ^ b;
❑ c‘s Bit Sequence in Memory:
Arithmetic Expressions
An arithmetic expression is a combination of variables, constants and operators arranged as per syntax of
the language.
❑ Following are some mathematical expressions and their equivalent arithmetic expressions in C.
Evaluation of expressions
❑ General format:
variable = expression;
❑ First expression is evaluated fully, then the result is assigned to the variable in LHS.
❑ x = a * b - c;
❑ y = b / c *a;
❑ z = a – b / c + d;
Expression Evaluation
Expressions are evaluated using an assignment statement of the form:
In the above syntax, variable is any valid C variable name. When the statement like the above form is
encountered, the expression is evaluated first and then the value is assigned to the variable on the left
hand side. All variables used in the expression must be declared and assigned values before evaluation is
attempted. Examples of expressions are:
❑ x = a-b/3+c*2-1
❑ Let a = 9, b =12, and c=3. Then our expression becomes:
❑ x = 9-12/3+3*2-1
❑ From the table in previous page, we can see that the * and / operators are having higher
precedence than + and – operators. Also, the * and / operators are at the same level of precedence,
so we have to apply the associativity rules. Since the associativity rule is left-to-right for
arithmetic operators, we apply the / operator first and the expression evaluates to:
❑ x = 9-4+3*2-1
❑ Next, we apply the * operator and the expression becomes:
❑ x = 9-4+6-1
❑ Next, we apply the first – operator as the – and + operators are at the same level and the
associativity rule is from left to right. The expression becomes:
❑ x = 5+6-1
❑ Now, we apply the + operator and the expression become:
❑ x = 11-1
❑ Finally, we apply the – operator and the result is:
❑ x = 10
5. Arithmetic expressions are evaluated from left to right using the rules of precedence
6. When parentheses are used, the expressions within parentheses assume highest priority
Operator Precedence
❑ All operators are classified into different levels of precedence.
❑ If expression involves multiple operators(e.g: a+b*c-d), operation with higher precedence is
performed first.
❑ ‘b*c’ is done first in the example, since * has higher precedence than +,-
❑ What if the precedence for two operators are the same? ?
❑ I.e, in example a+b*c-d , + and – have the same precedence.
❑ The answer is: Associativity
Associativity:
❑ Associativity : Defines the direction of execution when operators of the same precedence level are
involved.
❑ Two types of associativity:
Left-to-right associativity(L-R)
Right-to-left associativity(R-L)
❑ Arithmetic operators follow L-R associativity.
E.g.:
❑ E.g. a+b*c-e*f
❑ E.g. if(a>b && c==d)
❑ E.g. a=5, b=10; !a<b (Note: ! has higher precedence than <)
❑ (a*b)*(c-d)/(e+f)
❑ Precedence rules decide the order in which different precedence level operators are applied.
❑ Associativity rules decide the order in which operators in same precedence level are applied.
Entire Data Types in C (Primary types + Modification of primary types) based on 16-bit system
architecture.
The sequence of rules that are applied while evaluating expressions are given below:All short and char are
automatically converted to int, then,
1. If either of the operands is of type long double, then others will be converted to long double and
result will be long double.
2. Else, if either of the operands is double, then others are converted to double.
3. Else, if either of the operands is float, then others are converted to float.
4. Else, if either of the operands is unsigned long int, then others will be converted to unsigned long
int.
5. Else, if one of the operand is long int, and the other is unsigned int, then
a. if a long int can represent all values of an unsigned int, the unsigned int is converted to
long int.
b. otherwise, both operands are converted to unsigned long int & result is long int.
6. Else, if either operand is long int then other will be converted to long int & result is long int.
7. Else, if either operand is unsigned int then others will be converted to unsigned int & result is long
int.
x = t/i + i*f – d;
1. First t/i is considered. Since i is int type and t is long type, i is automatically converted to long
and result is a long type.
2. Next i*f is evaluated, iint and f is float, hence i is converted to float and result is also a float.
3. Result of step 1(long type) is added to the result of step 2(float type), and the result is float .
Because float is of higher precision(level) than long.
4. Remaining computation is (Result of step 3 - d). Since d is double, which is of higher precision
than float, Result of step 3 is elevated from float to double and subtraction is performed. The result is a
double.
5. The result is finally assigned to int type variable x (x = ........) Hence double value is truncated
to an integer type (E.g if the result of RHS computation was 76.3444 it will be truncated to 76 and stored
in x)
❑ Another example
int a = 5,b =3;
float c = a/b; // c = 1.0
float d = (float)a/b; // d = 1.666667 . Here, a is first converted to float. Then the
operation is performed