## C Operators

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

## Operators in C for Arithmetic

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// The Program to perform the arithmetic operators #include<stdio.h> int main() { int x = 20, y = 45; int addition, subtraction, multiplication, division, modulus; addition = x + y; //addition of 20 and 45 subtraction = x - y; //subtract 20 from 45 multiplication = x * y; //Multiplying both division = y / x; //dividing 45 by 20 (number of times) modulus = y % x; //calculation the remainder printf("Addition of two numbers x, y is : %d\n", addition); printf("Subtraction of two numbers x, y is : %d\n", subtraction); printf("Multiplication of two numbers x, y is : %d\n", multiplication); printf("Division of two numbers x, y is : %d\n", division); printf("Modulus of two numbers x, y is : %d\n", modulus); } |

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.

### Output:

## C Increment and Decrement Operators

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

## Example of C Increment and Decrement Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include<stdio.h> int main() { int i = 30, k = 5; printf("Initial value of i = %d\n", i); // print the initial value of i printf("Initial value of k = %d\n\n", k); // print the initial value of k k = ++i; // increment the value of i by 1 then assign this new value to k printf("After incrementing by 1: i = %d\n", i); printf("y = %d\n\n", k); k = --i; // decrement the value of i by 1 then assign this new value to k printf("After decrementing by 1: i = %d\n", i); printf("y = %d\n\n", k); // Signal to operating system everything works fine return 0; } |

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

### Output:

## C Relational Operators

The relationship between two operands is checked by a relational operator. 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 of C Relational Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/* The Program to Perform C Relational Operations on integers */ #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); } |

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

### Output:

## 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 of Logical Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
// The program to perform the logical operators #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; } |

- 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 both operands (a!= b) and (c b) are 0, (a!= b) || (c b) evaluates to 0. (false).
- 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).

### Output:

## 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 is advanced by the number of bits supplied by the right operand. | A >> 2 = 15, example : 0000 1111 |

## Bitwise AND operator &

If the matching bits of two operands are 1, the outcome of bitwise AND is 1. If one of an operand’s bits is 0, the result of the associated bit is also 0.

Consider the bitwise AND operation between two integers of 12 and 25.

1 2 3 4 5 6 7 8 |
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bit Operation of 12 and 25 00001100 & 00011001 ________ 00001000 = 8 (In decimal) |

## Example of Bitwise AND

1 2 3 4 5 6 7 |
#include <stdio.h> int main() { int x= 12, y= 25; printf("Output = %d", x|y); return 0; } |

We declared two integers, x and y, and assigned the values 9 and 25 to them in our bitwise operators program. 12= 0001001 and 25= 00011001 in binary. The printf statements below will conduct bitwise operations on a and b, then display the results.

### Output:

## Bitwise OR operator |

When at least one corresponding bit of two operands is 1, the output of bitwise OR is 1. The bitwise OR operator is indicated by the symbol | in C programming.

1 2 3 4 5 6 7 8 |
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise OR Operation of 12 and 25 00001100 | 00011001 ________ 00011101 = 29 (In decimal) |

## Example of Bitwise OR

1 2 3 4 5 6 7 |
#include <stdio.h> int main() { int x = 12, y = 25; printf("Output = %d", x|y); return 0; } |

We declared two integers, x and y, and gave them the values 12 and 25 in our bitwise operators program. 12= 0001001 and 25= 00011001 are the binary equivalents of each other. The printf commands that follow will execute bitwise operations on a and b before printing the results.

### Output:

## Bitwise XOR (exclusive OR) operator ^

If the corresponding bits of two operands are opposite, the outcome of the bitwise XOR operator is 1. It is denoted by the symbol ^.

1 2 3 4 5 6 7 8 |
12 = 00001100 (In Binary) 25 = 00011001 (In Binary) Bitwise XOR Operation of 12 and 25 00001100 ^ 00011001 ________ 00010101 = 21 (In decimal) |

## Example of Bitwise XOR

1 2 3 4 5 6 7 |
#include <stdio.h> int main() { int x = 12, y = 25; printf("Output = %d", x^y); return 0; } |

We declared two integers, x and y, and assigned them the values 9 and 65 in our bitwise operators program. 12= 0001001 and 12= 0001001 and 25= 00011001 in binary form. The printf commands below will execute bitwise operations on x and y before displaying the results.

Output:

## Bitwise complement operator ~

The unary complement operator is a bitwise complement operator (works on only one operand). It transforms 1 into 0 and 0 into 1. It is denoted by the symbol.

1 2 3 4 5 6 |
35 = 00100011 (In Binary) Bitwise complement Operation of 35 ~ 00100011 ________ 11011100 = 220 (In decimal) |

