Operators
Operators are the symbols, which specify the operations that can be performed on variables and contains and the excepted results. The data on which the operations are performed are called the operands. The operators provided by C language are divided into four categories:- Arithmetic operators
- Logical Operators
- Assignment Operators
- Relational Operators
Operators, which are used in mathematical calculation is called arithmetic operators. Arithmetic operators used in c are:
Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulus(%), increment (++) and decrement (--) operators.
For example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #include<stdio.h> void main(){ int x, y; x = 40; y = 5; printf("Sum is %d", x+y); printf("\nDifference is %d", x-y); printf("\nProduct is %d", x*y); printf("\nQuotient is %d",x/y); printf("\nRemainder is %d", x%y); x++; printf("\nIncrement of x %d", x); y--; printf("\nDecrement of x %d", y); getch(); } |
Output
Sum is 45
Difference is 35
Product is 200
Quotient is 8
Remainder is 0
Increment of x 41
Decrement of x 4
Product is 200
Quotient is 8
Remainder is 0
Increment of x 41
Decrement of x 4
Logical Operators
There are three logical operators in C they are:
- ! (Logical NOT)
- && (Logical AND)
- || (Logical OR)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include<stdio.h> void main(){ int x=10, y=5; if(x!=5){ printf("Value of x is not 5"); } if(x==10 && y==5){ printf("\nYes ! value of x = 10 and y = 5"); } if(x>=15 || y>=10){ printf("\n x is greater then 15 and y is greater then 10"); } getch(); } |
Output
Value of x is not 5
Yes ! value of x = 10 and y = 5
Yes ! value of x = 10 and y = 5
In the above program value of variable x is 10 and y is 5. In line 4, condition was if(x!=5) x is not equal to 5 then it will print Value of x is not 5, and condition become true because value of x is 10 not 5, that's why it prints Value of x is not 5 on the terminal window.
In logical AND (&&) if all of the given condition is true then it will print true result otherwise false. In second if condition x==10 and y==5 then it will print Yes! value of x = 10 and y = 5 in the output window. The condition became true because value of x is 10 and y is 5 and it display the result.
In third if condition x>=15 or y>=10 then it will print x is greater then 15 and y is greater then 10 other wise it will not display any output on the screen. In logical or condition if any one of the given condition is true the it will display the true result. But here both of the condition are false because value of x is 10, which is not greater then 15 and value of y is 5, which is also not greater then 10, so it is not printing any result in the terminal window.
Assignment Operators
Assignment operators like =, +=, etc are used to assign values to variables. In C, the following Assignment is possible.
a = b = c = d;
Assigning values are done from right to left. d's value is assigned to c, c is assigned to b and b is assigned to a.
In C, expression like x=x+1 and a = a+b are used quite frequently. To facilitate a simple shortcut to these expressions, operators like +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |= are used.
Hence, x=x+1 is analogous to x+=1 and a=a+b is analogous to a+=b.
Relational Operators
There are six relational operators in C. They are:
- > (greater than)
- >= (greater than or equal to)
- < (less than)
- <= (less than or equal to)
- == (equal to equal to)
- != (not equal to)
Ternary Operators (?:)
C's only ternary operator is ?:. Use of this operator results in terse and compact code. The following is the syntax of a ternary operator.<conditional_expression> ? <expression1> : <expression2>
It is just like a if then statement. If the <conditional_expression> results true value, <expression1> will be executed, otherwise <expression2) will be executed. For example:
1 2 3 4 5 6 | #include<stdio.h> void main(){ int n = 5; (n%2 == 0)? printf("%d is Even.",n) : printf("%d is Odd.",n); getch(); } |
Output
5 is Odd.
Comma Operators (,)
The final C operator that is the comma operator. The comma operator is generally used in the for statement for looping purposes. The expressions separated by comma are evaluated from left to right. On using the comma operator the value and type of the right most operand is returned. For example. in the assignment statement below:i = (j=1, k=2, 3, 4)
The expression j=1, k2, 3, 4 are evaluated first. Then the value of the expression is returned as 4 and assigned to i.
0 comments:
Post a Comment