IIT Delhi CS 123 - Operators in C

Unformatted text preview:

Operators in C Operators are the foundation of any programming language We can define operators as symbols that help us to perform specific mathematical and logical computations on operands In other words we can say that an operator operates the operands For example is an operator used for addition as shown below c a b Here is the operator known as the addition operator and a and b are operands The addition operator tells the compiler to add both of the operands a and b The functionality of the C programming language is incomplete without the use of operators C has many built in operators and can be classified into 6 types 1 Arithmetic Operators 2 Relational Operators 3 Logical Operators 4 Bitwise Operators 5 Assignment Operators 6 Other Operators C OPERATORS AND PRECEDENCE In C there are different types of operators Arithmetic Operators These are basic operators used to perform basic operation like addition subtraction It is also known as binary operator because it requires two variables for evaluation Arithmetic operators are as follows Operator Meaning Example Addition 4 3 7 Subtraction Division Multiplication 4 3 1 4 2 2 4 2 8 Modulus Operator 7 2 1 Assignment Operators It is used to assign back to a variable also assign a modified values of the present holding Operator Meaning Assign RHS value to LHS Value of LHS will be added to RHS and assign it back to LHS Values of LHS will be subtracted from RHS and assign back to LHS Logical and Comparison Operators It is used to compare two quantity They are grouped into three parts a Relational Relational operator compares values to see if they are equal or if one of them is greater than the other and so on Relational operators produce only one or zero result These are often specified as true or false Operator Meaning Example Result Less than 3 2 F Greater than 3 2 T Less than equal 6 2 F Greater than equal 10 32 T Syntax expression1 relational operator expression2 b Equality The following operators are used to check the equality of the given expression or a statement These operators are normally represented by using the two keys namely equal to by the operator and not equal to by Operator Meaning Example Result Equal to 3 2 Not equal to 3 2 F T Syntax expression1 relational operator expression2 c Logical The logical operators are and Logical AND It is a compound expression It has result true when both expressions are true otherwise false It is denoted by as follows Expression1 Expression2 The result of logical AND is Situation Result T T T F F T F F T F F F Logical OR It is a compound expression It has result true when any one expression is true otherwise false It is denoted by as follows Expression1 Expression2 The result of logical OR is Situation Result T T T F F T F F T T T F Situation Result True False F T Logical Negation A expression can be changed from true to false and false to true using logical negation It is denoted by as follows Special Operators There is some special operators used in C to perform particular type of operation a Unary It requires a single operand There are two main unary operators and is called increment operator use to increase the value by 1 and is called decrement operator use to decrease the value by 1 It has two subtypes Prefix In prefix first it increment and then supply to operand Example if a 5 then b a 6 Postfix In postfix first it supplies the value to operand and then increment Example if a 5 then b a 5 and then if a c then a 5 Which is similar to if value 2 0 even true else even false Example int a b c b Ternary It is called ternary because it requires three expression It acts like if else construction It can be written as Even value 2 0 True False c Comma It is used for separation of variables at the time of declaration Assignment Operator Value can be stored in a variable using assignment operator The operand on the left hand side should be a variable while the operand on the right hand side can be any variable constant or expression The value of right hand operand is assigned to the left hand operand Ex x 8 8 is assigned to x S x y 2 Value of expression x y 2 is assigned to s Y x Value of y is assigned to x When the variable on the left hand side of assignment operator also occurs on right hand side we can avoid writing the variable twice by using compound assignment operator Ex x x 5 can also be written as X 5 Here is a compound assignment operator Similarly we have other compound assignment operators x 5 is equivalent to x x 5 y 5 is equivalent to y y 5 sum 5 is equivalent to sum sum 5 k 5 x is equivalent to k k 5 x a b 5 is equivalent to a a b 5 Bitwise Operators C has the ability to support manipulation of data at the bit level Bitwise operators on integers only and they are use for operations on individual bits The bitwise operators are Bitwise Operator Name bitwise AND bitwise OR one s complement left shift right shift bitwise XOR Precedence of operators Operator Associativity Order of Precedence Left to Right Left to Right Right to Left Right to Left Left to Right 1 1 2 3 2 Left to Right Left to Right Left to Right Left to Right Left to Right Left to Right Left to Right Left to Right 3 4 4 5 5 6 7 7 Type Conversion It is a technique to convert one data type to other The operator used to force this conversion is known as the cast operator The different types of type conversions are 1 Implicit type conversion 2 Explicit type conversion 1 Implicit type conversion These conversions are done by the C compiler according to some predefined rules of C language The two type of implicit type conversions are automatic type conversions and type conversion in assignments Implicit type conversions are done by the compiler while the explicit type conversions are user defined conversions There may be certain situations where implicit conversions may not solve our purpose 2 Explicit type conversion Ex float z int x 20 y 3 Z x y The value of z will be 6 0 instead 6 66 In these types of cases we can specify our own conversion known as type casting This is done with the help of caste operator The caste operator is a unary operator that is used for converting an expression to a particular data temporarily The syntax of caste operator is A floating point number to an integer can be converted as Result int 19 2 4 Thus the cast operation int casts not only the 19 2 but the entire expression Thus result receives …

View Full Document Unlocking...