Operators and Operands in C Language

Operators is a symbol that is used to perform certain mathematical or logical operations on data and variables.

Operators and Operands in C Language
Operators and Operands in C Language

Operators 

Operators is a symbol that is used to perform certain mathematical or logical operations on data and variables.  
They  usually from a part of mathematical or logical expressions. Such as =, +, -, * ,  /,  <, >, & etc. 

In C language there are many types of operators:
  1. Arithmetic operators 
  2. Relational operators 
  3. Logical operators
  4. Assignment operators 
  5. Increment and decrement operators 
  6. Conditional operators 
  7. Bitwise operators
  8. Other operators 

Arithmetic Operators

In C language provide, all the basic arithmetic Operators, such as +, -, *,  /, %. 

Example : a+b, a-b ,b/a, a*b, b%a, etc.

Operator and it's Meaning 

  1. +       Addition 
  2. -        Subtraction 
  3. *       Multiplication 
  4. /        Division
  5. %      Modulus ( for find remainder ).
Program:      
#include<stdio.h>
int main()
{
    int a=2,b=3,sum;
    sum = a+b;   
    //sum = a-b;
    //sum = a*b;
    printf("%d",sum);
return 0;
}      
Output: 5

Relational Operators 

In c language , we use for compare two elements. Such as < , > , <= , >= , == , !=.

Example: a<b, b>c, a==0, etc.

Operator and it's meaning 

  1. <       Less than
  2. >       Greater than
  3. <=     Less than or equal to
  4. >=     Greater than or equal to
  5. ==     Is equal to
  6. !=      Not equal to 
Program:   
#include<stdio.h>
int main()
{
    int a=2,b=3,sum;
    if(a<b)
    printf("b is greater: %d",b);
return 0;
}
Output : b is greater: 3

Logical operators 

In C language, There three types of logical Operators.

Such as  && , || , !.

Operators and it's meaning 

  1. &&      AND
  2. ||          OR
  3. !           NOT
Program:             
#include<stdio.h>
int main()
{
    int a=2,b=3,c=1,sum;
    if(a<b&&b>c)
    printf("b is greater: %d",b);
return 0;
}
Output: b is greater: 3

Assignment operator

In C language, Assignment operators used for assign the result, Such as  =.

Assignment operator also used to shorthand operators.

Simple Assignment          shorthand Assignment                      operators                                  operators
  1. a = a+1                                  a+=1 
  2. a = a-1                                   a-=1
  3. a = a*(n+1)                            a*=n+1  
  4. a = a/(n+1)                             a/=n+1 
  5. a = a%b                                  a%=b
Program:                
#include<stdio.h>
int main()
{
    int a=2, b=3;
    a+=1;
    b-=1;
    printf("a :%d\nb :%d",a,b);
return 0;
}
Output: a : 3
               b :2

Increment And Decrement Operators 

In C language, two very useful operators, the operator ++ use for add 1 to the operand and -- use for subtract 1.

 Such as.        ++ a;      or      a++;
                       -- a;      or       a--;
Program:       
#include<stdio.h>
int main()
    int a=2, b=3 ,c=4,d=5;
    a++;
    ++c;
    b--;
    --d;
    printf("a:%d\nc:%d\nb:%d\nd:%d",a,c,b,d);
return 0;
}
Output:  a:3
                c:5
                b:2
                d:4

Conditional operators

In C language, A ternary operator pair " ? :  " is available , to construct expression of the form.

Example:-  exp1 ? exp2 : exp3.

Program:
#include<stdio.h>
int main()
{
    int a=2, b=3,x;
    x=(a>b) ? a:b;
    printf("%d",x);
return 0;
}
Output: 3

Simple program:

#include<stdio.h>
int main()
{
    int a=2, b=3,x;
    if(a>b)
    x=a;
    else
    x=b;
    printf("%d",x);
return 0;
}
Output:3

Bitwise Operators 

In C language, A distinction of supporting special operator known as bitwise operators for manipulation of data at bit level.

Such as &, | , ^ ,<<, >>.

Operators and it's meaning 

  1. &                  Bitwise AND
  2. |                    Bitwise OR
  3. ^                   Bitwise  XOR
  4. <<                Shift left
  5. >>                Shift right 
  6. ~                   Compliment

Program

#include<stdio.h>
int main()
{
    int a=2, b=3,y,x;
     x = (~a);
     y = (a&b); 
    printf("%d %d",x,y);
    return 0;
}

Output: -3 2

Others operator 

In C language, supports some special operators such as comma operator, size of operator and pointer operator.

Example  "," , " ."   " -> " , "#"  ,"##", "*".

What is operand?

An operand is a value , which an operator acts in a programming or mathematical expression. Operands are the components that the operator processes. They can be variables, constants, or expressions.

Example:

In the statement a = 3*b, the operands are 3 and b for the * operator.

Types of Operands:

  1. Constants
  2. Variables 
  3. Expressions

Post a Comment