### 1 Million +

Strong Tech Community

### 500 +

Questions to Practice

Jobs Referrals

# Operators In C

Back to home  Logicmojo - Updated March 6, 2023

### Introduction

A wide range of built-in operators are supported by C. The use of an operation or collection of operations on a variable or set of variables is represented by the use of operators, which are symbols. To carry out particular mathematical and logical operations on operands, C has a collection of operators.

The operators are various symbols that provide instructions to a compiler for carrying out particular logical or mathematical operations. The operators are the building blocks of computer languages.

### Operators In C

C operators are one of the features in C that have signs that can be applied for mathematical, relational, bitwise, conditional, or logical manipulations. There are many built-in operators in the C programming language that can be used to carry out different duties as required by the program. Operators are typically used in programs to manipulate data and variables and are included in mathematical, logical, or conditional statements.

In other terms, an operator is a symbol that instructs the compiler to carry out particular mathematical, logical, or conditional operations. It functions on a number or a variable as a symbol. For instance, the addition and subtraction operators in any C application are + and -.  Let us Understand by a simple example,

```int a = 100 + 50;
```

In the above example, "+" operator is being used which is known as addition operator for adding two operands, which are adding two integers and we get a sum of 150, after adding two integers.

### Use Of Operator in C

In simple terms, operators act as symbols that can be applied to any number or variable. We use it to carry out a variety of tasks, including logical, mathematical, relational, and many others. For carrying out specific kinds of mathematical procedures, a programmer must use a variety of operators. As a result, the operators' main function is to carry out different logical and mathematical calculations.

Programming languages with extensive built-in functions include C, for example. These operators are frequently used. All programming languages have these operators as very strong and practical features, and without them, the usefulness of those languages would essentially be useless. These make it very simple and effective for programmers to create the code.

### Types Of Operators In C

In the C language, various operators are useful to assist a programmer to carry out various types of operations. Using these operators, we can manage a variety of operations in a program:

1. Arithmetic Operators

2. Relational Operators

3. Logical Operators

4. Bitwise Operators

5. Assignment Operators

6. Conditional Operators

7. Misc Operators       ### Arithmetic Operators In C

The arithmetic operators in C language assist a user in performing mathematical as well as arithmetic procedures in a program, such as subtraction (-), addition (+), division (/), multiplication (*), remainder of division (%), decrement (-), and increment (++).

Arithmetic operators are classified into two types:

1. Binary Operators :–

This operator operates with two operands such as +,-,*,/.

2. Unary Operators :-

This type of operator, like ++ and -, operates with a single value (operand).

The table that follows lists all of the arithmetic operators available in the C language, as well as their individual functions.

OperatorOperator NameDescription Of Operator
substractionsubtracts second operand from the first
*Multiplicationmultiplies both operands
/DivisionDivides numerator by de-numerator.
%Modulusoperator gives the remainder of an integer after division
++Incrementincrement operator increases the integer value by 1
--Decrementdecrement operator decreases the integer value by 1

There are two kinds of Decrement and Increment operators:

Prefix - A prefix operator is used when we use the operator before the accessible variable in a program as a prefix. The program first adds 1 and then gives the resultant value to the variable in a prefix increment operator. The program first subtracts 1 and then gives the resultant value to the variable when using a prefix decrement operator. For instance, we can write ++x and --x in the prefix form.

Postfix - A postfix operator is used when we use the operator after the accessible variable in a program as a postfix. The program first assigns the value to the variable, then adds 1, and finally allocates the resultant value in a postfix increment operator. In a postfix decrement operator, the program first gives the variable's value, then subtracts 1, and finally assigns the resultant value. For instance, we can write x++ and x-- in the postfix form..

#### Program for Arthematic Operators

```#include <`stdio.h`>
int main()
{
int a = 25, b = 4, c;
c = a + b;
printf("a+b = %d \n", c);
c = a - b;
printf("a-b = %d \n", c);
c = a *b;
printf("a*b = %d \n", c);
c = a / b;
printf("a/b = %d \n", c);
c = a % b;
printf("Remainder when a divided by b = %d \n", c);

printf("++a = %d \n", ++a);

printf("--b = %d \n", --b);

printf("a++ = %d \n", a++);

printf("b-- = %d \n", b--);

return 0;
}
```

#### Output

```a+b = 29
a-b = 21
a*b = 100
a/b = 6
Remainder when a divided by b = 1
++a = 26
--b = 3
a++ = 26
b-- = 3
```

