# Unlock the complete

Logicmojo experience for FREE

Sign Up Using

### 1 Million +

Strong Tech Community

### 500 +

Questions to Practice

### 50 +

Jobs Referrals

Logicmojo experience for FREE

Sign Up Using

Strong Tech Community

Questions to Practice

Jobs Referrals

- Introduction
- Operators In C
- Use Of Operator in C
- Types Of Operators In C
- Arithmetic Operators In C
- Relational Operators In C
- Logical Operators In C
- Bitwise Operators In C
- Assignment Operators In C
- Conditional Operators In C
- Misc Operators In C
- Precedence of Operators in C
- Conclusion

Logicmojo - Updated March 6, 2023

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.

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.

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.

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:

Arithmetic Operators

Relational Operators

Logical Operators

Bitwise Operators

Assignment Operators

Conditional Operators

Misc Operators

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:

**Binary Operators :–**This operator operates with two operands such as +,-,*,/.

**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.

Operator | Operator Name | Description Of Operator |
---|---|---|

+ | addition | adds two operands |

– | substraction | subtracts second operand from the first |

* | Multiplication | multiplies both operands |

/ | Division | Divides numerator by de-numerator. |

% | Modulus | operator gives the remainder of an integer after division |

++ | Increment | increment operator increases the integer value by 1 |

-- | Decrement | decrement 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..

`#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;
}

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 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.

Operator | Operator Name | Description Of Operator | Example |
---|---|---|---|

== | equal to | it check if two operands are equal | a==b returns 0 |

!= | not equal to | it check if two operands are not equal. | a!=b returns 1 |

> | greater than | it check if the operand on the left is greater than operand on the right | a>b returns 0 |

< | less than | it check if the operand on the left is smaller than the right operand. | a< b returns 1 |

>= | greater than or equal to | it check if the left operand is greater than or equal to the right operand | a>=b returns 0 |

<= | less than or equal to | it check if operand on left is smaller than or equal to the right operand | a<=b returns 1 |

`#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;
}

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)

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.

Operator | Operator Name | Description Of Operator | Example |
---|---|---|---|

&& | logical AND | it returns true if both side operands value is true otherwise returns false | a && b returns false |

|| | logical OR | it returns true if one of the operand's value is true or both of the operand's values is true otherwise returns false | a || b returns true |

! | logical Not | it returns true if the condition in consideration is not satisfied Otherwise returns false | !a returns false |

`#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;
}

(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

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

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

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

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

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

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

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:

a | b | a & b (Bitwise AND) |
a | b (Bitwise OR) | a ^ b (Bitwise XoR) |
---|---|---|---|---|

1 | 1 | 1 | 1 | 0 |

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 0 | 0 | 1 | 1 |

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

Operator | Operator Name | Description Of Operator |
---|---|---|

& | Binary AND Operator | copies a bit to the result if it exists in both the operands |

| | Binary OR Operator | copies a bit if and only if it exists in either operand. |

~ | Binary XOR Operator | copies the bit only if it is set in one operand but not both. |

^ | Binary One's Complement Operator | It is unary and has the effect of 'flipping' bits. |

<< | Binary Left Shift Operator | value 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 |

#includeint 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 = 4 Output = 63 Output = -46

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:

Operator | Operator Name | Description Of Operator | Example |
---|---|---|---|

= | assignment | assign values from right side operands to left side operand | a = b |

+= | add assign | add the right operand to the left operand and assign the result to left | a += q is similar to a = a=b |

-= | substract assign | subtract the right operand from the left operand and assign the result to the left operand | a -= b is similar to a = a-b |

*= | multiply assign | multiply the left operand with the right operand and assign the result to the left operand | a *= b is similar to a = a*b |

/= | divide assign | divide the left operand with the right operand and assign the result to the left operand | a /= b is similar tof a = a/b |

%= | modulus assign | calculate modulus using two operands and assign the result to the left operand | a %= b is similar to a = a%b |

`#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;
}

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

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

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.

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.

Operator | Operator Name | Description Of Operator | Example |
---|---|---|---|

sizeof() | sizeof | returns the size of a variable | If a is an integer, then the size of(a) will return to be 4 |

& | Address Operator | operator 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 operator | Used to link the related expressions together | value = (a=1, b=3) |

cast | type cast | converts one datatype to another datatype | int(6.3000) would return 6 |

#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; }

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

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 | Operator | Associativity |
---|---|---|

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 |

The following are the things we learned about the operator in this article:

In C, operators are symbols that are used to execute various operations. It is possible for the action to be mathematical, logical, relational, bitwise, conditional, or logical. Unary operators are classified into seven types: Arithmetic operator, Relational operator, Logical operator, Bitwise operator, Assignment operator, and Conditional operator. Except for the logical and conditional operators, which yield a boolean value, all operators return a numerical value.(true or false). '=' and '==' are not the same thing because '=' gives a value whereas '==' determines whether or not two values are equal. The presence of a precedence in the operator indicates that one operator takes precedence over another.

**Good luck and happy learning!**