## What are Operators in C?

Operators are tokens constituted by predefined symbols that trigger computers to carry out operations. The participants of an operation are called operands. An operand may be either a constant or a variable.

For example, a+b triggers an arithmetic operation in which + (addition) is the operator and a bare operand. Operators in C++ are classified based on various criteria. Based on the number of operands required for the operation, operators are classified into three.

They are unary, binary, and ternary:

### Unary Operators

A unary operator operates on a single operand. Commonly used unary operators are unary+ (positive) and unary- (negative). These are used to represent the sign of a number. If we apply a unary+ operator on a signed number, the existing sign will not change.

If we apply unary-operator on a signed number, the sign of the existing number will be negated. Examples of the use of unary operators are given in Table 6.2. Some other examples of unary operators are increment ( ++) and decrement (–) operators.

### Binary Operators

Binary operators operate on two operands. Arithmetic operators, relational operators, logical operators, etc. are commonly used, binary operators.

### Ternary Operator

The ternary operator operates on three operands. The typical example is the conditional operator (? : ). The operations triggered by the operators mentioned above will be discussed in detail in the coming sections.

Based on the nature of the operation, operators are classified into arithmetic, relational, logical, input/ output, assignment, short-hand, increment/ decrement, etc.

## Different Types of Operators in C

These are the different types of operators in c which discussed below:

### Arithmetic Operators in C++

Arithmetic operators are defined to perform basic arithmetic operations such as addition, subtraction, multiplication, and division. The symbols used for this are +, -, *, and / respectively. C ++ also provides a special operator,% (modulus operator) for getting remainder during division.

All these operators are binary operators. Note that + and – are used as unary operators too. The operands required for these operations are numeric data. The result of these operations will also be numeric. Table 6.3 shows some examples of binary arithmetic operations.

Modulus operator (%)

The modulus operator also called as the mod operator, gives the remainder value during arithmetic division. This operator can only be applied over integer operands. Some examples of modulus operation. Note that the sign of the result is the sign of the first operand. Here in the table, the first operand is x.

### Relational Operators in C

Relational operators are used for comparing numeric data. These are binary operators. The result of any relational operation will be either True or False. In C ++, True is represented by 1, and False is represented by 0.

Read Also  What are Functions of Operating System? 6 Functions

There are six relational operators in C++. They are< (less than),> (greater than),<= (less than or equal to),>= (greater than or equal to),== (equal to) and ! = (not equal to). Note that equality checking requires two equal symbols (==). Some examples for the use of various relational operators.

### Logical Operators in C++

Using relational operators, we can compare values. Examples are 3<5, num!=10, etc. These comparison operations are called relational expressions in C + +. In some cases, two or more comparisons may need to be combined.

In Mathematics we may use expressions like a> b>c. But in C ++ it is not possible. We have to separate this into two, as a> b and b>c and these are to be combined using the logical operator &&, i.e. ( a> b )&&(b>c ). The result of such logical combinations will also be either True or False (i.e. 1 or 0).

The logical operators are:

#### Logical AND (&&) Operator

If two relational expressions El and E2 are combined using logical AND ( & &) operator, the result will be 1 (True) only if both E1 and E2 have values 1 (True). In all other cases the result will be 0 (False). The results of evaluation of && operation for different possible combination of inputs are shown in Table.

Examples:

10>5 & & 15<25 evaluates to 1 (True)

10> 5 & & 100<25 evaluates to 0 (False)

#### Logical OR ( I I ) Operator

If two relational expressions El and E2 are combined using logical OR ( I I) operator, the result will be 0 (False) only if both El and E2 are having value 0 (False). In all other cases the result will be 1 (True). The results of evaluation of I I operation for different possible combination of inputs are shown in Table.

Examples:

10>5 11100<25 evaluates to 1(True)

10> 15 11 100<90 evaluates to 0 (False)

#### Logical NOT operator ( ! )

This operator is used to negate the result of a relational expression. This is a unary operation. The results of the evaluation of! operator for different possible inputs is shown in Table.

Example:

!(100<2) evaluates to 1 (True)

!(100>2) evaluates to O (False)

### Input and Output Operators

Usually input operation requires user’s intervention. In the process of input operation, the data given through the keyboard is stored in a memory location. C + + provides >>operator for this operation. This operator is known as get from or extraction operator. This symbol is constituted by two greater than symbols.

Similarly in output operation, data is transferred from RAM to an output device. Usually, the monitor is the standard output device to get the results directly. The operator < < is used for output operation and is called put to or insertion operator. It is constituted by two less than symbols.

### Assignment Operator in C (=)

When we have to store a value in a memory location, the assignment operator (=) is used. This is a binary operator and hence two operands are required. The first operand should be a variable where the value of the second operand is to be stored. Some examples are shown in the table.

We discussed the usage of the relational operator == in Section 6.6.2. See the difference between these two operators. The = symbol assigns a value to a variable, whereas == symbol compares two values and gives True or False as the result.

### Arithmetic Assignment Operator

