**Operators in C Programming Language** – A symbol that operates on a value or variable is known as an C operators. C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions.(from wikipedia )

With the help of examples, you will learn about several operator in C programming in this course. For example, the operator + is used to execute addition. C contains a large number of operators that can be used to do a variety of tasks.

The C language has a large number of built-in operators, including the following: |
---|

1. Arithmetic Operators |

2. Relational Operators |

3. Logical Operators |

4. Bitwise Operators |

5. Assignment Operators |

6. Misc Operators |

**Try to run the C programming code for free online with the C Online Compiler. **

**C arithmetic operations**

On numerical values, an arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, and division (constants and variables).

The table below lists all of the arithmetic operators offered by the C programming language. Assume variable X has a value of 20 and variable Y has a value of 45.

Operator | Meaning of Operator | Description | Example |
---|---|---|---|

+ | addition or unary plus | Adds two operands to the equation. | X + Y= 65 |

– | subtraction or unary minus | The second operand is subtracted from the first. | X – Y= -25 |

* | multiplication | Both operands are multiplied. | X * Y= 900 |

/ | division | Numerator is divided by de-numerator. | Y / X = 2 |

% | remainder after division (modulo division) | After an integer division, the remainder of the modulus operator. | Y % X = 5 |

++ | Increases Increment | The increment operator adds one to the integer value. | X++ = 21 |

— | Decreases Increment | The increment operator reduces the value of an integer by one. | Y– = 44 |

**Example of C Arithmetic Operators**

#include<stdio.h> int main() { int i = 30, k = 5; printf("Initial value of i = %d\n", i); printf("Initial value of k = %d\n\n", k); k = ++i; printf("After incrementing by 1: i = %d\n", i); printf("y = %d\n\n", k); k = --i; printf("After decrementing by 1: i = %d\n", i); printf("y = %d\n\n", k); return 0; }

**The output of the Program:**

Initial value of i = 30

Initial value of k = 5

After incrementing by 1: i = 31

y = 31

After decrementing by 1: i = 30

y = 30

**Explanation:**

As you might guess, the operators +, -, and * compute addition, subtraction, and multiplication, respectively.

45/20 Equals 2.25 in conventional math. In the program, however, the output is 2. Because both x and y are integers, this is the case.

As a result, the result is also an integer. The compiler ignores the phrase after the decimal point, resulting in an answer of 2 rather than 2.25.

The residual is computed using the percent modulo operator. The remaining is 5 when x=20 is divided by y=45. Only integers can be used with the percent operator.

**You can try to test here the example code above**

**C Increment and Decrement Operators**

The **increment ++ and decrement C operators** modify the value of an operand (constant or variable) by one.

**Example**

#include<stdio.h> int main() { int x = 12, y = 1; printf("Initial value of x = %d\n", x); printf("Initial value of y = %d\n\n", y); y = x++; printf("After incrementing by 1: x = %d\n", x); printf("y = %d\n\n", y); y = x--; printf("After decrementing by 1: x = %d\n", x); printf("y = %d\n\n", y); return 0; }

**The output of the Program:**

Initial value of x = 12

Initial value of y = 1

After incrementing by 1: x = 13

y = 12

After decrementing by 1: x = 12

y = 13

**Explanation:**

**Decrement **— reduces the value by one, whereas **increment **++ increases it by one. These two operators are unary, which means they only operators work with one operand.

**You can try to test here the example code above**

**C Relational Operators**

**Relational Operator in C** is the relationship between two operands. It yields 1 if the relationship is true, and 0 if the relationship is false.

The table below lists all of the relational operators that C supports. Assume variable A has a value of 10 and variable B has a value of 20.

Operator | Description | Example |
---|---|---|

== | Checks whether two operands have the same value. If the answer is yes, the condition is true. | (A == B) is not true. |

!= | Checks whether two operands have the same value. The condition becomes true if the values are not equal. | (A != B) is true. |

> | Checks whether the left operand’s value is greater than the right operand’s value. If the answer is yes, the condition is true. | (A > B) is not true. |

< | Checks whether the left operand’s value is less than the right operand’s value. If the answer is yes, the condition is true. | (A < B) is true. |

