Ad Code

Responsive Advertisement

OPERATORS IN C-LANGUAGE

 

All essential operators of  C


OPERATORS IN C-LANGUAGE

Definition:

Operators are the symbols that are used to perform different operations on data provided by the user to the machine.

C language provides different types of operators which are given below:

1.    Arithmetic operators

2.    Relational operators

3.    Logical operators

4.    Assignment operators

5.    Increment & Decrement operators

6.    Compound Assignment operators

Before starting the explanation of the above operators we have to know about the concept of EXPRESSION. Let us know about it.

 EXPRESSION:

The expression is a statement that evaluates a value.

It gives a single value. It consists of operands and operators. Operators are the symbols that perform specific functions while Operands are the values on which operations perform.

Example:

              A+B;

           Here A and B are operands while + is the operator.

So it is clear that operator may be constant, variables or combination of both while operands are +, -, *, /, %.

 

1-Arithmetic operators:

     Arithmetic operators are symbols that perform different mathematical operations on data.

C language provides the facility of Arithmetic operations. All the basics operations we perform in CALCULATOR are Arithmetic operations .e.g.    +, -, *, /, %.

 

Addition(+):

It Adds two or more numbers. In C it is placed between two or more variables.

Example:       

#include<stdio.h>

main()

{

Int a,b,c;

Printf(“Enter value of  a:”);

Scanf(“%d”,&a);

Printf(“Enter value of  b:”);

Scanf(“%d”,&b);

c=a+b;

printf(“their sum is:%d”,c);

}

     

Subtraction(-):

It subtract two or more numbers. Example:

 

#include<stdio.h>

main()

{

Int a,b,c;

Printf(“Enter value of  a:”);

Scanf(“%d”,&a);

Printf(“Enter value of  b:”);

Scanf(“%d”,&b);

c=a-b;

printf(“their sum is:%d”,c);

}

Multiplication(*):

It multiplies two or more numbers. Eample:

 

#include<stdio.h>

main()

{

Int a,b,c;

Printf(“Enter value of  a:”);

Scanf(“%d”,&a);

Printf(“Enter value of  b:”);

Scanf(“%d”,&b);

c=a*b;

printf(“their sum is:%d”,c);

}

 

Division(/):

It divides two or more numbers. Example:

 

#include<stdio.h>

main()

{

Int a,b,c;

Printf(“Enter value of  a:”);

Scanf(“%d”,&a);

Printf(“Enter value of  b:”);

Scanf(“%d”,&b);

c=a/b;

printf(“their sum is:%d”,c);

}

 

Modulus(%):

 

Its sign is similar to a percentage but actually, it is not it is called MODULUS.

Many students confuse with this operator but here is the solution to the problem. It is also called the remainder operator.

It works only with integer values.

 

Example:

Ø 2%2==0

              On division 2/2 answer will 1 but the remainder will zero(0) that is our requirement.

Here modulus of different numbers given:

 

Ø 0%2==0

Ø 2%0==2

Ø 2%5==2

Example:

 #include<stdio.h>

main()

{

Int a,b,c;

Printf(“Enter value of  a:”);

Scanf(“%d”,&a);

Printf(“Enter value of  b:”);

Scanf(“%d”,&b);

c=a%b;

printf(“their sum is:%d”,c);

}

2-Rational operators:

Rational operators are used to specifying the condition in the program

It compares two values either one is larger, smaller, or equal to the other. That’s why these operators are called comparison

Operators. Also they test the condition either true or false.

 

Rational operators Include:

Rational operators include the following symbols:

·       >(Greater than)

·       <(smaller than)

·       <=(smaller or equal than)

·       >=(Greater equal than)

·       ==(Equal-only for true and false-)

·       !=(Not equal to)

Rational Expressions:

Rational expressions are A==B, A>B, A<B, A!=B, A,=B and so on

Rational Expressions in Programs :

These operators are mostly used in Decision construction(if, if-else, if-else-if, switch). Some examples are given below:

Program#1: (Writing a program that gets two numbers from the user and displays the maximum number using Rational operators)

#include<stdio.h>

#include<conio.h>

Main()

{

  Int a,b;

  Printf(“Enter value of a:”);

Scanf(“%d”,&a);

Printf(“Enter value of b:”);

Scanf(“%d”,&b);

If(a>b)

Printf(“a is Greater than b”);

 

}

 

Program#2: (Writing a program which get two numbers from user and displays the maximium number using Rational operators)

#include<stdio.h>

#include<conio.h>

Main()

{

  Int a,b;

  Printf(“Enter value of a:”);

Scanf(“%d”,&a);

Printf(“Enter value of b:”);

Scanf(“%d”,&b);

If(a>b)

Printf(“a is Greater than b”);

Else

Printf(“b is Greater than a”);

}

 

Program#3: (Writing a program that gets three numbers from the user and displays the maximum number using Rational operators)

 

