# Types of Operators in C Language –

C is a general-purpose, structured programming language. It is reliable, easy to use thatâ€™s the reason it becomes one of the popular languages.

**Definition of Operator – **Operator is defined as a function that operates on one or more variables/constants and manipulates the data and generates meaningful results.

It is very important to understand all the operators in a programming language and how the operators work and how we can use them in our programs. For every program, you need to apply one of the operators.

C language provides a wide variety of operators and they are very easy to understand and apply in your programs.

We will be discussing all the operators one by one and will be giving an example of each operator with an example. If you do not know anything, read the article first Introduction to c language. and How to run the first code in C language?.

## Types of operators in C language –

Operator Category | Operator Symbol |

1. Unary Operators | -,++, ! , — |

2. Arithmetic Operators | *, /, %, +, – |

3. Relational Operator | <, <=, >, >= |

4. Assignment Operator | =, +=, -=, *=, %= |

5. Equality Operator | ==,!= |

6. Logical And | && |

7. Logical OR | || |

8. Conditional Operator | ? |

Now let’s understand all the operators with an example one by one.

## 1. Unary Operator

Basically, all the operators work on two operands but the specialty of unary operators is that they work on a single operand. In Unary operators, we have four types of operators in the C language.

### Example of Unary Operator –

**Output – **The value of a is 5 The value of a after the increment is 6

## 2. Arithmetic Operator –

Mainly arithmetic is used to perform basic mathematic calculations. We need two or more operands for applying the arithmetic operators.

All the basic functions like addition, subtraction, multiplication, and division are under the arithmetic operators.

### Example of Arithmetic Operators –

If you did not get the program. Check the program C Program to add two numbers. In the above program, We added two variables using the ‘+’ operator.

**Output – **The addition of a & b is 37

## 3. Relational Operator –

Relational Operator is applied to two or more variables and is used to compare the variables. It compares the variables and returns true/1 if the comparison is true and false/0 if the statement is false.

### Example of Relational Operator –

**Output : **Enter the values of a and b 10 20 The comparison is 1.

If you want to check more programs related to the relational operator click here.

## 4. Assignment Operators –

From the name, you can get the idea. Assignment means assigning a value to something. Assignment Operators are used to assigning value to the variables. Variables can be assigned in many different ways as listed in the table.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
// C++ program to print optimal parenthesization // in matrix chain multiplication. #include<bits/stdc++.h> using namespace std; // Function for printing the optimal // parenthesization of a matrix chain product void printParenthesis(int i, int j, int n, int *bracket, char &name) { // If only one matrix left in current segment if (i == j) { cout << name++; return; } cout << "("; // Recursively put brackets around subexpression // from i to bracket[i][j]. // Note that "*((bracket+i*n)+j)" is similar to // bracket[i][j] printParenthesis(i, *((bracket+i*n)+j), n, bracket, name); // Recursively put brackets around subexpression // from bracket[i][j] + 1 to j. printParenthesis(*((bracket+i*n)+j) + 1, j, n, bracket, name); cout << ")"; } // Matrix Ai has dimension p[i-1] x p[i] for i = 1..n // Please refer below article for details of this // function // https://goo.gl/k6EYKj void matrixChainOrder(int p[], int n) { /* For simplicity of the program, one extra row and one extra column are allocated in m[][]. 0th row and 0th column of m[][] are not used */ int m[n][n]; // bracket[i][j] stores optimal break point in // subexpression from i to j. int bracket[n][n]; /* m[i,j] = Minimum number of scalar multiplications needed to compute the matrix A[i]A[i+1]...A[j] = A[i..j] where dimension of A[i] is p[i-1] x p[i] */ // cost is zero when multiplying one matrix. for (int i=1; i<n; i++) m[i][i] = 0; // L is chain length. for (int L=2; L<n; L++) { for (int i=1; i<n-L+1; i++) { int j = i+L-1; m[i][j] = INT_MAX; for (int k=i; k<=j-1; k++) { // q = cost/scalar multiplications int q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j]; if (q < m[i][j]) { m[i][j] = q; // Each entry bracket[i,j]=k shows // where to split the product arr // i,i+1....j for the minimum cost. bracket[i][j] = k; } } } } // The first matrix is printed as 'A', next as 'B', // and so on char name = 'A'; cout << "Optimal Parenthesization is : "; printParenthesis(1, n-1, n, (int *)bracket, name); cout << "nOptimal Cost is : " << m[1][n-1]; } // Driver code int main() { int arr[] = {40, 20, 30, 10, 30}; int n = sizeof(arr)/sizeof(arr[0]); matrixChainOrder(arr, n); return 0; } |