## Example of Bitwise complement

1 2 3 4 5 6 7 |
#include <stdio.h> int main() { printf("Output = %d\n",~35); printf("Output = %d\n",~-12); return 0; } |

We declared two output, and assigned them the values 35 and 12 in our bitwise operators program. 35 = 00100011 in binary form. The printf commands below will execute bitwise operations on x and y before displaying the results.

### Output:

## Shift Operators in C

In C programming, there are two shift operators:

Operator on the right shift

The left shift Operator

Operator on the Right Shift

The right shift operator shifts all bits to the right by a specified amount of bits. >> is the symbol for it.

1 2 3 4 5 |
212 = 11010100 (In binary) 212>>2 = 00110101 (In binary) [Right shift by two bits] 212>>7 = 00000001 (In binary) 212>>8 = 00000000 212>>0 = 11010100 (No Shift) |

## Left Shift Operator in C

The left shift operator moves all bits to the left by a specified amount of bits. The bit locations that the left shift operator has abandoned are filled with 0. The left shift operator is represented by the symbol.

1 2 3 4 |
212 = 11010100 (In binary) 212<<1 = 110101000 (In binary) [Left shift by one bit] 212<<0 = 11010100 (Shift by 0) 212<<4 = 110101000000 (In binary) =3392(In decimal) |

## Example of Left and Right Shift Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <stdio.h> int main() { int num=212, j; for (j=0; j<=2; ++j) printf("Right shift by %d: %d\n", j, num>>j); printf("\n"); for (j=0; j<=2; ++j) printf("Left shift by %d: %d\n", j, num<<j); return 0; } |

### Output:

## Assignment Operators in C

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

## Example of Assignment Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <stdio.h> main() { int x = 45; int y ; y = x; printf("Column 1: = Operator Example, Value of y = %d\n", y ); y += x; printf("Column 2: += Operator Example, Value of y = %d\n", y ); y -= x; printf("Column 3: -= Operator Example, Value of y = %d\n", y ); y *= x; printf("Column 4: *= Operator Example, Value of y = %d\n", y ); y /= x; printf("Column 5: /= Operator Example, Value of y = %d\n", y ); y = 200; y %= x; printf("Column 6: %= Operator Example, Value of y = %d\n", y ); y <<= 2; printf("Column 7: <<= Operator Example, Value of y = %d\n", y ); y >>= 2; printf("Column 8: >>= Operator Example, Value of y = %d\n", y ); y &= 2; printf("Column 9: &= Operator Example, Value of y = %d\n", y ); y ^= 2; printf("Column 10: ^= Operator Example, Value of y = %d\n", y ); y |= 2; printf("Column 11: |= Operator Example, Value of y = %d\n", y ); } |

When you compile and run the preceding program, you will get the following output:

### Output:

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

## Example of Misc. Operators

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/*The Program to perform for Misc. Operators*/ #include <stdio.h> int main() { int number1 = 4; short number2; double number3; int* ptr; /* example of sizeof operator */ printf("Column 1 - Size of variable number1 = %d\n", sizeof(number1) ); printf("Column 2 - Size of variable number2 = %d\n", sizeof(number2) ); printf("Column 3 - Size of variable number3= %d\n", sizeof(number3) ); /* example of & and * operators */ ptr = &number1; /* 'ptr' now contains the address of 'a'*/ printf("Value of number1 is %d\n", number1); printf("*ptr is %d.\n", *ptr); /* example of ternary operator */ number1 = 10; number2 = (number1 == 1) ? 20: 30; printf( "Value of number2 is %d\n", number2 ); number2 = (number1 == 10) ? 20: 30; printf( "Value of number2 is %d\n", number2 ); return 0; } |

When you compile and run the preceding program, you will get the following output:

## 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:**

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

### Summary

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

**Related Articles**

- List of IOT- Based Final Year Projects For Engineering Students 2022
- Best Software Engineering Project Topics and Ideas for 2022
- Online Examination System Activity Diagram | UML
- Best Software Engineering Projects Ideas with Source Code 2021
- Library Management System Sequence Diagram | UML
- Event Management System Use Case Diagram | UML
- Online Examination System Use Case Diagram | UML
- Online Examination System ER Diagram
- Railway Reservation System Activity Diagram | UML
- Online Banking System Activity Diagram | UML
- Airline Reservation System activity Diagram | UML
- Student Management System Activity Diagram | UML

**Inquiries**

If you have any questions or suggestions about **C Operators: The Complete Guide for Beginners 2021**, please feel free to leave a comment below.