Chapter 7

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

C Operators

CHAPTER 7 C OPERATORS
Objectives
Types of operators Expressions Type conversions Operator precedence Input & Output functions

7.1 Types of Operators C supports a rich set of operators. Operators are used in programs to manipulate data and variables. Operators can be classified into a number of categories. They are: 1. Arithmetic operators 2. Relational operators 3. Logical operators 4. Assignment operators 5. Increment and Decrement operators 6. Conditional operators 7. Bit wise operators 8. Special operators 7.1.1 Arithmetic operators C provides all the basic arithmetic operators. They are: Operator Meaning + * / % Addition or Unary plus Subtraction or Unary minus Multiplication Division Modulo division

C Operators Integer division truncates any fractional part. The Modulo division operator produces the remainder of an integer division. Examples or use of arithmetic operators are: a-b a*b a%b ab a/b -a*b

7.1.2 Relational operators We can compare two quantities and depending on their relation, take certain decisions. These comparisons can be done with the help of relational operators. They are: Operator Meaning < <= > >= == != Less Than Less than or equal to Greater than Greater than or equal to Equal to Not equal to

A simple relational expression can takes the following form: Operand1 relational operator operand 2 The value of relational expression is either one or zero. It is one if the specified relation is true and zero if the relation is false. For example, 10 < 20 is true 20 < 10 is false 7.1.3 Logical operators Operator Meaning && || ! Logical AND Logical OR Logical NOT

The logical operators && and || are used when we want to test more than one condition and make decisions. For example, A > b && x == 10

C Operators This is true only if a > b is true and x == 10 is true. If either of them false, the expression is false. Truth table for AND (&&) A 0 0 1 1 A 0 0 1 1 B 0 1 0 1 Truth table for OR (||) B 0 1 0 1 A || B 0 0 0 1 A && B 0 0 0 1

Truth table for NOT (!) A 0 1 !A 1 0

7.1.4 Assignment operators Assignment operators are used to assign the result of an expression to a variable. The form is v op=exp; where v is a variable, exp is an expression and op is a C arithmetic operator. For example, x=10; Operator Meaning = += -= *= /= Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment

C Operators %= Modulo division assignment

for example, x=x+3 this statement is equivalent to x+=3; when this statement is executed, 3 is added to x. 7.1.5 Increment and Decrement operators ++ and Increment operator ++ The operator ++ adds 1 to the operand. Two types of increment operators are: Prefix Increment operator(++op) This first adds 1 to the operand and then the result is assigned to the variable on left. For example, m=5; ++m; This statement increases the value of m by 1. that is m is 6. Postfix Increment operator (op++) Postfix operator first assigns the value to the variable on left and then increments the operand. For example, m=5; m++; This statement increases the value of m by 1. That is m is 6. While ++m and m++ mean the same thing when they form statements independently. They behave differently when they are used in expressions on the right hand side of assignment statements. For example, m=5; Y=++m; In this case, the value of y and m would be 6. Suppose, if we rewrite the above statements as m=5; y=m++; here, the value of y would be 5 and m would be 6. 7.1.6 Conditional operator (? :) This is also called as a ternary operator. The general form is: exp1 ? exp2 : exp3; where exp1, exp2 and exp3 are expressions. Here, exp1 is evaluated first. If it is nonzero (true), then the expression exp2 is evaluated and becomes the values of the expression. If exp1 is false, exp3 is evaluated and its value becomes the values of the expression. Note that only one of the expressions either exp2

C Operators or exp3 is evaluated. For example, a=10; b=34; max=(a > b) ? a: b; In this example, max will be assigned the value of b. because the condition is false. So, exp3 (b) is evaluated and its value is assigned to max. 7.1.7 Bitwise operators These operators are used for testing the bits, or shifting them left or right. These Operators may not be applied to float or double. Operator Meaning & | ^ << >> bitwise AND bitwise OR bitwise XOR shift left shift right

Bitwise AND The result of bitwise AND operator is 1 if both the bits have a value of 1; otherwise it is 0. For example, if x=13 and y=25 then z= x & y; if we execute this statement, x=0000 0000 0000 1101 y=0000 0000 0001 1001 the result would be: z=0000 0000 000 1001 Bitwise OR The result of bitwise OR operator is 1 if atleast one of the bits has a value of 1; otherwise it is 0. For example, if x=13 and y=25 then z= x | y; if we execute this statement, x=0000 0000 0000 1101 y=0000 0000 0001 1001 the result would be: z=0000 0000 001 1101

