What are Operators in C?

Operators in C are the different types of Symbols which tell the compiler to perform mathematical or logical operations.

C is very rich in built-in operators. In fact, it places more significance on operators than do most other computer languages.

An operator specifies an operation to be performed that yields a value. The data items that operators act upon are called operands.

The variables. constants can be joined by various operators to form in expression. Expressions are evaluated using the precedence of operators.

Types of Operators in C:

operators in c

Some operators require two operands, while others act upon only one operand. Classification of operators can be done in two ways based on the number of operands and their nature.

Based on Number of Operands:

Unary Operators: It can be applied on only one operand. For example, unary operators are ++, –, !, sizeof(), (typecast).

Binary Operators: It can be applied to two operands. For Example +, *, -, /, <, >, &&, || etc.

Ternary Operators: It can be applied to three operands. For example, Conditional operator(? :).

Mainly there are seven categories of Operators in C they are as follows:

1. Arithmetic Operators.
2. Relational Operators.
3. Logical Operators.
4. Increment and Decrement Operators.
5. Bitwise Operators.
6. Assignment Operators.
7. Conditional Operators.

Now let us see different Operators in C one by one with a detailed explanation.

1. Arithmetic Operators

Arithmetic operators are binary operators which act on two operands. The operands can be integer quantities, floating-point quantities or characters.

OperatorMeaning of Operator
+Addition.
Subtraction.
*Multiplication.
/Division.
%Modulo Division.

When both operands are of same type data then result is of the same type. But when operands are of different data types, then the result is promoted to the highest data type.

Promotion of data type is as follows:
char < short int < int < long int < float < double.

The remainder operator (%) requires that both operands be integers and the second operand be nonzero.

Similarly, the division operator (/) requires that the second operand be nonzero. Division of One integer quantity by another is referred to as integer division. This operation always results in a truncated quotient (i.e., the decimal portion of the quotient will be dropped).

On the other hand, if a division operation is carried out with two floating-point numbers, or with one floating-point number and one integer, the result will be a floating-point quotient.

If one or both operands represent negative values, then the addition, subtraction, multiplication and division operations will result in values whose signs are determined by the usual rules of the algebra.

Examples: 25+2=27, 26-4=22, 6*6=36, 25/5=5, 5%5=0.

Unary (+) and Unary (-) Operators:

When + and – operators are used with one operand, they are known as unary plus (+a) and unary (-a) operator.

+ indicates a positive value (It is optional) and – indicates a negative value.

2. Relational Operators

Relational operators are symbols that are used to test the relationship between two variables or between a variable and a constant.

These are binary operators. The result of the relational expression is true or false. In C language ‘false’, means zero and non zero value is considered as ‘true’ value. So, O means false and -5 means true value.

C language has six relational operators as follows:

OperatorsMeaning of Operator
>Greater than.
>=Greater than or equal to.
<less than.
<=Less than or equal to.
==Equal to.
!=Not Equal to

For Example: 25>5(true), 3<=3(true), 5!=5(false) etc.

3. Logical Operators

Logical operators are used to combining two or more expressions. Relational and logical operators are often working together.

The expression that uses logical operators returns zero (false) or non-zero (true).

If we want to compare more than one condition then we need to use logical operators.

‘C’ language contains the following three logical operators:

OperatorMeaning of Operator
&&Logical AND.
||Logical OR.
!Logical NOT.

The Logical AND and logical OR (||) are binary operators while logical NOT (!) is a unary operator.

The following table shows the truth table for logical operators using 1’s and 0’s.

pqp&&qp||q!p
00001
01011
11110
10010

In the case of logical AND, the second operand is not evaluated if the first operand is false.

In the case of logical OR, the second operand is not evaluated if the first operand is true.

Consider an example, i is an integer variable whose value is 7, f is a floating-point variable whose value is 5. 5, and c is a character variable that represents the character ‘w’.

ExpressionInterpretationValue
(i>=5)&&(c==’w’) true 1True1
(i>=6)||(c==115) true 1True1
(f<11)&&(i>100) false 0False0
(c!=’p’)||((i+f)<=10) true 1True1
!(f>5) false 0False0
!(i<=3) true 1True1

4. Increment and Decrement Operators

Two most commonly used unary operators are increment operator (++) and the decrement operator(–).

The increment ++ operator causes its operand to increase by one. The decrement — operator causes its operand to be decreased by one. The operand used with each of these operators must be a single variable.

For example, x is an integer variable that has been assigned a value Of 10. The expression ++x, which is equivalent to writing x = x+1, causes the value of x to be creased to 11.

similarly, the expression –x, which is equivalent to x =x-1, causes the original value of x to be decreased to 9.

The increment and decrement operators can each be utilized in two different ways, depending on whether the operator is written before or after the operand i.e. prefix and postfix form.

Pre-Increment Operator:

