Thursday, 4 July 2019

Operators in C

Operators in C


Today we will discuss about Operators that performs an operation on one or more operands.
1.Assignment Operator
2.Arithmetic Operator
3.Relational Operator
4.Logical Operator
5.Conditional Operator
6.Sizeof Operator

Assignment Operator: Assignment Operator (=) is used to assign the value to a variable.

Syntax :  variable = value/variable/expression;

For example:  
           a = 5;    //a assign  with a value or constant
           b=a;     //b assign with a value of a
           c=a+5;  //c assign with the value of expression a+5

Arithmetic Operator: The arithmetic operators are all binary operators ,all the operators have two operands

Operators
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulus

Arithmetic Operators includes unary operators also.There are two types of unary operators.

1.Increment operator (++)2.Decrement operator (- -)

Increment Operator increase the value of variable by 1.
Syntax: a++( Post increment operator) or ++a (Pre increment operator)
a++ or ++a is equivalent to a=a+1

Decrement Operator  decrease the value of variable by 1 .
Syntax : a—(Post decrement operator) or –a (Pre decrement operator)
a—or –a  is equivalent to a=a-1

Post and Pre Operator
The post operator is used to increment or decrement the value after using it .
For example:
Here the value of a is increase by 1 after assigning to x.Due to post increment operator.

Unary Operator Program in C



Unary Operator Demo





Here the value of a is increase by 1 before assigning to x. Due to pre increment operator.


PreIncrement Operator in C

PreIncrement Operator Demo in C


Relational Operator : These operators are used to compare two variables .

Relational Operator
Condition
==
X==Y   (x is equal to y)
!=
X!=Y   (x is not equal to y)
< 
X<Y    (x is less than y)
<=
X<=Y  (x is less than or equal to y)
> 
X>Y    (x is greater than y)
>=
X>=Y  (x is greater than or equal to y)

These relational operators are used with C Statements such as if-else,switch,for etc….

We will discuss these statements in details later .

Logical Operator: Logical operators are used to evaluate expressions which may be true or false.

There are 3 types of logical operators
·        
     &&     logical AND that combines two conditions and both conditions should be true.

          Suppose the grade of the student is ‘B’ only if her marks lie within the range 60 to 75.

                               If(marks>=60) &&(marks<=75)

·         ||      logical OR that combines two conditions but either one condition should be true.

        Suppose an employee should be Finance Manager or Sales Manager.

                       If(empdesignation==’Finance’) ||(empdesignation==’Sales’)

·         !         logical NOT that enables to reverse the meaning of the condition.
                         If (x! =0)


Conditional Operator : Conditional Operator are also called ternary operator.It takes 3 operands.It return one value if condition is true and returns second value if condition is false.

Syntax:
?:
Condition? expression1: expression2.

Let’s take an example:


Conditional Operator in C


TernaryOperator Demo in C


Sizeof operator : Sizeof operator is used to compute the size of any variable or datatype.

Syntax : sizeof (type name)

Lets take an example:


Sizeof Operator in C


Sizeof operator program


No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...