A simple arithmetic statement can be expressed in a more condensed form using arithmetic assignment operators. For example, a =a+ 10 can be represented as a+=lO. Here+= is an arithmetic assignment operator. This method is applicable to all arithmetic operators and they are shown in Table.

The arithmetic assignment operators in C++ are+=, -=, *=, /=,%=.These are also known as C++ short-hands. These are all binary operators and the first operand should be a variable. The use of these operators makes the two operations (arithmetic and assignment) faster than the usual method.

### Increment and Decrement Operators

Increment and decrement operators are two special operators in C++. These are unary operators and the operand should be a variable. These operators help keeping the source code compact.

#### Increment Operator (++)

This operator is used for incrementing the content of an integer variable by one. This can be written in two ways: ++x (pre-increment) and x++ (post-increment). Both are equivalent to x=x+ 1 as well as x+= l.

#### Decrement Operator (– )

As a counterpart of increment operator, there is a decrement operator which decrements the content of an integer variable by one. This operator is also used in two ways: –x (pre decrement) and x– (post decrement). These are equivalent to x=x-1 and x-=1.

The two usages of these operators are called prefix form and postfix form of increment and decrement operation. Both the forms make the same effect on the operand variable, but the mode of operation will be different when these are used with other operators.

#### Prefix form of Increment and Decrement Operators

In the prefix form, the operator is placed before the operand and the increment and decrement operation is carried out first. The incremented and decremented value is used for the other operations. So, this method is often called change, then use method.

Consider the variables a, b, c, and d with values a= l0, b= S. If an operation is specified as c=++a, the value of a will be 11 and that of c will also be 11. Here the value of a is incremented by 1 at first and then the changed value of a is assigned to c. That is why both the variables get the same value. Similarly, after the execution of d=–b the value of d and b will be 4.

#### Postfix form of Increment and Decrement Operators

When increment/ decrement operation is performed in postfix form, the operator is placed after the operand. The current value of the variable is used for the remaining operations and after that, the increment/ decrement operation is carried out. So, this method is often called use, then change method.

Consider the same variables used above with the same initial values. After the operation performed with c= a ++, the value of a will be 11, but that of c will be 10. Here the value of a is assigned to cat first and then a is incremented by 1.

That is, before changing the value of it is used to assign to c. Similarly, after the execution of d=b– the value of d will be 5 but that of b will be 4.

### Conditional Operator in C (? : )

This is a ternary operator applied over three operands. The first operand will be a logical expression ( condition) and the remaining two are values. They can be constants, variables or expressions.

The condition will be checked first and if it is true, the second operand will be selected to get the value, otherwise, the third operand will be selected. Its syntax is:

Expression 1? Expression2: Expression3

Let us see the operation in the following:

result = score>50 ? ‘ p ‘ ‘ f ‘

If the value of score is greater than 50 then the value ‘p’ is assigned to the variable result, else value ‘f’ is assigned to result.

### Sizeof Operator in C

The operator sizeof is a unary compile-time operator that returns the amount of memory space in bytes allocated for the operand. The operand can be a constant, a variable or a data type. The syntax followed is given below:

• Sizeof ( data type)
• Sizeof variable name
• Sizeof constant

It is to be noted that when data type is used as the operand for size of operator, it should be given within a pair of parentheses. For the other operands, parentheses are not compulsory. The table shows different forms of usages of sizeof operator.

### Precedence of Operators in C

Let us consider the case where different operators are used with the required operands. We should know in which order the operations will be carried out. C + + gives priority to the operators for execution. During the evaluation, pair of parentheses is given the first priority.

If the expression is not parenthesized, it is evaluated according to the predefined precedence order. The order of precedence for the operators is given in Table. In an expression, if the operators of the same priority level occur, the precedence of execution will be from left to right in most cases.

Consider the variables with values: a= 3, b= 5, c= 4, d=2, x.

After the operations specified in x = a + b * c – d, the value in x will be 21. Here * (multiplication) has higher priority than+ (addition) and – (subtraction). Therefore the variables b and c are multiplied, then that result is added to a. From that result, d is subtracted to get the final result.

It is important to note that the operator priority can be changed in an expression as per the need of the programmer by using parentheses ( ). For example, if a= 5, b= 4, c= 3, d=2 then the result of a+b-c*d will be 3. Suppose the programmer wants to perform subtraction first and then addition and multiplication.

You need to use proper parentheses as (a+ ( b-c) ) * d. Now the output will be 12. For changing operator priority, brackets [ ] and braces { } cannot be used.

## FAQs About Types of Operators in C

### What are the types of operators in c?

The following are the different types of operators in c which discussed below:
1. Arithmetic Operators in C++
2. Relational Operators in C
3. Logical Operators in C++
4. Input and Output Operators
5. Assignment Operator in C (=)
6. Arithmetic Assignment Operator
7. Increment and Decrement Operators
8. Conditional Operator in C (? : )
9. Size of Operator in C
10. Precedence of Operators in C.