# operators in C

Views:

Category: Education

c fundamentals

## Presentation Transcript

### Operators in C :

By : Ms.Meena Chauhan,Sr.Assist.Prof Operators in C

### Operators in C:

Operators in C Operators arethe verbs of a language that help the user perform computations on values. C language supports a rich set of operators. Different types of operators in C are : Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators

### Arithmetic Operators:

Arithmetic Operators These are used to perform arithmetic operations. All of these can be used as binary operators. These are : + add - subtract * multiply / divide( divisor must be non zero ) % modulo(gives remainder after div) The parenthesis() are used to clarify complex operations. The operators + and - cab be used as unary plus and unary minus arithmetic operators also. The unary – negates the sign of it’s operand .

### PowerPoint Presentation:

Note : C language has no operator for exponentiation. The function pow(x,y) which exists in math.h returns X y Following are some examples of arithmetic operators : x+y, x-y, x*y, x/y, x%y, -x*y Here x and y are operands. The % operator cannot be used on floating point data type.

### Arithmetic Expressions:

Arithmetic Expressions An expression consisting of numerical values(either any number, variable or even some function call) joined together by arithmetic operators is known as an arithmetic expression. For example , consider the following expression : (x-y)*(x+y)/5 Here x,y and 5 are operands and the symbols -,*,+,/ are operators. The precedence of operators for the expression evaluation has been given by using parenthesis which will over rule the operators precedence. If x=25 and y=15,then the value of this expression will be 80.

### Arithmetic Expressions:

Arithmetic Expressions Consider the following expression : 3*((i%4)*(5+(j-2)/(k+3))) Where I,j,k are integer variables. If I,j ,k have values 9 ,14 ,16 respectivly then above expression would be evaluated as 3 * ((9%4) * (5 + (14 – 2) / (6 +3 ))) = 3 * ( 1 * (5 + (12 / 9) ) ) = 3 * ( 1 * (5 + 1)) = 3 * ( 1 * 6 ) = 3 * 6 = 18

### Modes of Expression:

Modes of Expression In C, we can have an arithmetic statement of the following types : Integer mode arithmetic statement Real mode arithmetic statement Mixed mode arithmetic statement

### Integer mode arithmetic statement:

Integer mode arithmetic statement It consist of all operands as either integer constants or integer variables. For example , int s, y, I, j, result ; x=50; y=x+10; i=2; j=i*3; result=x/7 – y%j + i; In case of integer division, result is turnicated towards zero when both the operands are of the same sign. When one of the operands is negatic the turnication depends upon the implementation. For example :

### Integer mode arithmetic statement:

Integer mode arithmetic statement 3/5 = 0 and -3/-5 =0 But -3/5 can result into 0 or -1 (it is machine dependent) For modulo division,the sign of result is always that of the first operand or dividend. For example, 13 % - 5 = 3 - 13 % - 5 = - 3 - 13 % 5 = - 3

### Integer mode arithmetic statement:

Integer mode arithmetic statement /*sum of 4 digit number*/ #include<stdio.h> main() {int num,digit,sum=0; clrscr(); printf(“Enter 4 digit no”); scanf(“%d”, &num); digit=num%10; sum=sum+digit; num=num/10; digit=num%10; sum=sum+digit; num=num/10; digit=num%10; sum= sum+digit ; num=num/10; digit=num%10; sum= sum+digit ; printf (“sum is % d”,sum ); getch();} Output ---------- Enter 4 digit no 3275 Sum is 17

### Real mode arithmetic statement:

Real mode arithmetic statement It consist of all operands as either real variables or real constants For example, float a,b,d; a=5.0; b=a*20.5; d=a / 2.0 * b – 10.0; In C, real operand may store value in either of the two forms i.e., in fractional form or exponential form. The rounding takes place in such cases and the final result of an expression in real mode is alwys an approximation of the calculation performed. We cannot use % operator with real operands.

### Real mode arithmetic statement:

Real mode arithmetic statement The following program illustrates the use of real mode arithmetic : /*conversion of centigrade temperature to fahrenheit */ #include<stdio.h> main() {float cent,fahr; clrscr(); printf(“Enter centegrade “); scanf(“%f”,&cent); fahr=1.8*cent+32.0; printf(“Centegrade temp is : %f”,cent); printf(“fahr temp is : %f”,fahr); }

