## C Programming Operators

Let's start by knowing the actual meaning of operators and what the operators are?Â

Operators are the symbols that are used to perform some operations. Suppose we are given a statement a+b*c. Here, symbols like +, * are operators and the alphabets like a, b, c are operands.

When operands and operators combine to perform any operations then this is called expression.

Before knowing more about**Â C Programming Operators**, let us first understand some basic fundamentals. You may know that any programs written in any language are made up of using some basic elements.

- Alphabets
- Digits
- Blank spaces
- Special characters

What are the alphabets used in writing a program?

"**A**" to "**Z**" and "**a**" to "**z**".Â Here, you should remember that C is a case-sensitive language that means thatÂ 'Â ** aÂ **'Â andÂ 'Â

**both are different in C language.**

*A'Â Â*

What are the digits used in writing a program?

"**0**"**Â **to "**9**".

What are the blank spaces used in writing a program?

'**\n**' , '**\t**' , '**\r**' , and so on.

What are the special characters used in writing a program?

Here is the list of the special symbols or characters that are used in writing a program.

- Â
**.Â Â Â Â Â**period **Â ,Â Â Â Â Â**comma- Â
**:**Â Â Â Â colon - Â
**;Â Â Â Â Â**semi-colon - Â
**_Â Â Â Â**underscore - Â
**%Â Â Â**percentile - Â
**-Â Â Â Â Â**hyphen or minus - Â
**+Â Â Â Â**plus - Â
***Â Â Â Â**asterisk - Â
**&Â Â Â**ampersand - Â
**!Â Â Â Â**exclamation mark **Â ?Â Â Â Â**question mark- Â
**#Â Â Â Â**number sign **/Â Â Â Â Â**forward slash**\Â Â Â Â Â**backward slash**'Â Â Â Â Â**single**Â**quote- Â
**"Â Â Â Â**double quotes - Â
**~Â Â Â Â**tilde **( )Â Â Â Â**parenthesis or small brackets**{ }Â Â Â Â**curly braces or middle brackets**[ ]Â Â Â Â**big brackets**< >Â Â Â**angular brackets**|Â Â Â Â Â Â**pipe

## What are the keywords used in C language?

C language has some reserved words that cannot be used in naming any variables, functions, etc. They are the special because C compiler does not need any introduction before being applied in a program. They are only 32 words in c language which are keywords in C language and they are:

- Â auto
- Â break
- Â case
- Â char
- Â const
- Â continue
- Â default
- Â do
- Â double
- Â else
- Â num
- Â extern
- Â float
- Â for
- Â goto
- Â if
- Â int
- Â long
- Â register
- Â return
- Â short
- Â signed
- Â sizeof
- Â static
- Â struct
- Â switch
- Â typedef
- Â union
- Â unsigned
- Â void
- Â volatile
- Â while

## What are identifiers in C?

Identifiers are the words that are formed by the programmers to give names to the variables, functions, array, pointers, etc so that it becomes very easier to recognize them in the future by its unique name. However, there are some specific rules which are to be known in our mind before creating any identifiers.

### Rules to create Identifiers in C

- No keywords can be used as an identifier because they are special and already known to the compiler.
- The maximum length of the identifiers should not exceed 32 characters and almost length of identifier is 32.
- Only the following character sets can be used for creating identifiers. '
**A**'**Â**to '**Z**', '**a**'**Â**to '**z**', '**0**'**Â**to '**9**', and 'Â**_**'**Â**(underscore). - Â The starting character of an identifier can either be an alphabet or underscore.
- Â No two identifiers can be similar or identical in a single block.

Note that C is a case sensitive language where upper and lower case characters are important and pretend to be different from each other. for example, we can take Sum, sum, sUm, suM,... etc as identifiers. All of them represent different identifiers.

## What are Operators in C programming?

Operators are the symbol used to perform some operations. In C, they are defined in two ways.

- On the basis of operands they required.
- On the basis of the type of operation they perform.

Operators On the basis of operands required:

- Unary operators
- Binary operators
- Ternary operators

### Unary Operators in C

- Â
**++**increment - Â Â
**--Â Â**decrement - Â
**+**unary plus - Â
**-Â**unary minus - Â
**!Â**Â not

Here, let me tell you that what is mean by unary plus and minus. Consider a statementÂ -4, +5, the sign that is used before a number as a prefix only, then it is called unary plus or minus.

Let say -5-7: Here, the minus sign before 5 is called unary minus but the minus sign in between 5 and 7 is called subtraction.

### Binary Operators in C

They requires two operands to complete the operation. for example

