Arithmetic operators are used to perform arithmetic operations.
It consists of operators such as + , -, *, /, %.
Arithmetic operators have highest precedence.
#include <stdio.h>
void main()
{
int a,b,add,sub,mul,div,mod;
printf("ENTER TWO NUMBERS a and b :");
scanf("%d%d",&a,&b);
add=a+b;
sub=a-b;
mul=a*b;
div=a/b;
mod=a%b;
printf("\nADDITION : %d",add);
printf("\nSUBTRACTION : %d",sub);
printf("\nMULTIPLICATION %d:",mul);
printf("\nDIVISION : %d",div);
printf("\nMODULUS : %d",mod);
}
UNARY OPERATORS
OPERATORS
DESCRIPTION
EXAMPLE(Assuming A=10; B=20;)
++
It increases integer value by one.
A++ will give 11.
--
It decreases integer value by one.
A-- will give 9.
☆ FEATURES :
C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.
They only operate on a single operand.
#include <stdio.h>
void main()
{
int a = 10, b = 20;
float c = 10.5, d = 20.5;
printf("++a = %d \n", ++a);
printf("--b = %d \n", --b);
printf("++c = %f \n", ++c);
printf("--d = %f \n", --d);
}
RELATIONAL OPERATORS
OPERATORS
DESCRIPTION
EXAMPLE(Assuming A=10; B=20;)
==
Checks if the value of two operands are equal or not, if yes then condition becomes true.
(A == B) is not true.
!=
Checks if the value 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 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 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.
>=
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.
☆ FEATURES :
A relational operator checks the relationship between two operands.
If the relation is true, it returns 1; if the relation is false, it returns value 0.
Relational operators are commonly used in decision making and loops.
#include <stdio.h>
void main()
{
int a = 10, b = 10, c = 20;
printf("%d == %d = %d \n", a, b, a == b); // true
printf("%d == %d = %d \n", a, c, a == c); // false
printf("%d > %d = %d \n", a, b, a > b); //false
printf("%d > %d = %d \n", a, c, a > c); //false
printf("%d < %d = %d \n", a, b, a < b); //false
printf("%d < %d = %d \n", a, c, a < c); //true
printf("%d != %d = %d \n", a, b, a != b); //false
printf("%d != %d = %d \n", a, c, a != c); //true
printf("%d >= %d = %d \n", a, b, a >= b); //true
printf("%d >= %d = %d \n", a, c, a >= c); //false
printf("%d <= %d = %d \n", a, b, a <= b); //true
printf("%d <= %d = %d \n", a, c, a <= c); //true
}
LOGICAL OPERATORS
OPERATORS
DESCRIPTION
EXAMPLE(Assuming A=10; B=10; C=20;)
&&
Called Logical AND operator. If both the operands are non-zero then condition becomes true.
(A==B && B<C) is true.
||
Called Logical OR Operator. If any of the two operands are non zero then condition becomes true.
(A==B || B<C) 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 && B<C) is false.
☆ FEATURES :
An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.
Logical operators are commonly used in decision making in C programming.
#include <stdio.h>
void main()
{
int a = 10, b = 10, c = 20, result;
result = (a == b) && (c>b);
printf("(a == b) && (c>b) equals to %d \n", result);
result = (a == b) && (c<b);
printf("(a == b) && (c<b) equals to %d \n", result);
result = (a == b) || (c<b);
printf("(a == b) || (c<b) equals to %d \n", result);
result = (a != b) || (c<b);
printf("(a != b) || (c<b) equals to %d \n", result);
result = !(a != b);
printf("!(a == b) equals to %d \n", result);
result = !(a == b);
printf("!(a == b) equals to %d \n", result);
}
ASSIGNMENT OPERATORS
OPERATORS
DESCRIPTION
EXAMPLE
=
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.
/=
Division AND assignment operator, It divide right operand with the left operand and assign the result to left operand.
C /= A is equivalent to C = C / A.
☆ FEATURES :
Assignment operators are the short-hand operators which takes less execution time.
It consists of operators such as +=, -=, *=, /=, =.
It has lowest precedence among all operators.
CONDITIONAL OPERATORS
OPERATORS
DESCRIPTION
EXAMPLE(Assuming A=10; B=20;)
?:
Conditional Expression.
If Condition is true ? Then value X : Otherwise value Y.
☆ FEATURES :
Conditional operators are used to check conditions.
SYNTAX :
condition1?condition2:condition3
Here if the condition1 is TRUE then condition2 is executed and if it is FALSE then condition3 is executed.
#include <stdio.h>
void main()
{
int a,b;
printf("ENTER A AND B : ");
scanf("%d %d",&a,&b);
a>b?printf("A is GREATER"):printf("B is GREATER");
}
Comments
Post a Comment