The program's operators are +, -, and *, which calculate addition, subtraction, and multiplication, respectively. 25/4 equals 6.25 in standard math. In the above program, however, the result is 6. This is due to the fact that both values a and b are integers. As a result, the output must also be a number. As a result, the compiler ignores the word after the decimal point and displays 1 instead of 1.25 as the program output.

A modulus operator can only be used with integers. You can calculate the remainder of any integer using the modulus operator (%). The remainder is 1 when a=25 is split by b=4. If we want the division operator's result in decimal values, one of the operands must be a floating-point integer.

### Relational Operators In C

Relational operators facilitate in the formation of a connection or comparison between two operands. As a result, relational operators assist us in making programme decisions, and their end outcome is either true or false. Relationship operators include (==,!=,, >, =, >=).

The table below lists all of the relational operators that C supports. Here, we presume that variables a = 5, b = 9.

OperatorOperator NameDescription Of OperatorExample
==equal toit check if two operands are equala==b returns 0
!=not equal toit check if two operands are not equal.a!=b returns 1
>greater thanit check if the operand on the left is greater than operand on the righta>b returns 0
<less thanit check if the operand on the left is smaller than the right operand.a< b returns 1
>=greater than or equal toit check if the left operand is greater than or equal to the right operanda>=b returns 0
<=less than or equal toit check if operand on left is smaller than or equal to the right operanda<=b returns 1

#### Program for Relational Operators in C

```#include <`stdio.h`>

int main()

{

int a = 5, b = 9;

printf("%d == %d is False(%d) \n", a, b, a == b);

printf("%d != %d is True(%d) \n ", a, b, a != b);

printf("%d > %d is False(%d)\n ",  a, b, a > b);

printf("%d < %d is True (%d) \n", a, b, a < b);

printf("%d >= %d is False(%d) \n", a, b, a >= b);

printf("%d <= %d is True(%d) \n", a, b, a <= b);

return 0;

}
```

#### Output

```5 == 9 is False(0)
5 != 9 is True(1)
5 > 9 is False(0)
5 < 9 is True (1)
5 >= 9 is False(0)
5 <= 9 is True(1)
```

### Logical Operators In C

When we need to evaluate more than one condition to make a decision in the C programming language, we have three logical operators. In C, an expression having a logical operator returns either 0 or 1, depending on whether the expression returns true or false. In C program, logical operators are commonly used to make decisions.

The table below lists all of the logical operators provided by the C programming language. We are presuming that variables a = 12 and b = 10.

OperatorOperator NameDescription Of OperatorExample
&&logical ANDit returns true if both side operands value is true otherwise returns falsea && b returns false
||logical ORit returns true if one of the operand's value is true or both of the operand's values is true otherwise returns falsea || b returns true
!logical Notit returns true if the condition in consideration is not satisfied Otherwise returns false!a returns false

#### Program for Logical Operators in C

```#include<`stdio.h`>
int main()
{
int a = 10, b = 10, c = 20, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) equals to %d \n", result);

result = (a == b) && (c < b);
printf("(a == b) && (c < b) equals to %d \n", result);

result = (a == b) || (c < b);
printf("(a == b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);
printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);
printf("!(a == b) equals to %d \n", result);

result = !(a == b);
printf("!(a == b) equals to %d \n", result);

return 0;
}
```

#### Output

```(a == b) && (c > b) equals to 1
(a == b) && (c < b) equals to 0
(a == b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a == b) equals to 1
!(a == b) equals to 0
```

1. (a == b) && (c > 15) calculates to 1 because both the operands (a == b) and (c > b) are 1 (true).

2. (a == b) && (c < b) calculates to 0 because of the operand (c < b) is 0 (false).

3. (a == b) || (c < b) calculates to 1 because of the operand (a = b) is 1 (true).

4. (a != b) || (c < b) calculates to 0 because both the operand (a != b) and (c < b) are 0 (false).

5. !(a != b) calculates to 1 because the operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).