- Â
**+**plus or addition - Â
**-**minus or subtraction - Â
*****multiplication - Â
**/**division - Â
**%**modulus - Â
**<**Â lesser than - Â
**>**greater than - Â
**=**is equal to or equals to, and so on.

### Ternary Operators in C

They need three operands to complete its operations. for exampleÂ Â ''Â **?Â : **"Â is a ternary operator and is followed in the conditional statements.Â C programming operators on the base of the type of operation they show:

- Arithmetic operators or Mathematical operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and Decrement operators
- Bitwise operators
- Size of operators [sizeof ()]
- Comma (
**,**) operators

### Arithmetic or Mathematical Operators in C

**+Â**Addition**-Â Â**Subtraction***Â Â**Multiplication**/Â Â**Division**%**Modulus

These are the arithmetic or mathematical operators. They are also binary operators because they require exactly two operands to do an operation.

### For Example:

```
#include<stdio.h>
#include<conio.h>
void main()
{
printf("%d \n", 11+8);
printf("%d \n", 11-8);
printf("%d \n", 11*8);
printf("%d \n", 11/8);
printf("%d \n", 11%8);
}
```

**Output**

```
19
3
88
1
3
```

**Explanation:**

1. When we add 11 and 8, we get 19.

2. When we subtract 8 from 11, we get 3.

3. When we multiply 8and 11, we get 88.

4. When we divide 11 and 8, we get 1 as quotient and 3 as remainder.

5. The symbol 'Â **/ **' gives us quotient. So, the answer is 1.

When we divide 11 and 8, we get 1 as quotient and 3 as remainder.

The symbolÂ ' **%Â **' gives us the remainder. So, the answer is 3.

### Relational Operators in C

**<Â Â Â**lesser than**Â****>Â Â Â**greater than**< =Â Â**lesser than equals to**> =Â Â**greater than equals to**= =Â Â**is equal to**! =Â Â Â**is not equal to

These are called relational operators because they all show some relations between the operands. They are also binary operators because they require two operands for performing operations.

### For Example:

```
#include<stdio.h>
#include<conio.h>
void main()
{
printf("%d \n", 10>7);
printf("%d \n", 10==7);
printf("%d \n", 10!=7);
}
```

**Output**

```
1
0
1
```

**Explanation:**

This type of question shows either 1 or 0 as the output on the screen.

- Â 10 is greater than 7. So, the answer is 1.
- Â 10 is not less than 7. So, the answer is 0.
- Â 10 is neither lesser nor equals to 7. So, the answer is 0. If anyone of the condition is true, the answer will be true (1).
- Â 10 is greater than 7 but is not equals to 7. Here, one condition is true So, the answer will be 1 (True).
- Â 10 is not equal to 7. So, the answer will be 0.
- Â 10 is not equal to 7. So, the answer will be 1.

### Logical Operators in C

- Â
**&&Â Â AND**Operator **Â | |Â Â Â Â OR**Operator**Â !Â Â Â Â Â NOT**Operator

They are used to join two or more relations inside any program.Â An expression containing a logical operator returns 0 (0 means False) or 1(1 means True) depending upon the expression results. It is practiced for decision making in C language.

**&& - **Logical AND - It implies True only if all the operands are True.**|| - **Logical OR - It implies True if either of the one operand is True.**! - **Logical NOT - It implies True only if the operand denotes False.

### Assignment Operators in C

They are the type of operators that manipulates the value of the variables. There are two types of assignment operators.Â

- Â Constructive assignment operators
- Â Destructive assignment operators

Destructive assignment operators overwrite the old value of the variable with the new one whereas constructive assignment operators update the old value of the variable with the new one.Â

This can be addition, subtraction, multiplication depends upon the operators that are being used.

Destructive assignment operator is: "Â **= **"

Constructive assignment operators are: **+=** , **-= **, ***=** , **/=** , **%=** ,** >>=** , **<<=**

### For Example:

```
#include<stdio.h>
void main()
{
int i; // It is an example of destructive assignment operator.
i = 0;
i = 2;
i = 11;
printf("It is an example of destructive assignment operator. \n");
printf("%d", i);
}
```

**Output**

```
It is an example of destructive assignment operator.
11
```

#### For Example:

```
#include<stdio.h>
void main()
{
int i = 2; // It is an example of constructive assignment operator.
i += 1;
i = 3;
i *= 2;
printf("An example of constructive assignment operator. \n");
printf("%d", i);
}
```

**Output**

```
An example of constructive assignment operator.
6
```

### Increment / Decrement operator in C

**++** is an increment operator and **--** is a decrement operator. They can be used with variables only. They can be prefixed or postfixed with the variables.

**For Example:**

```
#include<stdio.h>
void main()
{
int i = 4;
i++;
i += 2;
i+4; // i+4 does not do anything.
++i;
i *= i;
printf("%d", i);
}
```

