Operators in C programming: A Complete Tutorial

# Operators in C programming: A Complete Tutorial

Updated on Oct 3, 2023 11:48 IST

Here we will explore operators in C programming. Also, you will learn about classification of operators with the help of examples.

Being a C programming learner always requires practicing the programs as much as possible, as it will boost programmers to learn more and more in programming. The best way to kick-start your programming journey is to start writing basic mathematical programs to learn deeply about tokens, keywords, and functions.  Here we will learn about operators in C programming in detail.

Also, Explore Data Types in C.

Check out: C Programming Online Courses & Certifications

### Table of Content

1. Operators in C programming
2. Classification of Operators
3. Operator precedence in C

Explore programming courses

## Operators in C programming

Some special symbols used to perform specific operations like arithmetic, logical, relational etc are known as operators.

These symbols are used between variables to compute mathematical and logical computations. An operator operates on a variable. Let’s understand this with an example

Example

z= x-y;

Here in this example symbol “-” is a subtraction operator that subtracts y from x and stores the evaluated value in the variable z. Here “=” is an assignment operator. x and y are two operands.

Check out: Understanding the Difference Between Structure and Union in C

## Classification of Operators

Operators in c programming are mainly classified into two types that are unary and binary operators, other than that more operators are used which we will discuss later.

## Unary Operators

The operator which operates on a single operand is called a unary operator. Examples-

• Increment operator
• Decrement operator
• size of operator

Yoy can also explore: Understanding Data Structures in C: Types And Operations

## Binary Operators

Operators who operate on two or more than two operands are called binary operators.

• logical operator (&&,||,!)
• Bitwise operator (&,|,<<,>>,~,^)
• Arithmetic operator (+,-,/,*,%)
• Relational operator(<,>,>=,<=,==,!=)
• Assignment operator (=,+=,-=,/=,*=,%=)

You must explore: Difference Between C and C++

## Unary Operators

### Increment operator

An increment operator is a unary operator that operates only on one operand. Increment operators increase the value of a constant or variable by one.

` `
`#include <stdio.h>int main(){ int x = 12; float y = 12.5; printf("++x = %d \n", ++x); printf("++y = %f \n", ++y); return 0;}Copy code`

Output-

```++x= 13
++y=13.50000
```

### Decrement Operator

The decrement operator is a unary operator that operates only on one operand. Decrement operators decrease the value of a constant or variable by one.

` `
```#include <stdio.h>int main(){ int x = 12; float y = 12.5; printf("--x = %d \n", --x); printf("--y = %f \n", --y); return 0;}Copy code```

Output

``` --x=11
--y=11.5
```

### sizeof operator

the sizeof operator is a unary operator and operates only on a single operand. sizeof() operator is used in many ways, in this program, we use it with data type to know the allocated memory of that data type.

` `
`#include <stdio.h>int main(){ printf("%lu\n", sizeof(unsigned)); printf("%lu\n", sizeof(int)); printf("%lu\n", sizeof(float)); printf("%lu", sizeof(signed)); return 0;}Copy code`

Output

```4
4
4
4
```

You can also explore: Implementing arrays in C programming

## Binary Operators

### Logical Operator

If an expression contains any logical operator in it, it will always return 0 or 1 based on the result true or false of that expression.

Three types of logical operators, AND, OR and NOT, are commonly used in decision-making in c programming.

C program to demonstrate the use of logical AND operator

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

Output –

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

C program to demonstrate the use of logical OR operator

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

Output –

```!(x != y)  is 1
!(x == y) is 0
```

### Bitwise Operator

Bitwise operators perform operations on bit patterns at the binary level. These operations are performed on single bits.

Bitwise operators perform operations on bit patterns at the binary level. These operations are performed on single bits.

### Arithmetic operator

Arithmetic operators are used to perform mathematical operations on two or more operands. To perform arithmetic functions 5 operators (*,-,+,/,%) are used in c programming.

C program to demonstrate the subtraction operation

` `
```#include<stdio.h>#include<conio.h> int main(){ int x, y, sub; printf("Enter the value of x: "); scanf("%d", &x); printf("Enter the value of y: "); scanf("%d", &y); sub = x - y; printf("Subtraction of %d and %d is %d.",x,y, sub); return 0;}Copy code```

Output-

```Enter the value of x: 6
Enter the value of y: 2
Subtraction of 6 and 2 is 4.
```

This operation is used to add two data items.

C program to demonstrate the Addition operation

` `
```#include<stdio.h>#include<conio.h> int main(){ int x, y, add; printf("Enter the value of x: "); scanf("%d", &x); printf("Enter the value of y: "); scanf("%d", &y); add = x + y; printf("Sum of %d and %d is %d.",x,y, add); return 0;}Copy code```

Output-

```Enter the value of x: 5
Enter the value of y: 6
Sum of 5 and 6 is 11.
```

You can also explore: Storage classes in C

### Division operation (/)

Division operator is use to divide one element from other.

C program to demonstrate the Division operation

` `
```#include<stdio.h>#include<conio.h> int main(){ int x, y, Division; printf("Enter the value of x: "); scanf("%d", &x); printf("Enter the value of y: "); scanf("%d", &y); Division = x/y; printf("Division of %d and %d is %d.",x,y, Division); return 0;}Copy code```

Output-