>= | Checks whether the left operand’s value is greater than or equal to the right operand’s value. If the answer is yes, the condition is true. | (A >= B) is not true. |

<= | Checks whether the left operand’s value is less than or equal to the right operand’s value. If the answer is yes, the condition is true. | (A <= B) is true. |

** Example**

#include <stdio.h> int main() { int x = 20; int y = 12; printf("x > y: %d \n", x > y); printf("x >= y: %d \n", x >= y); printf("x <= y: %d \n", x <= y); printf("x < y: %d \n", x < y); printf("x == y: %d \n", x == y); printf("x != y: %d \n", x != y); }

**The output of the Program:**

x > y: 1

x >= y: 1

x <= y: 0

x < y: 0

x == y: 0

x != y: 1

**Explanation:**

We verified X and Y values against every relational operator we have in this relational operators example. 1 is TRUE, while 0 is FALSE.

**You can try to test here the example code above**

**C Logical Operators**

**C Logical Operators** – Depending on whether the expression is true or false, a logical operator expression returns either 0 or 1. In C programming, logical operators are frequently employed in decision-making.

Operator | Meaning | Example |
---|---|---|

&& | It’s known as the logical AND operator. The condition is true if both operands are non-zero. | Expression ((c==5) && (d>5)) equals 0 if c = 5 and d = 2. |

|| | The Logical OR Operator is a term used to describe a type of logic. The condition becomes true if any of the two operands is non-zero. | Expression ((c==5) || (d>5)) equals 1 if c = 5 and d = 2. |

! | It’s known as the Logical NOT Operator. Its operand’s logical state is reversed using it. If a condition is true, the Logical NOT operator will turn it into a false condition. | When c equals 5, the expression!(c==5) equals 0. |

**Example**

#include <stdio.h> int main() { int x = 5, y = 5, z = 10, result; result = (x == y) && (z > y); printf("(x == y) && (z > y) is %d \n", result); result = (x == y) && (z < y); printf("(x == y) && (z < y) is %d \n", result); result = (x == y) || (z < y); printf("(x == y) || (z < y) is %d \n", result); result = (x != y) || (z < y); printf("(x != y) || (z < y) is %d \n", result); result = !(x != y); printf("!(x != y) is %d \n", result); result = !(x == y); printf("!(x == y) is %d \n", result); return 0; }

**The output of the Program:**

(x == y) && (z > y) is 1

(x == y) && (z < y) is 0

(x == y) || (z < y) is 1

(x != y) || (z < y) is 0

!(x != y) is 1

!(x == y) is 0

**Explanation**:

- Because both operands (a == b) and (c > b) are 1, (a == b) && (c > 5) evaluates to 1. (true).
- Because operand (c b) is 0, (a == b) && (c b) evaluates to 0. (false).
- (a == b) || (c b) equals 1 since (a = b) equals 1. (true).
- Because operand (a!= b) is 0,!(a!= b) evaluates to 1. (false). As a result,!(a!= b) equals 1. (true).
`!(a == b)`

evaluates to 0 because`(a == b)`

is 1 (true). Hence,`!(a == b)`

is 0 (false).

**You can try to test here the example code above**

**C Bitwise Operators**

**C Bitwise Operators** – This lesson will teach you how to use all six bitwise operators in C programming, along with examples.

Mathematical operations such as addition, subtraction, multiplication, and division are performed in bit-level in the arithmetic-logic unit (which is part of the CPU). Bitwise operators are used in C programming to execute bit-level operations.

In binary representation, assuming A = 60 and B = 13, they will be as follows:

- A = 0011 1100
- B = 0000 1101

—————–

- A&B = 0000 1100
- A|B = 0011 1101

- A^B = 0011 0001
- ~A = 1100 0011

The bitwise operators supported by C are listed in the table below. Assume variable ‘A’ has a value of 60 and variable ‘B’ has a value of 13, then

Operator | Description | Example |
---|---|---|

& | If a bit exists in both operands, the binary AND operator duplicates it to the result. | (A & B) = 12, example : 0000 1100 |

| | If a bit exists in both operands, the binary OR operator copies it. | (A | B) = 61, example : 0011 1101 |