**Output**

`64`

First, we take variable 'i' and initialize it with 4. At the next step, it got incremented by 1 and 'i' become 5.Â Then, the addition of 5 and 2 (that is 7) is updated in 'i' now i becomes 7.

Here, i+4 does mean nothing because no sign of '=' present here. So, it does nothing with i.

Now in the next step, i is again incremented by 1 and becomes 8.

Finally, the multiplication of i*i that is (8*8=64) and 64 is stored into i and 64 is printed by the printf statement as an output on the screen.

### Bitwise operator in C

- Â
**&**Bitwise AND **Â |**Â Â Bitwise OR- Â
**^**Â XOR (Exclusive OR) - Â
**~**1's Complement. - Â
**>>**Right shift - Â
**<<**Left shift

They operate on the binary value of an operand.

### Sizeof operator in C [sizeof ()]

It is used to determine the memory consumption of any variable and data types.

**For Example:**

```
#include<stdio.h>
void main()
{
int a;
printf("%d", sizeof(a)); // prints size of a.
printf("%d", sizeof(int)); // prints size taken by int datatype.
}
```

**Output**

```
4
4
```

### Comma operator in C

It is used as a separator between similar types of declaration. When the programmer wants to declare multiple variables of the same data types. Then, they make use of the comma operator as a separator between the declared variables.

**For Example:**

```
#include<stdio.h>
int main()
{
int a, b, c, e, f;
char m, z, j, g;
float n, k, s;
printf("Comma operator executed successfully");
return 0;
}
```

**Output**

`Comma operator executed successfully`

### Conditional operator in C

It is the only operator in C language which requires three operands to complete its operations.Â

It is a replacement of if-else statement i.e., the programs that are made in multiple lines using an if-else statement can be made in a simple one-line using conditional operator.

It is formed by the combination of two symbols (** ?** and **:**).Â It works exactly like an if-else statement where it tests for some condition first and after its evaluation executes either expression 1 or expression 2 as shown below in the syntax.

#### Syntax of Conditional operator in C

*(True Condition) ? expression1 : expression2;*

#### For Example:

```
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter two values \n");
scanf("%d%d", &a, &b);
c = (a>b) ? a : b;
printf("Greatest number = %d", c);
}
```

**Output**

```
Enter two values
105
10
Greatest number = 105
```

**For Example:**

```
/* programs to find the greatest among three numbers using the
conditional operator or conditional statement */
#include<stdio.h>
void main(
{
int a, b, c, big;
printf("Enter three values \n");
scanf("%d%d%d", &a, &b, &c);
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("Greatest number = %d", big);
}
```

**Output**

```
Enter three values
5
120
13
Greatest number = 120
```

### Operator precedence associativity or rank associativity in C

When there are multiple operators present in an expression then their priority of execution is determined by its precedence also known as rank.Â

According to the rule, the operator with the least rank operates first in the expression but when there is more than one operator of the same rank is present in an expression.

Then their priority of execution is determined by their associativity that can be left to right or right to left.

Following are the lists of operators rank associativity:

Rank: **1**Â

Operator:Â **( )**, **[ ]**, **++** (prefix), **-- **(prefix)Â Â Â Â Â Â

Direction: from **left to right**.

Rank: **2**Operator:

**sizeof( )**,

**+**(unary),

**-**(unary),

**&**(pointer),

*****(pointer)

Direction: from

**right to left.**

Rank: **3**Operator:

*****,Â Â

**/**,Â Â

**%**

Direction: from

**left to right.**

Rank: **4**Operator:

**+**(add),

**-**(subtract)

Direction: from

**left to right.**

Rank: **5**Operator:

**>>**,Â

**<<**

Direction: from

**left to right.**

Rank: **6**Operator:

**>**,

**<**,

**>=**,

**<=**

Direction: from

**left to right.**

Rank: **7**Operator:

**= =**,

**!=**

Direction: from

**left to right.**

Rank: **8**Operator:

**&**

Direction: from

**left to right.**

Rank: **9**Operator:

**^**

Direction: from

**left to right.**

Rank: **10**Operator:

**|**

Direction: from

**left to right.**

Rank: **11**Operator:

**&&**

Direction: from

**left to right.**

Rank: **12**Operator:Â Â

**| |**

Direction: from

**left to right.**

Rank:Â **13**Operator:

**(Condition) ?Â Â :Â**Â

Direction: from

**right to left.**

Rank: **14**Operator:

**=**,

**+=**,

**-=**,

**/=**,

**%=**

Direction: from right to left.

Rank: **15**Operator:

**,**

Direction: from

**left to right.**