` `
`Enter the value of x: 16Enter the value of y: 8Division of 16 and 8 is 2.Copy code`

You can also explore: What is Do While Loop in C?

### Multiplication operation (*)

To multiply one element to another, this operator is used.

C program to demonstrate the multiplication operation

` `
```#include<stdio.h>#include<conio.h> int main(){ int x, y, product; printf("Enter the value of x: "); scanf("%d", &x); printf("Enter the value of y: "); scanf("%d", &y); product = x*y; printf("product of %d and %d is %d.",x,y, product); return 0;}Copy code```

Output

```Enter the value of x: 5
Enter the value of y: 6
product of 5 and 6 is 30.
```

### Modulus operation (%)

Modulo operators return the reminder of an element when it is divided by another element.

C program to demonstrate the Modulus operation

` `
```#include<stdio.h>#include<conio.h> int main(){ int x, y, mod; printf("Enter the value of x: "); scanf("%d", &x); printf("Enter the value of y: "); scanf("%d", &y); mod= x%y; printf("remainder %d", mod); return 0;}Copy code```

Output-

```Enter the value of x: 15
Enter the value of y: 7
remainder 1
```
Prime Number in C: A Beginner’s Guide
A prime number is a whole number that is divided by 1, and the number itself has only two factors. Non-prime numbers are called composite numbers like 4, 6, 9,...read more
Factorial Programs in C – A Step-by-Step Guide
In this article, we will discuss how to calculate the factorial programming in c using three different methods: For Loops, Recursion, and While Loop. In this article, we will discuss...read more
File Handling in C
This article deals with File Handling in C and will answer your query regarding is its usage.This concept is cleared with programming example in C.

### Relational Operator

As it is clear from its name relational operator focuses on the relation between the two operands; if the relation is true, it returns 1, and if the relation is false, it returns 0. The below table depicts the relational operator symbols and their description.

C program to demonstrate the use of less than/ greater than operator

` `
```#include <stdio.h>int main(){ int x = 10, y = 10, z = 25; printf("%d > %d is %d \n", x, y, x > y); printf("%d > %d is %d \n", x, z, x > z); printf("%d < %d is %d \n", x, y, x < y); printf("%d < %d is %d \n", x, z, x < z); return 0;}Copy code```

Output

```10 > 10 is 0
10 > 25 is 0
10 < 10 is 0
10 < 25 is 1
```

C program to demonstrate the use of equal to/not equal to operator

` `
```#include <stdio.h>int main(){ int x = 10, y = 10, z = 25; printf("%d == %d is %d \n", x, y, x == y); printf("%d == %d is %d \n", x, z, x == z); printf("%d != %d is %d \n", x, y, x != y); printf("%d != %d is %d \n", x, z, x != z); return 0;}Copy code```

Output

` `
`10 == 10 is 1 10 == 25 is 0 10 != 10 is 0 10 != 25 is 1Copy code`

C program to demonstrate the use of greater than or equal to/less than or equal to operator

` `
```#include <stdio.h>int main(){ int x = 10, y = 10, z = 25; printf("%d >= %d is %d \n", x, y, x >= y); printf("%d >= %d is %d \n", x, z, x >= z); printf("%d <= %d is %d \n", x, y, x <= y); printf("%d <= %d is %d \n", x, z, x <= z); return 0;}Copy code```

Output

```10 >= 10 is 1
10 >= 25 is 0
10 <= 10 is 1
10 <= 25 is 1
```

### Assignment Operator

The name of this operator clearly defines its work, the assignment operator is used for assigning a value to a variable. The assignment operator and its example are given below in this table.

Here, x+=y is same as x=x+y

C program to demonstrate the use of assignment operator

` `
`#include <stdio.h>int main(){ int x = 25, y; y = x; printf("y = %d\n", y); y += x; printf("y = %d\n", y); y -= x; printf("y = %d\n", y); y*= x; printf("y = %d\n", y); y /= x; printf("y = %d\n", y); y %= x; printf("y = %d\n", y);return 0;}Copy code`

Output

```y = 25
y = 50
y = 25
y = 625
y = 25
y = 0
```

## Operators Precedence in C

Operator precedence decides how a mathematical expression is evaluated. Let’s understand it with an example of a mathematical equation.

a= 17+3*20

Here in this equation, the value of “a” will be evaluated as 77, not 400, because the precedence of the multiplication operator is higher than the addition operator. In the below table, precedence increases from top to bottom.

### Conclusion

Practicing makes a man perfect if a learner wants to master the mother of all languages, that is C, then, it’s mandatory to know deeply about the basic programs of operators in C. In a nutshell, this article will aid and support every individual’s c learning journey.

Contributed By: Srashti Soni

## FAQs

Q1 What is operator precedence in c?

Answer-Operator precedence decides how a mathematical expression is evaluated.

Q2 What is the use of the bitwise operator in c programming?

Answer- Bitwise operators perform operations on bit patterns at the binary level.

Q3 What is the ++ operator in C?

Answer- ++ (the increment operator) increase the value of the operand by 1.

Q4 What is the binary operator?

Answer- Those operators that operate on two or more operands are called binary operators.

Q5 What is a unary operator in C?

Answer- Those operators that operate on one operand are called binary operators.

Q6 Are addition and multiplication equal in precedence?

Q7 Which prefix has the higher precedence?