### Mixed mode arithmetic statement:

Mixed mode arithmetic statement It consists of operands of both types i.e., integers and reals. When any one of the two operands is real, the expression is known as mixed mode arithmetic expression and the operation performed in such a case results in a real value as the answer. For example, consider the following division : 24/10.0=24 If we write 24/10 then the result will be 2 as seen earlier. The following program illustrates the use of mixed mode arithmetic :

### Mixed mode arithmetic statement:

Mixed mode arithmetic statement /*sum and average of three numbers */ #include<stdio.h> main() {int num1,num2,num3,sum=0; float avg=0.0; printf(“enter three numbers”); scanf(“%d%d%d”,&num1,&num2,&num3); sum=num1+num2+num3; avg=sum/3.0; printf(“sum is %d:”,sum); printf(“average is %f”,avg); }

### Mixed mode arithmetic statement:

Mixed mode arithmetic statement The following points must be taken into account when we write arithmetic instructions or expressions : Only one variable is allowed when we use an = (assignment) operator in an arithmetic statement. For example, int i,j,k ; i =2; j=7; k= i+j ; /*valid c statements i+j =k; Character constants can also be assigned to character variables like the assignement of integer and float type expressions to int and float type variables. For example : char ch1,ch2; ch1=‘A’; ch2=‘y’; Arithmetic operations are allowed on integer float and character type values i.e.,int float and char data type . The following program illustrates this concept :

### PowerPoint Presentation:

/*illustration of arithmetic operations on char data type */ #include<stdio.h> main() { int I,j; char ch1,ch2; clrscr(); i=10; ch1=‘A’; /* ASCII value of A is 65 */ j=ch1-I; ch2=j+42; printf(“%d%d %c %c”,I,j,ch1,ch2); } output : 10 55 A a

### Arithmetic Operators Precedence:

Arithmetic Operators Precedence In C, the arithmetic operators have the priority as shown below: First priority * ? % Second priority + - Third priority = The sequence of operations in evaluating an arthmetic expression is also known as hierarchy of operations. This is necessary to avoid anydoubt while evaluating an expression. The following precedence rules are followed in expression evaluation :

### Arithmetic Operators Precedence:

Arithmetic Operators Precedence All the subexpressions withing the parentheses are evaluated first. Nested parenthesized subexpressions are evaluated inside-out, with the innermost expression being firt to be evaluated. Operators in the same sub expression are evaluated as given : *, / ,% perform first +, - performed next . Any function referenced (i.e.,invoked)in the expression gets the highest precedence over all the arithmetic operators. Operators in the same expression with the same priority are evaluated from left to right.

### Arithmetic Operators Precedence:

Arithmetic Operators Precedence For example : consider the following expression for checking the operators precedence. 15 * 7 / ( 2 – 3 * 5 / 7 + 4 ) – 7 * 9 % 4 = 15 * 7 / (2 – 15 / 7 + 4 ) – 7 * 9 % 4 = 15 * 7 / (2 – 2 + 4) – 7 * 9 % 4 = 15 * 7 / 4 – 7 * 9 % 4 = 105 / 4 - 63 % 4 = 26 – 3 = 23

### Increment and Decrement Operator:

Increment and Decrement Operator C language has two useful operators called increment(++) and decrement (--) that operate on integer data only. The increment (++) operator increments the operand by 1, while the decrement operator (--) decrements the operand by 1, for example ,: int i , j; i = 10; j = i++ ; printf(“ %d %d “, i, j); OUTPUT 11 10 . First i is assigned to j and then i is incremented by 1 i.e.,post-increment takes place

### Increment and Decrement Operator:

Increment and Decrement Operator If we have : int i, j ; I = 20; j = ++i; printf(“%d %d”, i, j); OUTPUT : 21 21. first i is incremented by 1 and then assignment take place i.e., pre-increment of i. now, consider the example for (--) operator : int a,b; a=10; b= a--; printf(“%d %d”, a , b) OUTPUT : 9 10. first a is assigned to b then a is decremented by 1. i.e.,post decrement takes place

