OPERATORS AND EXPRESSIONS IN C


OPERATORS AND EXPRESSIONS




ARITHMETIC OPERATORS



OPERATORS DESCRIPTION EXAMPLE(Assuming A=10; B=20;)
+ It adds two operands. A + B will give 30.
- It subtracts second operand from the first. A - B will give -10.
* It multiply both operands. A * B will give 200.
/ It divides numerator by denominator. B / A will give 2.
% It returns the remainder after integer division. B % A will give 0.

☆ FEATURES :

  1. Arithmetic operators are used to perform arithmetic operations.

  2. It consists of operators such as + , -, *, /, %.

  3. 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 :

  1. C programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.

  2. Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.

  3. 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 :

  1. A relational operator checks the relationship between two operands.

  2. If the relation is true, it returns 1; if the relation is false, it returns value 0.

  3. 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 :

  1. An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false.

  2. 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 :

  1. Assignment operators are the short-hand operators which takes less execution time.

  2. It consists of operators such as +=, -=, *=, /=, =.

  3. 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 :

  1. Conditional operators are used to check conditions.

  2. SYNTAX : condition1?condition2:condition3

  3. 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");
}





OPERATORS PRECEDENCE



NO. OPERATORS
1. UNARY
2. ARITHMETIC :
  1. *,/,%
  2. +,-
3. RELATIONAL
4. EQUALITY
5. LOGICAL : && ,||
6. CONDITIONAL
7. ASSIGNMENT

Comments