^ | If a bit is set in one operand but not both, the binary XOR operator replicates it. | (A ^ B) = 49, example : 0011 0001 |

~ | The Binary One’s Complement Operator is a unary operation that ‘flips’ bits. | (~A ) = ~(60), example : -0111101 |

<< | Left Shift Operator in Binary. The value of the left operand is shifted to the left by the number of bits given by the right operand. | A << 2 = 240, example : 1111 0000 |

>> | Right Shift Operator in Binary. The value of the left operand and assign advanced by the number of bits supplied by the right operand. | A >> 2 = 15, example : 0000 1111 |

**Assignment C** **Operators**

Assignment operators are used to give a variable a value. The assignment operator’s left side operand is a variable, while the assignment operator’s right side operand is a value. Otherwise, the compiler will throw an error if the value on the right side is not of the same data-type as the variable on the left side.

Operator | Description | Example |
---|---|---|

= | This is a straightforward assignment operator. Values from the right side operands are assigned to the left side operand. | C = A + B will assign the result value of A + B to C |

+= | The AND assignment operator is used to combine two variables. The right operand is added to the left operand, and the result is assigned to the left operand. | C += A is equivalent to C = C + A |

-= | Subtract AND assignment is a combination of subtraction and assignment. It takes the right operand and subtracts it from the left operand before assigning the result to the left operand. | C -= A is equivalent to C = C – A |

*= | The AND operator multiplies and assigns. It adds the right and left operands together and assigns the result to the left operand. | C *= A is equivalent to C = C * A |

/= | The AND operator divides and assigns. The left operand is divided by the right operand, and the result is assigned to the left operand. | C /= A is equivalent to C = C / A |

%= | The modulus AND assignment operator is a combination of the modulus and the assignment operator. It uses two operands to compute modulus and assigns the result to the left operand. | C %= A is equivalent to C = C % A |

<<= | Operator for both the left shift AND the assignment. | C <<= 2 is same as C = C << 2 |

>>= | Operator for both the right shift and the assignment. | C >>= 2 is same as C = C >> 2 |

&= | Bitwise AND assignment operator. | C &= 2 is same as C = C & 2 |

^= | Bitwise exclusive OR and assignment operator. | C ^= 2 is same as C = C ^ 2 |

|= | Bitwise inclusive OR and assignment operator. | C |= 2 is same as C = C | 2 |

## Misc. Operators

Aside from the operators mentioned above, the C Language has a few more significant operators, such as sizeof and ?:

Operator | Description | Example |
---|---|---|

sizeof() | Returns the variable’s size. | sizeof(a), where a is integer, will return 4. |

& | Returns the variable’s address. | &a; returns the actual address of the variable. |

* | A variable’s pointer. | *a; |

? : | Expression that is conditional. | If Condition is true ? then value X : otherwise value Y |

**Operators Precedence in C**

The order in which terms in an expression are grouped and evaluated is determined by operator precedence. Certain operators take precedence over others; the multiplication operator, for example, takes precedence over the addition operator.

For example, x = 7 + 3 * 2; because operator * has higher precedence than +, x is allocated 13, not 20, and is multiplied by 3*2 before being added to 7.

The highest-priority operators appear at the top of the table, while the lowest-priority operators appear at the bottom. Higher precedence operators will be evaluated first within an expression.

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 |

**Comma Operator in C**

It’s mostly used to separate expressions, variable declarations, and function calls, among other things. It has two operands to operate with. It’s a one-to-one operator. The comma is used as a separator.

**Syntax of comma operator:**

int a=1, b=2, c=3, d=4;

**Summary**

Operator is a symbol that operates on a variable or value is known as an operator. The types of operators include arithmetic, logical, conditional operator, ternary operator, unary operators, operators precedence, comparison operators, relational, bitwise, and assignment operators, among others. Sizeof() operator, Pointer operator, Reference operator, and other specific sorts of operators are also available in C.

In the next following post, I’ll create a **Decision Making in C** and try to explain its many components in full details. I hope you enjoy this post on **Operators in C Programming**, in which I attempt to illustrate the language’s core grammar.

**PREVIOUS**

**NEXT **