### Decrement Operator:

Decrement Operator If we have : int i, j ; I = 20; j = --i; printf(“%d %d”, i, j); OUTPUT : 19 19. first i is decremented by 1 and then assignment take place i.e., pre-decrement of i. Note : on some compilers a space is required on both sides of ++I or i++ , i-- or --i

### Relational Operators:

Relational Operators These are used to compare two variables or constants . C has the following relational operators : OPERATOR MEANING == Equals != Not Equals < Less than > Greater than <= Less than or equals >= Greater than or equals

### Logical Operators:

Logical Operators In C, we can have simple conditions (single) or compound conditions(two or more). The logical operators are used to combine conditions. The notations for these operators is given below : Operator Notation in C NOT ! AND && OR || The notation for the operator OR is given by two broken lines. These follow the same precedence as in other language. NOT(!) is evaluated before AND(&&) which is evaluatedbefore OR(||). Parenthesis( ) cab be used to change this order.

### Relational and Logical Expressions:

Relational and Logical Expressions An expression involving a relational operator is known as a relational expression. The resulting expression will be of integer type, since in C, true is represented by 1 and false by 0, for example : int a=2, b=3, c=4; Relational Expression Value a<b 1(true) c<=a 0(false) b==c 0(false) c>=b 1(true) a!=c 1(true) a>1 1(true) When a relational operation is carried out, different types of operands(ifpresent) will be converted appropriately.

### Relational and Logical Expressions:

Relational and Logical Expressions Note : don’t use = for testing equality. The operator for testing equality is == (two = signs together). In C, the arithmetic operators have higher priority over relational operators. Relational operators are used with if, while, do….while statements to form relational expressions which help in making useful decisions. An expression formed with two or more relational expressions is known as a logical expression or compound relational expression. For example : int day; day>=1&&day<=31

### Relational and Logical Expressions:

Relational and Logical Expressions The logical expression given above is true, when both the relational expressions are true. If either of these or both are false, it evaluates as false. The following truth tables gives the result of applying &&, || operators on two operands : Operand1 operand2 operant1&&operand2 operand1||operand2 1 1 1 1 1 0 0 1 0 1 0 1 0 0 0 0 Here 1 denotes non-zero or true and 0 denote zero or false. The !(logical NOT) operator negates the value of any logical expression i.e.,true expression becomes false and vice-verse

### Relational and Logical Expressions:

operand expression value ! Operand 1 0 0 1 The following examples illustrate the logical expressions. int a=10, b=50; char ch1=‘A’; float x=9.5; Logical expression Value a+x<=b 1 (true) a<b&& ch1==‘A’ 1 (true) b-x<5.0||ch1!=‘A’ 0(false) Relational and Logical Expressions

### PowerPoint Presentation:

Arithmetic operations are carried out prior to relational and logical operations. The relational operations take place before the logical operations. In the last of the above example, first operation carried out is substraction(i.e,b-x), then the comparison (i.e,b-x<5.0), after that the quality comparison (i.e.,ch1!=‘A’) and finally the logical || is carried out.

### Precedence of Relational Operators and Logical Operators:

Precedence of Relational Operators and Logical Operators Each operator in C has a precedence of its own. It helps in evaluation of an expression. Higher the precedence of the operator, earlier it operates. The operators having same precedence are evaluated either from left to right or from right to left, depending on the level, known as the associativity of the operator. ! , < , <= , > , >=, ==, !=, ==, !=, &&, ||

### The Conditional Operator:

The Conditional Operator This operator ? And : together forms a ternary operator called as the conditional operator. Syntax : (test-expression) ? T-expr : F-expr ; Let us see example program :

### The conditional operator:

The conditional operator #include<stdio.h> main() { int age; clrscr(); printf)(“Enter ur age”); scanf(“%d”,&age); (age>18)? Printf(“Eligible to vote”):printf(“nt eligible”); getch(); }

### Bitwise Operators:

Bitwise Operators These are used to perform bitwise operations such as testing the bits, shifting the bits to left to right, one’s complement of bits etc. these operations can be applied onlyon int and char data types but not on float and double data types. Various bitwise operators in C language are : ~ Bitwise (1’s) complement) << shift left >> shift right & bitwise AND ^ bitwise XOR(Exclusive OR) | bitwise OR

