Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator. For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7. Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.
Example Try the following example to understand operator precedence in C − #include <stdio.h>
main() {
int a = 20; int b = 10; int c = 15; int d = 5; int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5 printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5 printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5) printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5) printf("Value of a + (b * c) / d is : %d\n" , e );
return 0; } When you compile and execute the above program, it produces the following result − Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50 |
Every expression in C language contains set of operators and operands. Operators are the special symbols which are used to indicate the type of operation whereas operands are the data member or variables operating as per operation specified by operator. One expression contains one or more set of operators and operands. So to avoid the ambiguity in execution of expression, C compiler fixed the precedence of operator. Depending on the operation, operators are classified into following category. Arithmetic Operator: These are the operators which are useful in performing mathematical calculations. Following are the list of arithmetic operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.
Example: #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter two numbers"); scanf("%d %d",&a,&b); printf("\n Result of addition of %d and %d is %d",a,b,a+b); printf("\n Result of subtraction of %d and %d is %d",a,b,a-b); printf("\n Result of multiplication %d and %d is %d",a,b,a*b); printf("\n Result of division of %d and %d is %d",a,b,a/b); printf("\n Result of modulus of %d and %d is %d",a,b,a%b); getch(); }
Output: Enter two numbers 5 3 Result of addition of 5 and 3 is 8 Result of subtraction of 5 and 3 is 2 Result of multiplication of 5 and 3 is 15 Result of division of 5 and 3 is 1 Result of modulus of 5 and 3 is 2 |
Relational Operators: Relational operator used to compare two operands. Relational operator produce result in terms of binary values. It returns 1 when the result is true and 0 when result is false. Following are the list of relational operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.
Example: #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter two numbers"); scanf("%d %d",&a,&b); printf("\n Result of less than operator of %d and %d is %d",a,b,a<b); printf("\n Result of greater than operator of %d and %d is %d",a,b,a>b); printf("\n Result of leass than or equal to operator %d and %d is %d",a,b,a<=b); printf("\n Result of greater than or equal to operator of %d and %d is %d", a,b,a>=b); printf("\n Result of double equal to operator of %d and %d is %d",a,b,a==b); printf("\n Result of not equal to operator of %d and %d is %d",a,b,a!=b); getch(); } Output: Enter two numbers 5 3 Result of less than operator of 5 and 3 is 0 Result of greater than operator of 5 and 3 is 1 Result of less than or equal to operator of 5 and 3 is 0 Result of greater than or equal to operator of 5 and 3 is 1 Result of double equal to operator of 5 and 3 is 0 Result of not equal to operator of 5 and 3 is 1 |
In a statement or expression if one the operand is real (float) and another one is integer then expression is called as Mixed Mode Arithmetic Expression. If in an expression either operand is of real then output is always in real format. If both operands are real then output will be in real formats. Example Real operand and Integer operand = Real operand (Output) Types of Arithmetic Operators used in Mixed Mode Expression "*" - Multiply use for Multiplication Rules for Evaluation Mixed Mode Arithmetic Expression Rule 1 Evaluate Expressions always from Left to Right For Example: 3 + 5 - 4 = 4 Rule 2 Operator Priorities in descending order
Mixed mode operator example Rule 3 Both Real Numbers double a = 2.0; Double add = 2.0 + 3.0 = 5.0 (Result in Real Number only) Both Integer Number int a = 2; Rule 4 If one of the operator is Integer and other one is real then convert integer number to real number and compute the result in real number only. For Example double a = 5.0; |
Also known as ‘automatic type conversion’.
bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double
It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long is implicitly converted to float).
Example of Type Implicit Conversion:
Output:
x=107, z = 108.000000 |
This process is also called type casting and it is user defined. Here the user can type cast the result to make it of a particular data type. The syntax in C: (type) expression Type indicated the data type to which the result is converted.
|
Logical Operators: Logical operators are used to compare logical values of two operands. Following are the list of logical operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5.
Example: #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter two numbers"); scanf("%d %d",&a,&b); printf("\n Result of logical and operator of %d and %d is %d",a,b,a&&b); printf("\n Result of logical or operator of %d and %d is %d",a,b,a||b); printf("\n Result of logical not operator of %d and and %d is %d",a,b,!(a&&b)); getch(); } Output: Enter two numbers 5 3 Result of logical and operator of 5 and 3 is 1 Result of logical or operator of 5 and 3 is 1 Result of logical not operator of 5 and 3 is 0 |
These operators are used to work on the each bits of data. These operators can work on the binary data. If the data is non binary then computer first of all convert it into binary form and then perform the operation. Following are the list of logical operator in C language. To understand the operation assume variable A contains value 10 and variable contains value 5. So the binary values of A = 0000 1010 and B = 0000 0101
Example: #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter two numbers"); scanf("%d %d",&a,&b); printf("\n Result of bitwise and operator of %d and %d is %d",a,b,a&b); printf("\n Result of bitwise or operator of %d and %d is %d",a,b,a|b); printf("\n Result of bitwise exclusive operator of %d and %d is %d",a,b,a^b); printf("\n Result of bitwise left shift operator of %d and %d is %d",a,b,a<<b); printf("\n Result of bitwise right shift operator of %d and %d is %d",a,b,a>>b); getch(); } Output: Enter two numbers 5 3 Result of bitwise and operator of 5 and 3 is 1 Result of bitwise or operator of 5 and 3 is 7 Result of bitwise exclusive operator of 5 and 3 is 6 Result of bitwise left shift operator of 5 and 3 is 40 Result of bitwise right shift operator of 5 and 3 is 0 |
The following table lists the assignment operators supported by the C language −
Example Try the following example to understand all the assignment operators available in C – #include <stdio.h> main() {
int a = 21; int c ;
c = a; printf("Line 1 - = Operator Example, Value of c = %d\n", c ); c += a; printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a; printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a; printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a; printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200; c %= a; printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
c <<= 2; printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2; printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &= 2; printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2; printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2; printf("Line 11 - |= Operator Example, Value of c = %d\n", c ); } When you compile and execute the above program, it produces the following result − Line 1 - = Operator Example, Value of c = 21 Line 2 - += Operator Example, Value of c = 42 Line 3 - -= Operator Example, Value of c = 21 Line 4 - *= Operator Example, Value of c = 441 Line 5 - /= Operator Example, Value of c = 21 Line 6 - %= Operator Example, Value of c = 11 Line 7 - <<= Operator Example, Value of c = 44 Line 8 - >>= Operator Example, Value of c = 11 Line 9 - &= Operator Example, Value of c = 2 Line 10 - ^= Operator Example, Value of c = 0 Line 11 - |= Operator Example, Value of c = 2
|
#include <stdio.h> int main() { int number1, number2; printf("Enter two integers: "); scanf("%d %d", &number1, &number2);
//checks if the two integers are equal. if(number1 == number2) { printf("Result: %d = %d",number1,number2); }
//checks if number1 is greater than number2. else if (number1 > number2) { printf("Result: %d > %d", number1, number2); }
//checks if both test expressions are false else { printf("Result: %d < %d",number1, number2); }
return 0; } |