6. !(a == b) calculates to 0 because the (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

### Bitwise Operator In C

In C, we use bitwise operators to execute bit-level operations on various operands. It converts the operators to bit-level first, then conducts various calculations.

Bitwise operators operate on bits and are used to conduct bit-by-bit operations. The truth table for the &, ^, and | is as follows:

aba & b (Bitwise AND) a | b (Bitwise OR)a ^ b (Bitwise XoR)
11110
00000
01011
10011

Assume A = 45 and B = 22 in binary form, as shown below.

A = 0010 1101

B = 0001 0110

Lets see how Bitwise works,

A&B = 0000 0100

A|B = 0011 1111

A^B = 0011 1011

OperatorOperator NameDescription Of Operator
&Binary AND Operatorcopies a bit to the result if it exists in both the operands
|Binary OR Operatorcopies a bit if and only if it exists in either operand.
~Binary XOR Operatorcopies the bit only if it is set in one operand but not both.
^Binary One's Complement OperatorIt is unary and has the effect of 'flipping' bits.
<<Binary Left Shift Operatorvalue of the left operands is moved left by the number of bits specified by the right operand
>>Binary Right Shift Operator value of the left operands is moved right by the number of bits specified by the right operand

#### Program for Bitwise Operators in C

```#include

int main()
{
int a = 45, b = 22;
printf("Output = %d \n", a&b);
printf("Output = %d \n", a|b);
printf("Output = %d \n", ~a);
return 0;
}
```

#### Output

```Output = 4
Output = 63
Output = -46
```

### Assignment Operator In C

An assignment operator in a program is primarily responsible for giving a value to a variable. To give the result of an expression to a variable, use assignment operators. This operator is essential for giving values to variables." = " is the most commonly used assignment function.

The C language includes a set of shorthand assignment operators that can be used in C computing. The following table shows all of the assignment operators that the C language supports:

OperatorOperator NameDescription Of OperatorExample
=assignmentassign values from right side operands to left side operanda = b
+=add assignadd the right operand to the left operand and assign the result to lefta += q is similar to a = a=b
-=substract assignsubtract the right operand from the left operand and assign the result to the left operanda -= b is similar to a = a-b
*=multiply assignmultiply the left operand with the right operand and assign the result to the left operanda *= b is similar to a = a*b
/= divide assigndivide the left operand with the right operand and assign the result to the left operand a /= b is similar tof a = a/b
%=modulus assigncalculate modulus using two operands and assign the result to the left operanda %= b is similar to a = a%b

#### Program for Assignment Operators in C

```#include<`stdio.h`>

int main()

{

int a = 4, b;

b = a;
printf("b = %d\n", b);

b += a;
printf("b = %d\n", b);

b -= a;
printf("b = %d\n", b);

b *= a;
printf("b = %d\n", b);

b /= a;
printf("b = %d\n", b);

b %= a;
printf("b = %d\n", b);

return 0;

}
```

#### Output

```b = 4
b = 8
b = 4
b = 16
b = 4
b = 0
```

### Conditional Operators In C

The conditional statement is built using ternary or conditional operators. These are used to make decisions. A set of conditional operators "?:"

#### Syntax

```Expression1 ? Expression2 : Expression3
```

The Operator " ?: " operates like this: Expression1 is evaluated first. If it is true, the Expression2 is assessed and becomes the expression's value. If Expression1 is false, Expression3 is evaluated and its result becomes the expression's value.

### Misc Operators In C

Aside from the operators mentioned above, the C programming language implements a few other important operators, which include operators (such as?: and sizeof). The following table lists all of the miscellaneous operators accessible in the C language, along with their individual functions.

OperatorOperator NameDescription Of OperatorExample
sizeof()sizeofreturns the size of a variableIf a is an integer, then the size of(a) will return to be 4
&Address Operatoroperator is used to get the address of the variable.&a will give an address of a
*pointer operator operator is used as a pointer to a variable.*a
,comma operatorUsed to link the related expressions togethervalue = (a=1, b=3)
cast type castconverts one datatype to another datatypeint(6.3000) would return 6

#### Program for Misc Operators in C

```#include<`stdio.h`>

int main()
{
int *ptr, b;
b = 8;

ptr = `&b`;

printf("Adress of b is : %d \n", *ptr);
printf("Size of Integer b is : %d \n", (int)sizeof(b));
return 0;
}
```

#### Output

```Adress of b is : 8
Size of Integer b is : 4
```

### Precedence of Operators in C

Operator precedence is another feature of the C computer language that determines how terms in an expression are grouped and how an expression is evaluated based on the provided expressions. Some operators have greater precedence than others, while others have lower precedence. In C, for example, the multiplication operator takes priority over the addition operator.

Category OperatorAssociativity
Postfix() [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / %Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND& Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND&& Left to right
Logical OR || Left to right
Conditional?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma , Left to right