## Operators in C

Operators are symbols which are use to perform operations on data, operations like arithmetic, assignment, unary, etc.

Operators helps us to manipulate, comparison, Calculate data in program. This help us to make decision and control the flow of program.

Following are the types of operators in C program.

- Unary Operators
- Arithmetic Operators
- Relational Operators
- Logical Operator
- Ternary Operator
- Assignment Operator
- Bitwise Operator

## Unary Operators

As the name suggests **unary operator** works on one operand, in simple terms unary operator works with only one variable.

**Minus “-“**

When we have to change sign of value we can use minus operator. It can change sign from positive to negative or vice versa.

__Syntax :__

**-operand**

__Example :__

** int a = 8, b;**

** b = -a;**

** printf(“b = %d”,b);**

__Output :__

** **b = **-8**

**Address “&”**

Address operator is use to get memory location of a variable. Variable which store another variables’s address is known as pointer.

__Syntax :__

**&operand**

__Example :__

** int a = 8, *b;**

** b = &a;**

** printf(“b = %d”,b);**

__Output :__

** **b = **102031 //address of a**

**NOT “!”**

NOT operator will return 0(false) when condition is true or value of variable is non-zero else it will return 1(true)

__Syntax :__

!condition OR !variable

__Example :__

int a = 8, b=3, c;

c = !(a < b);

printf(“%d”,c);

__Output :__

1

**sizeof “sizeof()”**

It will return the size of variable. sizeof() operator will not execute any operations mention in its parenthesis, it will return size.

__Syntax :__

**sizeof(operand)**

__Example :__

** int a = 8, b;**

** b = sizeof(a);**

** printf(“b = %d”,b);**

__Output :__

** **b = 2

**Increment “++”**

Increment operator will increment value by 1, in simple words it will add 1 to the value. There are 2 types of increment operator.

- Prefix Operator(Pre-Increment)
- Postfix Operator(Post-Increment)

**Pre-Increment**

Prefix operator will increment value before variable is used.

__Syntax :__

**++operand**

__Example :__

** int a = 8, b;**

** b = ++a;**

** printf(“b = %d”,b);**

__Output :__

** b = 9**

**Post-Increment**

Postfix operator will increment value after variable is used.

__Syntax :__

**operand++**

__Example :__

** int a = 8, b, c;**

** b = a++;**

**c = a;**

** printf(“b = %d”,b);**

** printf(” c = %d”,c);**

__Output :__

** b = 8 c = 9**

**Decrement “——“**

Decrement operator will decrement value by 1, in simple words it will subtract 1 from the value. There are 2 types of decrement operator.

- Prefix Operator(Pre-Decrement )
- Postfix Operator(Post-Decrement )

**Pre-Decrement**

Prefix operator will decrement value before variable is used.

__Syntax :__

**——operand**

__Example :__

** int a = 8, b;**

** b = ——a;**

** printf(“b = %d”,b);**

__Output :__

** b = 7**

**Post-Decrement**

Postfix operator will decrement value after variable is used.

__Syntax :__

**operand——**

__Example :__

** int a = 8, b, c;**

** b = a——;**

**c = a;**

** printf(“b = %d”,b);**

** printf(” c = %d”,c);**

__Output :__

** b = 8 c = 7**

## Arithmetic Operators

When we want to perform basic mathematical operations(addition, subtraction, multiplication, division, modulus) on values or variables in program we have to use arithmetic operators.

Arithmetic operators are binary operators, they work with two variables.

**Addition “+”**

As name says addition operator is use to add 2 values, or in simple words it will return sum of two values which we can catch in variable.

__Syntax :__

**operand_1 + operand_2**

__Example :__

** int a = 8, b=3, c;**

** c = a + b;**

** printf(“a + b = %d”,c);**

__Output :__

** a + b = 11**

**Subtraction “-“**

As name says subtraction operation use to perform subtraction between two values, it will return the result which we can catch in variable

__Syntax :__

**operand_1 – operand_2**

__Example :__

** int a = 8, b=3, c;**

** c = a – b;**

** printf(“a – b = %d”,c);**

__Output :__

** a – b = 5**

**Division “/”**

Division operator is use to perform division operation between values. It will return quotient from calculation which we can save in variable.

__Syntax :__

**operand_1 / operand_2**

__Example :__

** int a = 8, b=2, c;**

** c = a / b;**

