03 Operators in C

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

CHAPTER NO 3.

OPERATOR AND EXPRESSION.

OPERATORS AND EXPRESSION


 C expressions.
 C Operators, Its meaning and types of Operators.
o Arithmetic Operators, Assignment Operator, Compound Assignment Operators,
o Relational Operators, Logical Operators, Conditional Operator,
o Increment or Decrement Operator, Bit Wise Operators.
 Special Operators : Comma, Sizeof, Address Operator, Pointer Operator
 Operator Precedence and order of evaluation.
 Type modifier, Type conversion

EXPRESSION.
 Expression is a statement which is form by the combination of functions, Operators and operands (i.e.
constants and variables).
 The Variables and Constant itself are expression.
 The expression that only involves constant are called as Constant Expression.
 The types of expressions are Arithmetical, Relational etc.
 Consider the expression A + B * 5. where,
o Operators are : +, *
o Operands are : Either Variables A, B and Constant is 5
o A + B * 5 is an expression.

CONCEPT OF L_VALUE AND R_VALUE IN AN EXPRESSION.


L_VALUE ( Left Value )
 L_Value stands for the left value in an expression.
 Expression that refers to memory locations are called as “L value” expression.
 In Assignment statement “L value” must be the Variable (To hold the data)
 L value can not be constant, functions or valid data type in C.
R_VALUE ( Right Value )
 L_Value stands for the right value in an expression.
 In Assignment statement “R value” must be anything having capability to return constant value.
 R value can be constant, variable, function, macro, Enum constant etc.

OPERANDS
 Operands are either variables or constants on which operator perform its operations.
OPERATORS.
“Operators are the meaningful symbols which are used to perform meaningful logical and
mathematical operations”.
 Operator performs its operations on operands ( constants and variables ) in an expression.

Subject: C Programming (Bsc/BCA/MCM) Print Date: [03 October 2017], Page 1 of 7


 Each operator has its own meaning, which is given by compiler.
 C language supports a rich set of operators.
 The operators may be unary, binary or ternary.
 Operator also has its own precedeance and associativity for evaluation.

TYPES OF OPERATORS.
1. Arithmetical Operators
2. Assignment Operator.
3. Compound Assignment Operators.
4. Relational Operators.
5. Logical Operators.
6. Conditional Operator.
7. Increment or decrement Operator.
8. Bit Wise Operators.
9. Special Operators

EXPLAINATION OF OPERATORS IN DETAILS.


 ARITHMETICAL OPERATORS
o There are 5 arithmetic operators in C.
o They are + (Addition), - (Subtraction), * (Multiply), / (Division) and % (Modulo)
o These operators are used to perform mathematical (arithmetical) calculations.
o These operators are called as binary operators because It works on at least two operands
(Variable / Constant ).
o These operands may be integer, float or character.
o When arithmetical operators are used in expression, then it is called as Arithmetical Expressions.
 For Example :
o C=A+B;
o In above expression, it perform the addition of variable A and B and returns the result in variable C.
 ASSIGNMENT OPERATOR
o Assignment operator is used to assign a value to the variable.
o It is a binary operator. It is denoted by the Equal to Sign “=”.
o It have two values L_Value and R_Value.
o It copies R_value in to L_Value. (Right to Left)
o It has lower precedence than any other operators and higher precedence than comma.
o Syntax : L_Value = R_Value ;
o Here, L_Value must be the variable and
o R_value is Variable, Constant, Expression or Function.
o For Example :
1. A = 10 ; // It assign value 10 to variable A.
2. B = A ; //It assign value of variable A into variable B.
3. C = A + B; // It returns the result of A+B expression in C.
4. D = sqrt( 16 ) // It return the result of function in D.

Subject: C Programming (Bsc/BCA/MCM) Print Date: [03 October 2017], Page 2 of 7


 COMPOUND ASSIGNMENT OPERATOR
o It is combination of assignment operator and arithmetic operators.
o It performs arithmetical operation as well as assign value to the same variable.
o The list of compound assignment operator is += , -=, *= , /= and %=

Operator Example Meaning

+= sum+=10 This is same as sum = sum+10

-= sum – = 10 This is same as sum = sum – 10

*= sum*=10 This is same as sum = sum*10

/+ sum/=10 This is same as sum = sum/10

