Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3 1435/1436.

Similar presentations


Presentation on theme: "Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3 1435/1436."— Presentation transcript:

1 Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3 1435/1436 Place photo here

2 The Objectives and the outcomes The Objectives: To learn different operators in C Language To understand the function of C Language operators To understand the input and output of C Language operators The Outcomes: Students would be able to understand the use of C Language Operators Students would be able to write programs using C Language Operators

3 Overview An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C language is rich in built-in operators and provides the following types of operators: Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators

4 Arithmetic Operators: Assume variable A holds 10 and variable B holds 20 then: OperatorDescriptionExample + Adds two operandsA + B will give 30 - Subtracts second operand from the firstA - B will give -10 * Multiplies both operandsA * B will give 200 / Divides numerator by de-numeratorB / A will give 2 % Modulus Operator and remainder of after an integer divisionB % A will give 0 ++ Increments operator increases integer value by oneA++ will give 11 -- Decrements operator decreases integer value by oneA-- will give 9

5 Program to demonstrate working of arithmetic operators in C. CodeOutput #include int main() { int a=9, b=4, c; c=a+b; printf("a+b=%d\n",c); c=a-b; printf("a-b=%d\n",c); c=a*b; printf("a*b=%d\n",c); c=a/b; printf("a/b=%d\n",c); c=a%b; printf("Remainder when a divided by b=%d\n",c); return 0; } a+b=13 a-b=5 a*b=36 a/b=2 Remainder when a divided by b=1

6 Relational Operators: Assume variable A holds 10 and variable B holds 20, then: OperatorDescriptionExample == Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

7 Logical Operators: Assume variable A holds 1 and variable B holds 0, then: OperatorDescriptionExample && Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true. ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(A && B) is true.

8 Bitwise Operators: Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and ^ are as follows: pqp & qp | qp ^ q 00000 01011 11110 10011

9 Assignment Operators: OperatorDescriptionExample = Simple assignment operator, Assigns values from right side operands to left side operand C = A + B will assign value of A + B into C += Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand C += A is equivalent to C = C + A -= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand C -= A is equivalent to C = C - A *= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand C *= A is equivalent to C = C * A /= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand C /= A is equivalent to C = C / A

10 %= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand C %= A is equivalent to C = C % A <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator C &= 2 is same as C = C & 2 ^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2 |= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2 Assignment Operators:

11 Operators Precedence in C: No. CategoryOperatorAssociativity 1 Postfix () [] ->. ++ - - Left to right 2 Unary + - ! ~ ++ - - (type)* &sizeof Right to left 3 Multiplicative * / % Left to right 4 Additive + - Left to right 5 Shift > Left to right 6 Relational >= Left to right 7 Equality == != Left to right 8 Bitwise AND & Left to right Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator. 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.

12 No. CategoryOperatorAssociativity 9 Bitwise XOR ^ Left to right 10 Bitwise OR | Left to right 11 Logical AND && Left to right 12 Logical OR || Left to right 13 Conditional ?: Right to left 14 Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left 15 Comma, Left to right Operators Precedence in C:

13 Example 1: Write a program to find the area of a circle. Display the result on the screen. Code:Output #include int main() { float radius, area; printf("\nEnter the radius of Circle : "); scanf("%d", &radius); area = 3.14 * radius * radius; printf("\nArea of Circle : %f", area); return (0); } Enter the radius of Circle : 2.0 Area of Circle : 6.14

14 Example 2: Write a program to calculate the Area of a rectangle using formula; Area = Length x Width. Code:Output #include int main() { int length, breadth, area; printf("\nEnter the Length of Rectangle : "); scanf("%d", &length); printf("\nEnter the Breadth of Rectangle : "); scanf("%d", &breadth); area = length * breadth; printf("\nArea of Rectangle : %d", area); return (0); } Enter the Length of Rectangle : 5 Enter the Breadth of Rectangle : 4 Area of Rectangle : 20

15 Example 3: Program to display Addition, Subtraction, Multiplication, Division, Divisor, Increment and Decrement functions for two numbers. Code: Output: #include main() { int a = 21; int b = 10; int c ; c = a + b; printf("Line 1 - Value of c is %d\n", c ); c = a - b; printf("Line 2 - Value of c is %d\n", c ); c = a * b; printf("Line 3 - Value of c is %d\n", c ); c = a / b; printf("Line 4 - Value of c is %d\n", c ); c = a % b; printf("Line 5 - Value of c is %d\n", c ); c = a++; printf("Line 6 - Value of c is %d\n", c ); c = a--; printf("Line 7 - Value of c is %d\n", c ); } Line 1 - Value of c is 31 Line 2 - Value of c is 11 Line 3 - Value of c is 210 Line 4 - Value of c is 2 Line 5 - Value of c is 1 Line 6 - Value of c is 21 Line 7 - Value of c is 22


Download ppt "Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills 4800153-3 1435/1436."

Similar presentations


Ads by Google