Ticker

6/recent/ticker-posts

C Operators

 Chapter 5: Operators



 




Operators in C are symbols that perform various
operations on operands (variables, constants, expressions). They are classified
into different categories, such as arithmetic operators, relational operators,
logical operators, bitwise operators, and assignment operators. Let's explore
each category:



 



5.1
Arithmetic Operators:



Arithmetic operators perform mathematical
calculations on operands.



 



                                
·           
Addition: `+` (Adds two operands)



                                
·           
Subtraction: `-` (Subtracts the second
operand from the first)



                                
·           
Multiplication: `*` (Multiplies two
operands)



                                
·           
Division: `/` (Divides the first operand
by the second)



                                
·           
Modulus: `%` (Returns the remainder of
the division)



 



Here's
an example:





#include
<stdio.h>



    int main() {



    int a = 10, b = 4;



    int sum = a + b;



    int difference = a - b;



    int product = a * b;



    int quotient = a / b;



    int remainder = a % b;



    printf("Sum: %d\n", sum);



    printf("Difference: %d\n",
difference);



    printf("Product: %d\n", product);



    printf("Quotient: %d\n",
quotient);



    printf("Remainder: %d\n",
remainder);



    return 0;



}





 



 



The program performs various arithmetic operations
on variables `a` and `b` and displays the results using `printf()`. Compile and run the program to see the output.



 



5.2
Relational Operators:



Relational operators compare the relationship
between operands and return a boolean result (`1` for true or `0` for false).



 



·        
Equality: `==` (Checks if two operands
are equal)



·        
Inequality: `!=` (Checks if two operands
are not equal)



·        
Greater than: `>` (Checks if the
first operand is greater than the second)



·        
Less than: `<` (Checks if the first
operand is less than the second)



·        
Greater than or equal to: `>=`
(Checks if the first operand is greater than or equal to the second)



·        
Less than or equal to: `<=` (Checks
if the first operand is less than or equal to the second)



Example:





#include
<stdio.h>



int
main() {



    int a = 5, b = 3;



    int result1 = (a == b);



    int result2 = (a != b);



    int result3 = (a > b);



    int result4 = (a < b);



    int result5 = (a >= b);



    int result6 = (a <= b);



 



    printf("Result 1: %d\n",
result1);



    printf("Result 2: %d\n",
result2);



    printf("Result 3: %d\n",
result3);



    printf("Result 4: %d\n",
result4);



    printf("Result 5: %d\n",
result5);



    printf("Result 6: %d\n",
result6);



 



    return 0;



}





 



 



The program compares variables `a` and `b` using
relational operators and stores the boolean results in different variables.
Compile and run the program to see the output.



 



5.3
Logical Operators:



Logical operators perform logical operations on
boolean operands and return a boolean result.



 



·        
Logical AND: `&&` (Returns true
if both operands are true)



·        
Logical OR: `||` (Returns true if either
operand is true)



·        
Logical NOT: `!` (Reverses the boolean
value of the operand)



 



Example:





#include
<stdio.h>



int
main() {



    int a = 5, b = 3;



    int result



 



1
= (a > 0 && b > 0);



    int result2 = (a > 0 || b > 0);



    int result3 = !(a > 0);



 



    printf("Result 1: %d\n",
result1);



    printf("Result 2: %d\n",
result2);



    printf("Result 3: %d\n",
result3);



 



    return 0;



}





 



 



The program performs logical operations on variables
`a` and `b` and stores the boolean results in different variables. Compile and
run the program to see the output.



 



5.4
Bitwise Operators:



Bitwise operators perform operations on individual
bits of operands.



 



·        
Bitwise AND: `&` (Performs bitwise
AND operation)



·        
Bitwise OR: `|` (Performs bitwise OR
operation)



·        
Bitwise XOR: `^` (Performs bitwise
exclusive OR operation)



·        
Bitwise NOT: `~` (Performs bitwise NOT
operation)



·        
Left Shift: `<<` (Shifts the bits
of the left operand to the left by the number of positions specified by the
right operand)



·        
Right Shift: `>>` (Shifts the bits
of the left operand to the right by the number of positions specified by the
right operand)



 



Example:





#include
<stdio.h>



int
main() {



    int a = 5, b = 3;



    int result1 = a & b;



    int result2 = a | b;



    int result3 = a ^ b;



    int result4 = ~a;



    int result5 = a << 1;



    int result6 = a >> 1;



 



    printf("Result 1: %d\n",
result1);



    printf("Result 2: %d\n",
result2);



    printf("Result 3: %d\n",
result3);



    printf("Result 4: %d\n",
result4);



    printf("Result 5: %d\n",
result5);



    printf("Result 6: %d\n",
result6);



 



    return 0;



}





The program performs bitwise operations on variables
`a` and `b` and stores the results in different variables. Compile and run the
program to see the output.



 



5.5
Assignment Operators:



Assignment operators are used to assign values to
variables.



·        
Assignment: `=` (Assigns the value of
the right operand to the left operand)



·        
Compound Assignment: `+=`, `-=`, `*=`,
`/=`, `%=` (Performs an arithmetic operation on the operands and assigns the
result to the left operand)



 





Example:



#include
<stdio.h>



int
main() {



    int a = 5, b = 3;



   



    a += b; // Equivalent to a = a + b;



    printf("a: %d\n", a);



   



    b *= 2; // Equivalent to b = b * 2;



    printf("b: %d\n", b);



   



    return 0;



}





 



 



The program demonstrates the use of compound
assignment operators. Compile and run the program to see the output.

Post a Comment

0 Comments