%= sum%=10 This is same as sum = sum%10

o For Example :
1. A += 10 ; // It increase the existing value of A by 10.
 RELATIONAL OPERATOR
o These operators are used to compare the relation between two operands.
o It returns the result true(non zero value) or false (zero value).
o The list of relational operator is as follow.

Operator Name Example


> Greater than X>Y
< Less than X<Y
>= Greater than or equal to X >= Y
<= Less than or equal to X <= Y
== Equal to X == Y
!= Not equal to X != Y

Note :
 10 > 20 returns the result 0 because condition is false.
 10 < 20 returns the result 1 because condition is true.
 These operators are used to build the condition.
 it is used with decision making statement.
 It execute different block of statement depending on the result of condition.
 LOGICAL OPERATOR
o These operators are used to perform logical operations.
o These operators are used to compare more than one condition at a time.
o These operators returns true (Non Zero ) or false ( 0 ) , after finding the result of all given
conditions.
o There are three types of logical operators.

Logical Operators Truth Table

Subject: C Programming (Bsc/BCA/MCM) Print Date: [03 October 2017], Page 3 of 7


Logical AND : Logical AND ( && )
 It is denoted by “&&” Sign.
Cond-1 Cond-2 Result
 When all the given conditions are returns true
(NON ZERO) , then logical AND “&&” operator T T T
return true, otherwise it returns false. T F F
 For Example
F T F
 A > B && A > C : This condition returns true only
when A is greater than B & C both. F F F
Logical OR : Logical OR ( || )
 It is denoted by “||” Sign.
Cond-1 Cond-2 Result
 When any one condition from the given conditions
is returns true, then logical OR “||” operator return T T T
true, otherwise it returns false. T F T
 For Example
F T T
 A > B || A > C : This condition returns true only
when A is greater than either B, C or both. F F F
Logical NOT : Logical NOT ( ! )
 It is denoted by “!” Sign.
Condition Result
 It returns true when condition is false and returns
false when condition is true. T F
 For Example F T
 !(A > B) : This condition returns false when A is
greater than B otherwise true.

 CONDITIONAL OPERATOR
o This operator is also called as ternary operator.
o It is denoted by the “?:” Sign.
o Conditional operators return one value (followed by ? ) if condition is true and returns other value (
followed by : ) if condition is false.
o It takes three arguments.
o Syntax : Expression1 ? Expression2 : Expression3;
o Here Expression1 is evaluated first,
o If it is true, then Expression2 is evaluated and its value becomes the value of the expression.
o If it is false, then Expression3 is evaluated and its value becomes the value of the expression.
o For Example :
o Int C = A > B ? A : B;
o In above example, if A is greater than B, it returns value of A in variable C otherwise it returns value
of B in variable C.

 INCREMENT OR DECREMENT OPERATOR


o This operator is also called as unary operator.
o These operators are used to either increase or decrease the value of the variable by one.
o These operators can be applied only on variables. (not on constants)
o These operators having higher priority than other operator.

Subject: C Programming (Bsc/BCA/MCM) Print Date: [03 October 2017], Page 4 of 7


o DIFFERENT TYPES OF UNARY OPERATORS.
o Pre-Increment ( Prefix ) Operator :
1. Pre-increment operator is used to increase the value of variable before using in the
expression.
2. For-Example : X = ++Y; //Increase value of Y is stored in X.
o Post-Increment ( Postfix ) Operator :
1. Post-increment operator is used to increase the value of variable after executing expression
completely.
2. For-Example : X = Y++; //Value of Y is stored in X and then Y is increase.
o Pre-Decrement ( Prefix ) Operator :
1. Pre-decrement operator is used to decrease the value of variable before using in the
expression.
2. For-Example : X = --Y; //Decrease value of Y is stored in X.
o Post-Decrement ( Postfix ) Operator :
1. Post-Decrement operator is used to Decrease the value of variable after executing
expression completely.
2. For-Example : X = Y--; //Value of Y is stored in X and then Y is decrease.
o Syntax :
o ++var_name; (or) var_name++;
o -- var_name; (or) var_name --;

 BIT WISE OPERATOR


o These operators are used to perform bit operations on given two variables.
o These operators operate on integer and character but not on float or double.
o These operators allow to manipulate individual bit.
o Truth table for bit wise operation