** printf(“a / b = %d”,c);**

__Output :__

** a / b = 4**

**Multiplication “*”**

Multiplication operator will return product of two values or in simple terms it will multiply both the values and returns it’s results which can be store in variable.

__Syntax :__

**operand_1 * operand_2**

__Example :__

** int a = 8, b=3, c;**

** c = a * b;**

** printf(“a * b = %d”,c);**

__Output :__

** a * b = 24**

**Modulus “%”**

Modulus operator similar as division operator but the difference is it will return remainder instead of quotient which can be stored in variable.

__Syntax :__

**operand_1 % operand_2**

__Example :__

** int a = 8, b=3, c;**

** c = a % b;**

** printf(“a % b = %d”,c);**

__Output :__

** a % b = 2**

## Relational Operators

Relational operators are use to compare values in program. These are use to create condition and also to control flow of program.

Relational operators returns 1(true) if condition is true else it will return 0(false).

**Greater than “>”**

It will return 1(true) if LHS is greater than RHS, else it will return 0(false).

__Syntax :__

**operand_1 > operand**_2

__Example :__

** int a = 8, b=3, c;**

** c = a > b****;**

** printf(“%d”,c);**

__Output :__

** 1**

**Less than “<“**

It will return 1(true) if LHS is less than RHS, else it will return 0(false).

__Syntax :__

operand_1 < operand_2

__Example :__

int a = 8, b=3, c;

c = a < b;

printf(“%d”,c);

__Output :__

0

**Equal to “==”**

It will return 1(true) if LHS is equal to RHS, else it will return 0(false).

__Syntax :__

operand_1 == operand_2

__Example :__

int a = 8, b=3, c;

c = a == b;

printf(“%d”,c);

__Output :__

0

**Greater than equal to “>=”**

It will return 1(true) if LHS is greater than equal to RHS, else it will return 0(false).

__Syntax :__

**operand_1 >= operand**_2

__Example :__

** int a = 8, b=3, c;**

** c = a >= b****;**

** printf(“%d”,c);**

__Output :__

** 1**

**Less than equal to “<=”**

It will return 1(true) if LHS is less than equal to RHS, else it will return 0(false).

__Syntax :__

operand_1 <= operand_2

__Example :__

int a = 8, b=3, c;

c = a <= b;

printf(“%d”,c);

__Output :__

0

**Not equal to “!=”**

It will return 1(true) if LHS is not equal to RHS, else it will return 0(false).

__Syntax :__

operand_1 != operand_2

__Example :__

int a = 8, b=3, c;

c = a != b;

printf(“%d”,c);

__Output :__

1

## Logical Operators

Logical works with conditions statements, it check if conditions are true of false and accordingly it will return integer 0 of 1. Where 0 represents false and 1 represents true.

It is use to control flow of program.

**Logical OR operator** supports **Short circuiting** which means if first condition is true then it will not check next condition and it will return 1(true).

**Logical AND “&&”**

Logical AND will check both the conditions and returns 1(true) if both the conditions are true.

If even one of the condition is false then it will return 0(false).

__Syntax :__

**condition_1 && **condition_2

__Example :__

** int a = 8, b=3, c;**

** c = a > b && b == 3****;**

** printf(“%d”,c);**

__Output :__

** 1**

**Logical OR “||”**

Logical AND will check both the conditions and returns 1(true) if both the conditions are true and even if one of the condition is true then also it will return 1(true).

If both the conditions are false then it will return 0(false).

__Syntax :__

condition_1 || condition_2

__Example :__

int a = 8, b=3, c;

c = a > b || b == 2;

printf(“%d”,c);

__Output :__

1

**Logical NOT “!”**

Logical operator will return 0(false) when condition is true or value of variable is non-zero.

If condition is false or value of variable is 0 then it will return 1(true)

__Syntax :__

!condition OR !variable

__Example :__

int a = 8, b=3, c; int a = 8, b;

c = !(a < b); OR b = !a;

printf(“%d”,c); printf(“%d”,b);

__Output :__ __Output :__

1 0

## Try fun practice game on operators

## CHECKOUT OTHER RELATED TOPICS

## PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.

Programs on Operators and Expressions.

Programs on decision statements.

Programs on looping.

Programs to understand the basic data types and I/O.

Programs on arrays.

Programs on functions.

Programs on structures and unions.

Programs on pointers.

Programs on string manipulations.