Monday, November 2, 2020

Operators in C

An operator is a symbol which helps the user to command the computer to do a certain mathematical or logical manipulations. Operators are used in C language program to operate on data and variables. C
has a rich set of operators which can be classified as

1. Arithmetic operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Increments and Decrement Operators
6. Bitwise Operators 
7. Special Operators 

1. Arithmetic Operators

All the basic arithmetic operations can be carried out in C. All the operators have almost the same meaning as in other languages. Both unary and binary operations are available in C language. Unary
operations operate on a singe operand, therefore the number 5 when operated byunary – will have the value –5.

Arithmetic Operators



Operator



Meaning


+


Addition or Unary Plus



Subtraction or Unary Minus


*


Multiplication


/


Division


%


Modulus Operator



 Examples of arithmetic operators are 

x + y

x - y

-x + y

a * b + c

-a * b

here a, b, c, x, y are known as operands. The modulus operator is a special operator in C language which evaluates the remainder of the operands after division. 

Example

 


#include //include header file stdio.h

void main() //tell the compiler the start of the program

{

int num1, num2, sum, sub, mul, div, mod; //declaration of variables

scanf (“%d %d”, &num1, &num2); //inputs the operands

sum = num1+num2; //addition of numbers and storing in sum.

printf(“\n Thu sum is = %d”, sum); //display the output

sub = num1-num2; //subtraction of numbers and storing in sub.

printf(“\n Thu difference is = %d”, sub); //display the output

mul = num1*num2; //multiplication of numbers and storing in mul.

printf(“\n Thu product is = %d”, mul); //display the output

div = num1/num2; //division of numbers and storing in div.

printf(“\n Thu division is = %d”, div); //display the output

mod = num1%num2; //modulus of numbers and storing in mod.

printf(“\n Thu modulus is = %d”, mod); //display the output

}

2. Relational Operators

Often it is required to compare the relationship between operands and bring out a decision and program accordingly. This is when the relational operator come into picture. C supports the following relational
operators.

 


Operator


Meaning


<


is less than


<=


is less than or equal to


>


is greater than


>=


is greater than or equal to


==


is equal to

It is required to compare the marks of 2 students, salary of 2 persons, we can compare them using relational operators.

A simple relational expression contains only one relational operator and takes the following form.

exp1 relational operator exp2

Where exp1 and exp2 are expressions, which may be simple constants, variables or combination of
them. Given below is a list of examples of relational expressions and evaluated values.

6.5 <= 25     TRUE

-65 > 0         FALSE

10 < 7 + 5     TRUE 

Relational expressions are used in decision making statements of C language such as if, while and for statements to decide the course of action of a running program. 

3. Logical Operators

C has the following logical operators, they compare or evaluate logical and relational expressions.

 


Operator


Meaning


&&


Logical AND


||


Logical OR


!


Logical NOT

 

Logical AND (&&)

This operator is used to evaluate 2 conditions or expressions with relational operators simultaneously. If both the expressions to the left and to the right of the logical operator is true then the whole compound expression is true.

Example

 a > b && x = = 10 

The expression to the left is a > b and that on the right is x == 10 the whole expression is true only if both expressions are true i.e., if a is greater than b and x is equal to 10. 

Logical OR (||)

The logical OR is used to combine 2 expressions or the condition evaluates to true if any one of the 2 expressions is true.

Example

 a < m || a < n 

The expression evaluates to true if any one of them is true or if both of them are true. It evaluates to true if a is less than either m or n and when a is less than both m and n.

 Logical NOT (!)

The logical not operator takes single expression and evaluates to true if the expression is false and evaluates to false if the expression is true. In other words it just reverses the value of the expression.

For example

 ! (x >= y) the NOT expression evaluates to true only if the value of x is neither greater than or equal to y.

4. Assignment Operators

The Assignment Operator evaluates an expression on the right of the expression and substitutes it to the value or variable on the left of the expression.

Example

 x = a + b

Here the value of a + b is evaluated and substituted to the variable x.

In addition, C has a set of shorthand assignment operators of the form. 

var oper = exp;

Here var is a variable, exp is an expression and oper is a C binary arithmetic operator. The operator oper
= is known as shorthand assignment operator

Example

 x + = 1 is same as x = x + 1

 The commonly used shorthand assignment operators are as follows 

Shorthand assignment operators   


Statement with simple assignment operator


Statement with shorthand operator


a = a + 1


a += 1


a = a – 1


a -= 1


a = a * (n+1)


a *= (n+1)


a = a / (n+1)


a /= (n+1)


a = a % b


a %= b


5. Increments and Decrement Operators

programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1.

6. Bitwise Operators 

During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.



Operators



Meaning of operators



&



Bitwise AND



|



Bitwise OR



^



Bitwise exclusive OR



~



Bitwise complement



<< 



Shift left



>> 



Shift right



7. Special Operators 



Operators



Description



&



This is used to get the address of the variable.

Example: &a will give address of a.



*



This is used as pointer to a variable.

Example: * a  where, * is pointer to the variable a.


Sizeof()


This gives the size of the variable.

Example: size of (char) will give us 1.



0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top