Let us dive into C Part-5 (Operators & Operands)

Spread to the Techies

C Operators and Operands

Hello Readers!!!!!

How are you doing? I wish you are doing well. If you face any difficulties then please let me know here with comments.

Today we re going to learn about the Operators and Operands in C.

What is Operator?

Operators are the foundation of any programming language. We can define operators as symbols that helps us to perform specific mathematical and logical computations on operands. In other words we can say that an operator operates the operands.

For example we can consider the below expression:

a = b+c;

here, we got a, b and c are the operands and ‘+’ and ‘=’ are the operators. The ‘+’ is known as addition operator and ‘=’ is known as assignment operator. Now, the full expression means to us as, add b and c operands then assign the result to the operand a.

C programming language has many built-in operators. We can classify them as below:

Arithmetic Operators:  These operators are used to perform the arithmetic and mathematical operations on variables or operands. We can find them in the mathematical operations and expressions. They are, (+, -, /, *, %, ++, – -)

We can divide arithmetic operators in two sections.

Binary Operators: Operators that operates or works with a single operand are unary operators.
For example: (++ , –)

Unary Operators: Operators that operates or works with two operands are binary operators.For example: (+ , – , * , /)

 

Relational Operator:  These operators are used to compare the value of two operands. As example: checking if the operand is less than the other operand or not, checking if the operand is greater than the other operand or not, checking if the operand is equals to the other operand or not. Some of the relational operators are: (==, > , = , <= ).

Logical Operator:  Logical operators are AND, OR and NOT operators. They are used to combine two or more conditional expressions. The logical operators are, &&, ||, !

'&&' we call it as AND
'||' we call it as OR
'!' we call it as NOT

for example, we consider below:

// C program to demonstrate working of logical operators
#include <stdio.h>
 
int main()
{
    int a=10, b=4, c = 10, d = 20;
 
    // logical operators
 
    // logical AND example
    if (a>b && c==d)
        printf("a is greater than b AND c is equal to d\n");
    else printf("AND condition not satisfied\n");
 
    // logical AND example
    if (a>b || c==d)
        printf("a is greater than b OR c is equal to d\n");
    else printf("Neither a is greater than b nor c is equal to d\n");
 
    // logical NOT example
    if (!a)
        printf("a is zero\n");
    else printf("a is not zero");
 
    return 0;
}

Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).

Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.

Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.

Assignment Operator:  Assignment operators are used to assign value to a variable. This operator treats the left sided operand as variable and the right side operands as the value. So, assignment operator assigns value from right to the left. For example:

a= 12;

This expression means, assign 12 to the left side operand denoted as ‘a‘.

b = a;

This line assigns the value of ‘a’ to the variable ‘b‘ So, the value of b will be 12 now.

That’s all for today friends. If you found this post important then you can share it to your friends. To share it with your friends in social media or to email to your dearest one you can use the social media links drawn on the top of this post.

Thank you for your time.

Leave a Reply

Your email address will not be published. Required fields are marked *