March 28, 2024
Operators in c language
TechTechInfo.com » Operators in c language

Operators in c language

Operators in c language
Operators in c languag

An Operator is the predefined symbols in c language. Which tells the compiler to perform the mathematical or logical operations. 

Types of operators in c language

  • Arithmetic Operator
  • Assignment Operator
  • Relational Operator
  • Logical Operator
  • Increment & Decrement Operator
  • Bitwise Operator
  • Ternary Operator
  • Type Cast Operator
  • sizeof Operator
  • C shorthand

Arithmetic Operators:-

Arithmetic Operators are used for Arithmetic Operations for example add, multiply.

1+Add
2Subtract
3*Multiply
4/Divide
5%Modulus

 

Example:

#include<stdio.h>

int main(){

int num1,num2;
Int add,sub,mul,div,mod;

printf(“\nEnter First Number :”);
scanf(“%d”,&num1);

printf(“\nEnter second Number :”);
scanf(“%d”,&num2);

add = num1 + num2;
printf(“sum of entered no’s are %d”,add);

sub = num1 - num2;
printf(“subtraction of entered no’s are %d”,sub);

mul = num1 * num2;
printf(“Multiplications of entered no’s are %d”,mul);

div = num1 / num2;
printf(“Divisions of entered no’s are %d”,div);

mod = num1 % num2;
printf(“Modulas of entered no’s are %d”,mod);

}

Assignment Operator:-

This Operator is used to assign the value to a variable. this is a binary operator that operates on two operands or variables.

1=Assignment Operator

 

Example:

#include<stdio.h>
int main(){
int a;
a = 55;
printf(“value of a = %d”,a);
return (0);
}

Relational Operator:-

This operator is used to compare the value of two variables with different conditions.

1==Check for the value of 2 variables are equal.
2!=Check for the value of 2 variables are equal or not;
3>Check value greater than
4>=Check value is greater than or equal to
5<Check value is less than
6<=Check value is less than or equal to

 

Example:

#include<stdio.h>
int main(){

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

// == operator
if(a == b){
 printf(“a is equal to b\n”);
}else{
 printf(“a is not equal to b\n”);
}

// > operator
if(a > b){
 printf(“a is greater than b\n”);
}else{
 printf(“a is not greater than b\n”);
}

// < operator
if(a < b){
 printf(“a is less than b\n”);
}else{
 printf(“a is not less than b\n”);
}

// != Opertor
if(a != b){
 printf(“a is not equal to b\n”);
}else{
 printf(“a is equal to b\n”);
}

// >= orperator
if(a >= b){
 printf(“a is greater than or equal to b\n”);
}else{
 printf(“a is not greater than or equal to b\n”);
}

// <= operator
if(a <= b){
 printf(“a is less than or equal to b\n”);
}else{
 printf(“a is not less than or equal to b\n”);
}

}

Logical Operator:-

Logical operators are used to perform logical operations on the given two variables.

1&&Both conditions are true
2||Minimum one condition is true
3!not

 

Logical Operator chart with different conditions 

OperatorCondiation1Condition2Result
&&truetruetrue
&&truefalsefalse
&&falsetruefalse
&&falsefalsefalse
||truetruetrue
||truefalsetrue
||falsetruetrue
||falsefalsefalse
!truefalse
!falsetrue

 

Example:

#include<stdio.h>
int main(){
  A = 5;
  B = 10;
if((A == 5) && (B < 11)){
 printf(“Both conditions are ture”);
}

if(!(A==5)){
 printf(“A is not equal to 5”);
}

if((A>=3) ||(B<=9)){
 printf(“min any one condition is true”);
}

}

Increment & Decrement Operator:-

The increment operator (++) has increased the value of its operands by 1

The decrement operator (–) decreases the value of its operands by 1

Example:-

int x;
int y;

x = 1;

y = ++x;      // value of x = 2, y = 2
y = x++;     // value of x = 3, y = 2

x = 3;

y = x--;      //value of x =2,y =3;
y = --x;      //value of x 1, y= 1;

Bitwise Operators:-

Bitwise operators perform manipulations of data at the bit level. These operators are perform shifting of bit form right to left. Bitwise operators are not applied to float or double.

1&Bitwise AND

Bitwise add
Bitwise add
2|Bitwise OR

Bitwise OR
Bitwise OR
3^Bitwise XOR

Bitwise XOR
4<<Left shift

Left shift
Left shift
5>>Right shiftright shift

 

Example:-

int main()

{

    // a = 5(00000101), b = 9(00001001)
    unsigned char a = 5, b = 9;

    // The result is 00000001

    printf("a = %d, b = %d\n", a, b);
    printf("a&b = %d\n", a & b);

    // The result is 00001101
    printf("a|b = %d\n", a | b);

    // The result is 00001100
    printf("a^b = %d\n", a ^ b);

    // The result is 11111010
    printf("~a = %d\n", a = ~a);

    // The result is 00010010
    printf("b<<1 = %d\n", b << 1);

    // The result is 00000100
    printf("b>>1 = %d\n", b >> 1);

    return 0;

}

Ternary Operator:-

Ternary is a shortcut of if-else condition. It accepts 3 inputs to execute.

Syntax:

(Condition) ? expression1: expression2;

If the condition is true then expression 1 will be executed and else expression 2 will execute.

Example:-

int a=5;
int b=6;
int c;

C = (a>b) ? “1”: “0”; // value of C will be 1 because the condition is true

Typecast Operator:-

The typecast operator is used to change the data type of a variable to another data type

Two types of Cast 

  • Implicit Cast
  • Explicit cast

Implicit Cast –

Implicit type conversion also known as coercion is an automatic type conversion by the compiler

Explicit Cast – 

This typecast is defined by a developer explicitly in a program

Note:-

  1. Lower datatype to a higher data type is converted implicitly 
  2. Higher Datatype to lower datatype is converted explicitly
int i =10;                             //integer value
int f =3.147;                      //float value
//implicit conversion
f = i;
//explicit conversion
i = (int)f;

Size of Operator:-

sizeof operator is used to computing the size of any object

Syntax:-

sizeof(obj name);

Example:

printf(“size of int in byte : %d “,sizeof(int));

C Shorthand:-

C has a special shorthand that simplifies the coding of a certain type of assignment.

S.noOperatorsExampleMeaning
1+=a+=2a=a+2
2-=a-=2a=a-2
3*=a*=2a=a*2
4/=a/=2a=a/2
5%=a%=2a=a%2
6&&=a&&=2a=a&&2
7||=a||=2a=a||2

 

Join the discussion

1 comment
Translate »