# Operator in C with Types and Examples

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.

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.

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

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

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

## Assignment COperators

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.

## Misc. Operators

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

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

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