# Operators and Expression

OPERATORS

Definition:-It is a symbol which performs particular task.  Ex:- +,-,*,etc….

Operand:- It is an entity which acts on operator.

Unary operators:-  The operators consists only one operand are called unary operators.

Binary operators:-  The operators consists two operands are called binary operators.

C has rich set of operators. They are

1. Arithmetic operators
2. Relational operators.
3. Logical operators.
4. Assignment operators.
5. Bit-wise operators
6. Increment and decrement operators
7. Special operators

(i)   ternary operator (or) Conditional operator

(ii)  comma operator.

(iii) sizeof operator.

Arithmetical Operators:-These are the basic operators in C language. They are useful forperforming arithmetic calculations.

+  addition                                                             /  division

–  subtraction                                                        %  Modulus (remainder)

*  Multiplication

Program:-Write a program to accept any two numbers and test all arithmetic operators

```#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter any Two numbers:");
scanf("%d%d",&a,&b);
printf("\nAddition of %d and %d is %d",a,b,a+b);
printf("\nSubtraction of %d and %d is %d",a,b,a-b);
printf("\nMultipliction of %d and %d is %d",a,b,a*b);
printf("\nDivision of %d and %d is %d",a,b,a/b);
printf("\nModulus of %d and %d is %d",a,b,a%b);
getch();
}
```

Logical operators:-

These operators are used for combine the result of two or more expressions.

•                              &&                        Logical AND
•                              ||                             Logical OR
•                              !                              Logical NOT

If  E1 = True then  !E1= false                                                       If  E1 = False then !E1 True

Relational operators:-

These are used to test the relation between two values or two variablesor expressions.All c relational operators are binary operators and hence requires two operands.

<             less than

>              greater than

<=          less than or equal to

>=          greater than or equal to

= =          equal to

!=            not equal to

Assignment operators:-

These are used to assign a value of an expression to an identifier.

=          Assignment operator.

Compound assignment operators (or) Short hand assignment operators:-+=, -=, *=, /=, %=

Example:-int a=10;

a=a+20; (or) a+=20;

Increment and Decrement operators:

These are used to control the loops in an effective method.

Increment operators:- The symbol ‘++’  is used for incrementing by 1.

Syntax:-                      ++identifier;                          (prefix increment)

identifier++;                          (postfix increment)

Example:-  int a=10,b;

b=a++                                    here o/p : a=11,b=10

b=++a                                    here o/p : a=11,b=11

Decrement operators:- The symbol ‘–‘  is used for decrementing by 1.

Syntax:-                      –identifier;                            (prefix decrement)

identifier–;                            (postfix decrement)

Example:-  int a=10,b;

b=a–                                     here o/p : a=9,b=10

b=–a                                     here o/p : a=9,b=9

BIT-WISE operators:-

These are used for manipulating data at bit level they are two types:

1.     Bitwise logical operators.

2.     Bitwise shift operators.

Bit wise logical operators:-

These are used for the bitwise logical decision making.

&         bitwise AND

|           bitwise OR

^  bitwise exclusive OR

Program:

`#include<stdio.h>#include<conio.h>void main(){int a,b;clrscr();printf("Enter any two values:");scanf("%d%d",&a,&b);printf("\na&b=%d",a&b);printf("\na|b=%d",a|b);printf("\na^b=%d",a^b);getch();}`

Bitwise shift operators:-

The shift operators take binary patterns and shift the bits to the leftor right.

<< – left shift

>>  – right shift

Program:- To find b, c values using shift operators.

`#include<stdio.h>#include<conio.h>void main(){int a=8,b,c;clrscr();b=a<<4; c=a>>3;printf("\n a=%d", a);printf("\n b=%d", b);printf("\n c=%d", c);getch();}`

SPECIAL OPERATORS:

1. Ternary operator  (or) Condition operator:-

A Ternary operator is represented as “?:” It is used to construct a conditional expression. The general form of ternary operator is exp1?exp2:exp3; here exp1 , exp2 , exp3 are expressions.

If exp1 is true, then exp2 is evaluated and its value becomes the value of expression. If exp1 is false, then the exp3 is evaluated and its value becomes the value of the expression.

Program:- Write a program to find max and min of given two values by using ternary operators

```#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max,min;
clrscr();
printf("Enter values for a,b:");
scanf("%d%d",&a,&b);
max=a>b ? a:b;
min=a<b ? a:b;
printf("Maximum value is :%d",max);
printf("\nMinimum value is :%d",min);
getch();
}

Example:
int a=10,b=20,c,d;
c=a>b?a:b;
a=10, b=20 and c=20,d=10```

b)Comma operator:-

It can be used to link the relative expressions together. The general form of a comma operator is

Syntax: identifier =(identifier1=value,———identifier n=value, expression)

Program:-

```#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
c=(a=10,b=20,a+b);
printf("a=%d",a);
printf("\nb=%d",b);
printf("\nc=%d",c);
getch();
}

Example:
int a,b,c;
c=(a=10,b=20,a+b);
Therefore a=10, b=20 , c=30
(first assigns the value 10 to ‘a’ next assigns the value 20 to b and finally assigns the value 30 to c)```
1. sizeof operator:-

It is a compile name operator and is used to get the memory size of a variable , expression and data type.

Syntax:- sizeof(variable(or)exp(or)datatype);

Program:

```#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int n;
float ft;
double db;
clrscr();
printf("\nSize of char:%d byte",sizeof (ch));
printf("\nSize of int:%d byte",sizeof (n));
printf("\nSize of float:%d byte",sizeof (ft));
printf("\nSize of double:%d byte",sizeof (db));
getch();
}

```

PRECIDIENCE OF OPERATORS