If the operator ++ precedes the operand, it is pre-increment i.e., ++y. It is used to increment the value of the variable before using the expression.

In the pre-increment value is first incremented and then used inside the expression. For example b = ++y; In this example suppose the value of variable ‘y’ is 5 then the value of the variable ‘b’ will be 6 because the value of ‘y’ is incremented first then used in an expression. So y will be ‘6’.

Post-Increment Operator:

If the operator ++ follows the operand, it is post-increment i.e. y++.

In the post-increment value is first used in an expression and then incremented. For example b=y++; In this example suppose the value of variable ‘y’ is 5 then value of the variable ‘b’ will be 5 because the value of ‘y’ used first then y is incremented. so y will be ‘6’.

Pre-Decrement Operator:

If the operator — precedes the operand, it is pre-decrement i.e., –y. It is used to decrement the value of the variable before using the expression. In the pre-decrement, value is first decremented and then used inside the expression.

For Example, b=–y; In this example suppose the value of the variable ‘y’ is 5 then the value of the variable ‘b’ will be 4 because the value of ‘y’ is decremented first then used in a expression. Value of y will be ‘4’.

Post-Decrement Operator:

If the operator — follows the operand, it is post-decrement i.e., y–. In the Post-Decrement value is first used in an expression and then decremented.

For Example b=–y; In this example suppose the value of variable ‘y’ is 5 then value of variable ‘b’ will be 5 because the value of ‘y’ used first then y is decremented. So y will be ‘4’.

5. Bitwise Operators

During computation, mathematical operations like addition, subtraction, addition and division are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming language to perform bit-level operations. Bitwise operators used for testing, setting or shifting the actual bits in a byte or word; which is standard data type int or char.

Bitwise operators cannot be used with float, double, void, long double or other complex types.

OperatorsMeaning Of Operators
&Bitwise AND
|Bitwise OR
^Bitwise Ex-OR
~Bitwise Complement
<<Left Shift
>>Right Shift

The bitwise &, |, ^, <<, >> are binary operator. The truth table of &,|, ^ is shown below:

xyx|yx&yx^y
00000
01101
10101
11110

Let us suppose the bitwise AND operation of two integers 12 and 25.

A=12=00001100(In Binary) and B=25=00011001(In Binary)

A&B=00001000=8, A|B=00011101=29, A^B=00010101=21.

Bitwise Complement Operator(~): Bitwise Complement Operator is a unary operator. It changes 1 to 0 and 0 to 1. It is denoted by (~).

Example: ~(00100011)=11011100.

Right Shift Operator: It Shifts all bits towards the right by a certain number of specified bits. It is denoted by >>.

Example: 212=11010100(In Binary) 212>>2=00110101.

Left Shift Operator: It Shifts all bits towards left by a certain number of specified bits. It is denoted by <<.

Example: 212=11010100(In Binary) 212<<1=110101000.

6. Assignment Operators

The most commonly used operator is an assignment operator =. The Assignment operator = is used to assign a value of the expression to the variable.

An expression may be as simple as a single constant or as complex as we require. The assignment expressions that make use of this operator are written in the form/syntax:
identifier=expression where, identifier generally represents a constant, a variable or a more complex expression.

Assignment operator = and the equality operator == are distinctly different. The assignment operator is used to assign a value to an identifier, whereas the equality operator is used to determine if two expressions have the same value. These two operators cannot be used in place of one another.

If the two operands in an assignment expression are of different data types, then the value of the expression on the right will automatically be converted to the type of the identifier on the left. The entire assignment expression will then be of this same data type.

For example, the floating-point value may be truncated if assigned to an integer identifier. a double-precision value may be rounded if assigned to a floating-point identifier, an integer quantity may be altered if assigned to a shorter integer identifier or to a character identifier.

Examples: a=10 (value 10 is assign to variable a).

We can assign many variables of the same value by using multiple assignments in a single statement. Example: a=b=c=0;

7. Conditional Operators

The conditional operator (?:) is a ternary operator in ‘C’ language. An expression that makes use of the conditional operator is called a conditional expression. Such an expression can be written in place of the more traditional if-else statement.

operators in c

A conditional expression is written in the form/syntax: Expression 1? Expression 2: Expression 3

When evaluating a conditional expression, expression 1 is evaluated first.

If Expression 1 is true, then expression 2 is evaluated and this becomes the value of the conditional expression.

If Expression 1 is false, then expression 3 is evaluated and this becomes the value of the conditional expression.

if a=10 b=5, max=(a>b)? a:b; so max variable will contain 10.

sizeof() Operator:

It is a compile-time unary operator which can be used to compute the size of its operand.

sizeof() operator is used to calculate the size of data type or variables. sizeof() operator will return the size in integer format in bytes.

It can be used with data types or variables or constants, we can use this operator in dynamic memory allocation to calculate memory.

Syntax: sizeof(datatype); OR sizeof(variable);

Also Read: