Back to: C Tutorials For Beginners and Professionals

**Operators in C**

In this article, I am going to discuss **Operators in C** Program with examples. Please read our previous article, where we discussed the **Constants in C** program. As part of this article, you will learn what are Operators in C, it’s type, and when and how to use Operators in C Program with examples.

**C Operators:**

Operators are the special kind of symbol which performs certain operations on the data. The collection of operators along with the data or operands is known as expression. C language supports various type of operators but depends on the number of operands, operators are classified into 3 types:

**Unary Operator****Binary Operator****Ternary Operator**

When we are evaluating any expression with input data we are passing on it is called **operands**, which symbol we are using it is called **operators**.

**Unary Operators:**

Unary means consisting of a single component or element. A Unary Operator in C is an operator that takes a single operand in an expression or a statement. Here +** **& – operators will indicate the sign of operand like +5, -3, -45.

**Types of Unary Operators are:**

**Increment operators (++): **Example: (++x)

**Decrement operators (–): **Example: (–x)

**Increment Operators:**

Increment Operators is a unary operator. It takes one value at a time. It is classified into two types:

- Post Increment Operator
- Pre Increment Operator

**Post Increment Operators **are the operators that are a suffix to its variable. It uses the value present inside the variable. It increments the value present inside the variable by ‘1’ and updates it.

**Syntax: Variable++;**

**Example: x++;**

**Pre Increment Operators **are the operators which is a prefix to its variable. It increments the value present inside the variable by ‘1’ and updates it then it uses the value present inside the variable.

**Syntax: ++Variable;**

**Example: ++x;**

**Program:**

#include<stdio.h> int main() { int x, y, z; x=5; y=x++; printf("x: %d y: %d", x, y); z=++y; printf("y: %d z:%d", x, y); }

**Output:**

**Steps to Follow:**

First, take the memory block.

Write down the expression.

** y=x++;**

** z=++y;**

If the expression contains post operator remove the post and write down the expression again.

** y=x;**

Then assign priority.

** y=x;**

Finally, update the post values into variables (memory block).

**Decrement Operators:**

Decrement Operators is a unary operator. It takes one value at a time. It is classified into two types:

- Post Decrement Operator
- Pre Decrement Operator

**Post Decrement Operators **are the operators that are a suffix to its variable. It uses the value present inside the variable. It decrements the value present inside the variable by ‘1’ and updates it.

**Syntax: Variable–;**

**Example: x–;**

**Pre Decrement Operators **are the operators that are a prefix to its variable. It decrements the value present inside the variable by ‘1’ and updates it then it uses the value present inside the variable.

**Syntax: –Variable;**

**Example: –x;**

**Program:**

#include<stdio.h> int main() { int x, y, z; x=5; y=x--; printf("x: %d y: %d", x, y); z=--y; printf("y: %d z:%d", x, y); }

**Output:**

**Steps to Follow:**

First, take the memory block.

Write down the expression.

** y=x–;**

** z=–y;**

If the expression contains post operator remove the post and write down the expression again.

** y=x;**

Then assign priority.

** y=x;**

Finally, update the post values into variables (memory block).

**Binary Operators:**

Binary operators are those operators that work with two operands. A Binary Operator in C is an operator that takes two operands in an expression or a statement. Here these + & – operators will not indicate the sign of operand but add or subtract two operands, because these are binary operators like 3+5, 3–5.

Types of Binary Operators are:

**Arithmetic operators****Logical operators****Relational operators****Bit-wise operators****Assignment operators**

**Arithmetic operators:**

These operators are used to perform arithmetic/mathematical operation and give us results accordingly.

**+ (Plus) –** Give sum as a result. Example: 3+2=5

**– (Minus)** – Give the difference as a result. Example: 4-2=2

*** (Asterisk)** – Used for multiplication and give the product as a result. Example: 4*2=8

**/ (Forward slash)** – Used for division and give quotient as a result. Example: 4/2=2

**% (modulus)** – Give the remainder as a result. Example: 7%2=1

#include<stdio.h> int main() { int a = 11, b = 4; // Declare and initialize variable a and b printf("a + b = %d\n", a + b); printf("a - b = %d\n", a - b); printf("a * b = %d\n", a * b); printf("a / b = %d\n", a / b); // because both operands are integer result will be an integer printf("a %% b = %d\n", a % b); // % operator returns the remainder of 11/4 i.e 3 // Signal to operating system everything works fine return 0; }

**Output:**

** **

**Logical operators:**

Logical Operators are operators that determine the relation between 2 or more operands and return a specific output as a result of that relation. They had usually worked with Boolean (true/false) values.

**NOT (!) –** Used to negate a Boolean statement.

**Example**: NOT (!)

If A=1 and B=0, It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.

!(A && B) is true.

**AND (&&) and OR (||)** – Used to combine simple relational statements into more complex Expression.

**Example: **AND (&&)

If A=1 and B=0, If both the operands are non-zero, then the condition becomes true.

(A && B) is false.

**Example: OR (||)**

If A=1 and B=0, If any of the two operands are non-zero, then the condition becomes true.

(A || B) is true.

**Program:**

#include <stdio.h> main() { int a = 5; int b = 20; int c ; if ( a && b ) { printf("Line 1 - Condition is true\n" ); } if ( a || b ) { printf("Line 2 - Condition is true\n" ); } /* lets change the value of a and b */ a = 0; b = 10; if ( a && b ) { printf("Line 3 - Condition is true\n" ); } else { printf("Line 3 - Condition is not true\n" ); } if ( !(a && b) ) { printf("Line 4 - Condition is true\n" ); } }

**Output:**

**Relational Operator:**

These Operators are used to check the relationship between the two operands. If the relation is true, it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision making and loops. Programming language like C which doesn’t support Boolean data type return result as 1 or 0

Here 1->True and 0->False

**Greater than (>**) – Returns true when left operand value is greater than right operand value. Example: 5 > 3 is evaluated to 0.

**Less than (<)** – Returns true when left operand value is less than the right operand value. Example: 5 < 3 is evaluated to 0.

**Greater than or equal to (>=)** – Return true when left operand value is greater than or equal to the right operand. Example: 5 >= 3 is evaluated to 0.

**Less than or equal to (<=) **– Return true when left operand value is less than or equals to the right operand. Example: 5 <= 3 is evaluated to 0.

**Equal to (==):** – Returns true when left operand value is equal to right operand value. Example: 5 == 3 is evaluated to 0.

**Not Equal to (!=): **– Return true when left operand value is not equal to right operand. Example: 5 != 3 is evaluated to 0.

**Program:**

#include<stdio.h> int main() { int x = 12, y = 13; printf("x = %d\n", x); printf("y = %d\n\n", y); // Is x is greater than y? printf("x > y : %d\n", x > y); // Is x is greater than or equal to y? printf("x >= y : %d\n", x >= y); // Is x is smaller than y? printf("x < y : %d\n", x < y); // Is x is smaller than or equal to y? printf("x <= y : %d\n", x <= y); // Is x is equal to y? printf("x == y : %d\n", x == y); // Is x is not equal to y? printf("x != y : %d\n", x != y); // Signal to operating system everything works fine return 0; }

**Output:**

**Bitwise Operator:**

Bitwise Operators are used to performing bitwise operations on bit patterns or binary numerals that involve the manipulation of individual bits. Bitwise operators always evaluate both operands. Bitwise operators work on bits and perform bit by bit operations.

**& (AND): **Example: a & b

**|(OR):** Example: a | b

**^(Exclusive OR (XOR)): **Example: a ^ b

**~(One’s Complement (NOT)): **Example: ~a

**>> (Shift Right): **Example: a >> 1

**<< (Shift Left): **Example: a << 1

**Program:**

#include<stdio.h> int main( ) { int a=9 , b=65; printf("Bitwise AND Operator a & b =%d\n", a & b); printf("Bitwise OR Operator a | b =%d\n", a | b); printf("Bitwise XOR Operator a ^ b =%d\n", a ^ b); printf("Bitwise NOT Operator ~a =%d\n", ~ a); printf("SHIFT RIGHT Operator a >> 1 =%d\n", b >> 1); printf("SHIFT LEFT Operator a << 1 =%d\n", a << 1); return 0; }

**Output:**

**Assignment Operator:**

**Assignment Operators are **the operator which are used to assign a new value to a variable. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.

**=** : Simple assignment operator. Assigns values from right side operands to left side operand. Example: C = A + B will assign the value of A + B to C

**+=** :Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand. Example: C += A is equivalent to C = C + A

**-+**: Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. Example: C -= A is equivalent to C = C – A

***=**: Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand. Example: C *= A is equivalent to C = C * A

**/=** :Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. Example: C /= A is equivalent to C = C / A

**%=**: Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. Example: C %= A is equivalent to C = C % A

**Program:**

#include<stdio.h> int main( ) { int a=5, c; c = a; // c is 5 printf(“c = %d \n” , c); c += a; // c is 10 printf(“c = %d \n” , c); c -= a; // c is 5 printf(“c = %d \n” , c); c *= a; // c is 25 printf(“c = %d \n” , c); c /= a; // c is 5 printf(“c = %d \n” , c); c %= a; // c = 0 printf(“c = %d \n” , c); return 0; }

**Output:**

**Ternary Operator or Conditional Operator:**

**This **is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison. If it helps you can think of the operator as a shortened way of writing an if-else statement**.**

**?:** Ternary or Conditional Operator. Example: (a>b)? c : d ;

(a>b) is a condition where if the condition is true the variable “**c**” in the operator which acts as an operand will be executed if not Operand “**d”** will be executed.

**Program:**

#include <stdio.h> main() { int a , b; a = 10; printf( "Value of b is %d\n", (a == 1) ? 20: 30 ); printf( "Value of b is %d\n", (a == 10) ? 20: 30 ); }

**Output:**

In the next article, I am going to discuss **Variables in C** in detail. Here, in this article, I try to explain **Operators in C** Program in detail with examples and I hope you enjoy this article.