C Operators Bitwise XOR (Exclusive OR) The result of bitwise XOR operator is 1 if only one of the bits is 1; otherwise it is 0. For example, if x=13 and y=25 then z= x ^y; if we execute this statement, x=0000 0000 0000 1101 y=0000 0000 0001 1001 the result would be: z=0000 0000 000 0100 Bitwise shift operators The shift operators are used to move bit patterns either to the left or to the right. The shift operators are represented by the symbols << and >>. Left shift op << n op is an integer expression that is to be shifted and n is the number of bit positions to be shifted. The left shift operation shifts all the bits in the operand op to be shifted to the left by n positions. The leftmost n bits in the original bit pattern will be lost and rightmost n bits that are vacated will be filled with 0s. For example, x is an unsigned integer that has the value 11. Then bit pattern of x is: 0000 1011 x << 1 is, 0001 0110 that is 22. This is equal to the value of x will be multiplied by 2. Right shift op >> n The right shift operation shifts all the bits in the operand op to be shifted to the right by n positions. The rightmost n bits in the original bit pattern will be lost and leftmost n bits that are vacated will be filled with 0s if the op is an unsigned integer. If the variable to be shifted is signed, then the operation is machine dependent. For example, x is an unsigned integer that has the value 11. Then bit pattern of x is: 0000 1011 x >> 1 is, 0000 0101 that is 5. This is equal to the value of x will be divided by 2. Bitwise complement operator (~) The operator ~ inverts all the bits represented by its operand. That is, 0s become 1s and 1s become 0. For example, x=10;

C Operators binary representation of x is=0000 1010 ~x =1111 0101 sizeof operator The sizeof is a compile time operator and when used with an operand, it returns the number of bytes the operand occupies. The operand may be a variable, a constant or a data type qualifier. For example, int n; m=sizeof(n); Will return 2. That is m is assigned a value 2, because int occupies 2 bytes. m=sizeof(34.76); Will return 4. That is m is assigned a value 4, because int occupies 4 bytes. 7.2 Operator Precedence Each operator in C has a precedence associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. This table lists all C operators in order of their precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied. Description Parentheses (grouping) Brackets (array subscript) Member selection via object name Member selection via pointer Associativity left-to-right

Operator () [] . ->

++ -- Unary preincrement/predecrement + - Unary plus/minus ! ~ Unary logical negation/bitwise complement (type) Unary cast (change type) * Dereference & Address sizeof Determine size in bytes * + << < > == & / >> >= != % Multiplication/division/modulus Addition/subtraction Bitwise shift left, Bitwise shift right

right-to-left

left-to-right left-to-right left-to-right

<= Relational less than/less than or equal to left-to-right Relational greater than/greater than or equal to Relational is equal to/is not equal to Bitwise AND left-to-right left-to-right

C Operators ^ | && || ?: = += *= %= ^= <<= , Bitwise exclusive OR Bitwise inclusive OR Logical AND Logical OR Ternary conditional Assignment -= Addition/subtraction assignment /= Multiplication/division assignment &= Modulus/bitwise AND assignment |= Bitwise exclusive/inclusive OR assignment >>= Bitwise shift left/right assignment Comma (separate expressions) left-to-right left-to-right left-to-right left-to-right right-to-left

right-to-left

left-to-right

Rules for Evaluation of Expression First, parenthesized sub expression from left to right be evaluated. If parentheses are nested, the evaluation begins with the innermost sub expression. The precedence rule is applied in determining the order of application of operators in evaluating sub expressions. Arithmetic expressions are evaluated from left to right using the rules of precedence. 7.4 Type conversions in Expressions Two types of conversions: 1. Implicit type conversion 2. Explicit type conversion 7.4.1 Implicit type Conversion C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic conversion is known as Implicit Type Conversion. If the operands are of different types, the lower type is automatically converted to the higher type before the operation proceeds. Rules that are applied while evaluating expressions: All short and char are automatically converted to int; Then, If one of the operands is long double, the other will be converted to long double and the result will be long double. Else, if one of the operands is float, the other will be converted to double and the result will be double.

C Operators Else, if one of the operands is float, the other operand will be converted to double and the result will be float. Else, if one of the operands is unsigned long int, the other operand will be converted to unsigned long int and the result will be unsigned long int. The final result of an expression is converted to the type of variable on the left of the =. 2.4.2 Explicit type conversion C performs type conversion automatically. However, there are instances when we want to force a type conversion in a way that is different from the automatic conversion. For example, int a,b; Ratio=a/b; Since a and b are declared as integers in the program, the decimal part of the result of the division would be lot and ratio would represent a wrong figure. Converting locally one of the variables to the floating point as shown below can solve this problem: Ratio=(float)a/b; The operator (float) converts the variable a to floating point for the purpose of evaluation of the expression. Then using the rule of automatic conversion, the division is performed in floating point mode, thus retaining the fractional part of the result. The process of such a local conversion is known as explicit type conversion or casting a value. The general format of a cast is: (type_name)expression;

You might also like