### Special Operator:

Special Operator C provides the following special operators : Comma Operrator sizeof operator Address operator Derefencing operator Dot operator Member selection operator Pointer

### The Comma Operator:

The Comma Operator The comma operator (,) has the lowest precedence. The comma operator is mainly used in for statement. For example : int i , j; for(i=1 , j=400 ; i<=10 ; ++I , j/=2) printf(“%d\n”, i+j ) ; The initial value of i is 1 and that of j is 400 and every time the value of i is incremented by 1 and that of j is divided by 2 after execution of the body of the for loop . The distinct expression on either side of the comma operator are evaluated from left to right. The associativity of comma operator is from left to right .

### The sizeof Operator:

The sizeof Operator It is a unary operator which provides the size , in bytes, of the given operand. The syntax of sizeof operator is : sizeof(operand) Here the operand is a built in or user defined data type or variable. The sizeof operator always precedes its operand. For example, sizeof (float) returns the value 4 . The sizeof operator mainly used in dynamic memory allocation for calculating the number of bytes used by some user defined data type.

### Precedence of operators among themselves and across all the sets of operators.:

Precedence of operators among themselves and across all the sets of operators. The TURBO C operators are divided into the following 16 categories : these are ordered from the highest precedence to the lowest precedence. The operation within each category have equal precedence.

### Precedence of operators among themselves and across all the sets of operators.:

Category Operator What it does ? 1. Highest precedence () Function call [] Array subscript -> C indirect component selector 2.Unary ! NOT ~ Bitwise(1’s) component + Unary plus - Unary minus 3.Member acces .* Dereference ->* Dereference 4.Multiplication * Multiply / Divide % Remainder (Modulus) Precedence of operators among themselves and across all the sets of operators.

### Precedence of operators among themselves and across all the sets of operators.:

Category Operator What it does ? 5.Additive + Binary plus - Binary minus 6.Shift << Shift left >> Shift right 7.Relational < Less than <= Less than or equal to > Greater than >= Greater than equal to 8.Equality == Equal to != Not equal to 9.Bitwise AND & Bitwise AND 10.Bitwise XOR ^ Bitwise XOR 11.Bitwise OR | Bitwise OR Precedence of operators among themselves and across all the sets of operators.

### Precedence of operators among themselves and across all the sets of operators.:

Category Operator What it does ? 12.Logical AND && Logical AND 13.Logical OR || Logical OR 14.Conditional ?: ( exp?x:y ) 15.Assignment = Simple assignment *= Assign product /= Assign quotient %= Assign remainder (modulus) += Assign sum -= Assign difference &= Assign bitwise AND ^= Assign bitwise XOR |= Assign bitwise OR <<= Assign left shift >>= Assign right shift 16.Comma , Evaluate Precedence of operators among themselves and across all the sets of operators.

### The Associativity of Operators:

The Associativity of Operators In C , the operators having the equal precedence are evaluated either from left to right or from right to left, depending on the level. It is known as associativity property of the operator.

### Associativity of the Operator:

Associativity of the Operator Category Operator Associativity 1.Highest precedence ( ) Left to Right [ ] -> :: . 2.Unary ! ~ + - ++ -- & * sizeof

### Associativity of the Operator:

Associativity of the Operator Category Operator Associativity 3.Member access .* Left to Right ->* 4.Multiplication * Left to right / % 5.Additive + Left to Right - 6.Shift << Left to Right >> 7.Relational < Left to Right <= > >=

### Associativity of the Operator:

Associativity of the Operator Category Operator Associativity 8.Equality == Left to Right != 9.Bitwise AND & Left to right 10.Bitwise XOR ^ Left to Right 11.Bitwise OR | Left to Right 12.Logical AND && Left to Right 13.Logical OR || Left to Right 14.Conditional ?: Right to Left 15.Assignment = Right to Left *= /= %= += -=

### Associativity of the Operator:

Associativity of the Operator Category Operator Associativity &= Right to Left ^= |= <<= >>= 16.Comma . Left to Right