X Y X|Y X&Y X^Y


0 0 0 0 0
0 1 1 0 1
1 0 1 0 1
1 1 1 1 0

o There are 6 bit wise operators, and they are explain below
Bitwise OR – |
 It takes 2 bit patterns and
 It performs OR operations on each pair of corresponding bits.
 The following example will explain it.
 Int x = 20 | 25; // Store 29 in x
Bitwise AND – &
 It takes 2 bit patterns, and perform AND operations with it.
 The Bitwise AND will take pair of bits from each position,

Subject: C Programming (Bsc/BCA/MCM) Print Date: [03 October 2017], Page 5 of 7


and if only both the bit is 1, the result on that position will be 1.
 Bitwise AND is used to Turn-Off bits.
 For Example:
 Int x = 20 & 25; //Store 16 in x
Bitwise NOT - ~
 One’s complement operator (Bitwise NOT) is used to convert each “1-bit to 0-bit” and “0-bit to 1-
bit”, in the given binary pattern.
 It is a unary operator i.e. it takes only one operand.
 For Example:
 Int x = ~9; // It Store -10
 1001 ---- NOT 0110 -------
Bitwise XOR ^
 It takes 2 bit patterns and perform XOR operation with it.
 For Example
 Int x = 20 ^ 25; // Store 13 in x
Left shift Operator – <<
 The left shift operator will shift the bits towards left for the given number of times.
 For Example : int a=20<<1;
Right shift Operator – >>
 The right shift operator will shift the bits towards right for the given number of times.
 int a=20>>1;

 COMMA OPERATOR
o It is denoted by the “,” Sign.
o We can combine/link two related expression into one.
o It returns the value of right most operand.
o It has the lowest precedence than other operators.
o Comma operator can act as
1. As Operator : When it is used in Expression.
2. As Separator : When it is used in Declaring variable, Function call and definition for
parameter separation.
o For Example :
1. Int a=1, b=2; // As Operator
2. X = ( A , B) // As Separator
 SPECIAL OPERATORS
o SizeOf Operator :
1. This is compile time operator and used with operand. It returns the number of bytes the
operands occupies in memory.

Subject: C Programming (Bsc/BCA/MCM) Print Date: [03 October 2017], Page 6 of 7


2. The operand may be variable , constant or data types.
3. For Example : X = sizeof( int );
o AddressOf Operator :
1. It is denoted by “&” sign.
2. It returns the address of variable when it is used prefix with variable.
3. For Example : int *P = &X; It returns address of X and it stored in pointer P.
o Pointer / Value At Operator :
1. It is denoted by “*” sign.
2. It returns the value stored at particular address. (Value at)
3. It is also used to declare pointer variable. (Pointer)
4. For Example : int val = *P; It returns value at address P.
OPERATOR PRECEDENCE AND ASSOCIATIVITY.
 Operands are group with operators using precedence and associativity.
 Precedence :
o Precedence is the priority for grouping different types of operators with their operands.
o An operator's precedence is meaningful only if other operators with higher or lower precedence are
present.
o In an Expressions, higher-precedence operators are evaluated first.
o For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence
than +, so it first gets multiplied with 3*2 and then adds into 7.
 Associativity :
o Associativity gives their evaluation order.
o When an expression with more than one operators having equal priority, is executed, it works
according to associativity.
o Associativity of operator is either left-to-right or right-to-left.
o It is the order for grouping operands to operators that have the same precedence.
 The grouping of operands can be forced by using parentheses.
TYPE CONVERSION.
 Type casting is used to convert the value of one data type into another data type.
 New data type should be mentioned before the variable name or value in parenthesis which is to be type
casted.
 Syntax : data_type variable = (new_data_type)Variable/value.
 There are two types of type casting
o Automatic Type Conversion : The values of some type are automatically converted in
other type is known as Automatic type casting. E.g char to int , int to float means smaller type of
values are automatically converted without type casting.
 Int X = (int ) ‘A’ ; // Convert char to int.
 float div = ( float ) 7 / 5 ;
o Explicit Type Conversion : When we force to convert one type of value into another
type explicitly then it is called as Explicit Type Casting.
o For Example : int a = int( 3.44 f ); .

Subject: C Programming (Bsc/BCA/MCM) Print Date: [03 October 2017], Page 7 of 7

You might also like