#include<stdio.h>

#include<conio.h>

Main()

{

  Int a,b,c,Max;

Printf(“Enter value of a:”);

Scanf(“%d”,&a);

Printf(“Enter value of b:”);

Scanf(“%d”,&b);

Printf(“Enter value of c:”);

Scanf(“%d”,&c);

Max=a;

If(Max<b)

Max=b;

If(Max<c)

Max=c;

Printf(“Maximium is %d”,Max);

 

 

}

 

3-Logical operators:

Logical operators are used to evaluating the compound conditions.

Compound condition:

A type of condition in which more than one conditions are Evaluated.

Example:

              Program:

    #include<stdio.h>

Main()

{

   Int a,b;

Printf(“Enter value of a”);

Scanf(“%d”,&a);

   If(a>100)

Printf(“OK”);

Else

Printf(“Invalid”);

}

(This is an example of a compound condition)

Types of Logical operators:

Ø AND operators(&&)

Ø OR operators(||)

Ø NOR operators(!)

AND operators(&&):

It is used to evaluate two conditions. It produces a TRUE result if both conditions are TRUE if anyone condition FALES it produce result FALSE.

 

Example:

#include<stdio.h>

#include<conio.h>

Main()

{

  Int a,b;

Printf(“Enter value of a:”);

Scanf(“%d”,&a);

Printf(“Enter value of b:”);

Scanf(“%d”,&b);

If(a>50 && b>50)

Printf(“Ok”);

Else()

Printf(“INvalid”);

}

OR operators(| |):

It also used to evaluate the two conditions. It produces TRUE result if BOTH or any ONE condition will TRUE.

Example:

#include<stdio.h>

#include<conio.h>

Main()

{

  Int a,b;

Printf(“Enter value of a:”);

Scanf(“%d”,&a);

Printf(“Enter value of b:”);

Scanf(“%d”,&b);

If(a>50 || b>50)

Printf(“Ok”);

Else()

Printf(“INvalid”);

}

NOR operators(!):

It is used to REVERSE the result of the condition. It gives a TRUE result if the condition is FALSE and vise versa.

Example:

Program(write a program that reads the current state of the telephone line. User should enter Z for working state and X for the dead state. Any other input should be invalid.)

#include<stdio.h>

Main()

{

Char a;

Do

  {

Printf(“Enter the state of phone[Z for working and X for Dead]”);

Scanf(“%c”,&a);

  }

While(!=‘Z’ && !=’X’);

}

 

4-Assignment operator:

This operator is used to assigning a value to a variable.

Its symbol is “=” and it is placed between the LVALUE and RVALUE.

LVALUE:

It is an operand that can be written on the left side of the assignment operator. It must always be a single value.

RVALUE:

It is an operand that can be written on the Right side of the assignment operator.

All Lvalues can be used as Rvalues but all Rvalues can't be use as Lvalue

Example:

Ø A=x+1;

Ø B=a+c;

A and B are lvalues while x+1 and a+c are rvalues.

 

5-Compound Assignment operators:

A statement that assigns a value to many variables is known as compound statement.  For example;

Ø A=B=C=D=10

C language provides Compound assignment operators that combine assignment operators with arithmetic operators.

Syntax of Compound assignment operators is:

Ø Variable op=expression;  

Example:

Ø A+=10; It is equal to A=A+10;

Ø A-=10; It is equal to A=A-10;

Ø A*=10; It is equal to A=A*10;

Ø A/=10; It is equal to A=A/10;

Ø A%=10; It is equal to A=A%10;

 

6-Increment and Decrement operators:

We’ll explain the concept of Increment operators and decrement operators.

Increment operator:

It is used to increase the value of the variable by 1. It is denoted by ‘++’(it is unary operator and works with a single value).

It can be written in two ways:

A++: It is a postfix form.

++A: It is prefix form.

Decrement operator:

It is used to decrease the value of the variable by 1. It is denoted by ‘--’(it is a unary operator and works with a single value).

It can be written in two ways:

A--: It is postfix form.

--A: It is prefix form.

 

Difference Between Prefix and Postfix Increment and Decrement:

When Increment and Decrement operators have used independently then prefix and postfix forms work similarly. For example;

{

A=1;

A++;   //Here value of A is 2

++A;  // Here value of A is 2

B=2;

B--;   // Here value of B is 1

 

--B;  // Here value of B is 1

 

}  

But when Increment operators and decrement operators are used in a large expression with other operators, prefix, and postfix work differently.

Example:

A statement A=++B works in the following ways:

Ø It increments the value of B by 1 (++B)

Ø It assigns the value of B to A (A=B)

A statement A=B++ works in the following ways:

Ø It assigns the value of B to A (A=B)

Ø It increments the value of B by 1 (B++)

Same process with the decrement operators.

 

 

 

Thanks for visiting

******************************************************************

Reactions

Post a Comment

1 Comments