9. Operators in C

Table of Contents
< All Topics
Print

9. Operators in C

Assignment Operators

The assignment operator is responsible for assigning values to the variables. While the equal sign (=) is the fundamental assignment operator, C also supports other assignment operators that provide shorthand ways to represent common variable assignments. They are shown in the table. Assignment operators are used to assign the result of an expression to a variable. C has a variety of shorthand assignment operators.

OPERATORSYNTAXEQUIVALENT TO
/=variable /= expressionvariable = variable / expression
\=variable \= expressionvariable = variable \ expression
*=variable *= expressionvariable = variable * expression
+=variable += expressionvariable = variable + expression
-=variable -= expressionvariable = variable – expression
&=variable &= expressionvariable = variable & expression
^=variable ^= expressionvariable = variable ^ expression
<<=variable <<= amountvariable = variable << amount
>>=variable >>= amountvariable = variable >> amount

Logical Operators

Logical operators are used to evaluate two or more conditions. In General, Logical operators are used to combine relational expressions, but they are not limited to just relational expression you can use any kind of expression even constants. If the result of the logical operator is true then1is returned otherwise0is returned. C language supports three logical operators. They are- Logical AND (&&), Logical OR (||) and Logical NOT (!).
As in case of arithmetic expressions, the logical expressions are evaluated from left to right.

OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then the condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true.(A || B) is true.
!Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.!(A && B) is true.
ABA &&B
000
010
100
111
Results of && (AND) operator
ABA || B
000
011
101
111
Results of || (OR) operator
A! A
01
10
Results of !(NOT) operator

Example: Program to check logical operators

//Sample program to check logical operators
#include 

int main() {

   int a = 10;
   int b = 20;
   int c ;

   if ( a && b ) {
      printf("Statement 1 - Status of Condition is true\n" );
   }

   if ( a || b ) {
      printf("Statement 2 - Status of Condition is true\n" );
   }
   
   /* Now change the value of  a and b */
   a = 0;
   b = 10;

   if ( a && b ) {
      printf("Statement 3 - Status of Condition is true\n" );
   } else {
      printf("Statement 3 - Status of Condition is not true\n" );
   }

   if ( !(a && b) ) {
      printf("Statement 4 - Status of Condition is true\n" );
   }

 return 0;
}

Output

Image 13

Example 1:

int age = 10, height = 45; 
(age < 12 && height < 48) || (age > 65 && height > 72);

Solution: In this case, the order of evaluation of operators will be parentheses (()), relational operators (<,>), AND (&&) operator, OR (||) operator.

(age < 12 && height < 48) || (age > 65 && height > 72) 
=> (10 < 12 && 45 < 48) || (10 > 65 && 45 > 72) 
=> (1 && 1) || (10 > 65 && 45 > 72) => 1 || (10 > 65 && 45 > 72) 
=> 1 || (0 && 0) 
=> 1 || 0 
=> 1

Example 2:

int year = 2000; 
(year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0);

Solution: In this case, the order of evaluation of operators will be parentheses (()), Modular division (%), Relational operators (==,!=) AND (&&) operator, OR (||) operator.

(year % 4 == 0 && year % 100 != 0 ) || (year % 400 == 0) 
=> (2000 % 4 == 0 && 2000 % 100 != 0 ) || (2000 % 400 == 0) 
=> (0 == 0 && 2000 % 100 != 0 ) || (2000 % 400 == 0) 
=> (0 == 0 && 0 != 0 ) || (2000 % 400 == 0) 
=> (1 && 0 != 0 ) || (2000 % 400 == 0) 
=> (1 && 0 ) || (2000 % 400 == 0) 
=> 0 || (2000 % 400 == 0) 
=> 0 || (0 == 0) 
=> 0 || 1 
=> 1
Previous Section | Next Section

Leave a comment