Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

C Operators (with Live Examples)

The C language supports a rich set of built-in operators. An operator is a symbol that tells the compiler to perform a certain mathematical or logical operations, based on the values provided to the operator.

Operators are used in programs to manipulate data and variables.

Before moving forward with Operators in C language, we recommend you learn about C variables and datatypes:

C Operators

C operators can be classified into the following types:

  • Arithmetic operators

  • Relational operators

  • Logical operators

  • Bitwise operators

  • Assignment operators

  • Conditional operators

  • Special operators

Let's understand each one of these operator types, one by one with working code examples.

What is an Operand?

An operand is a value on which any operator works. For example, when we say 4+5, here, numbers 4 and 5 are operands whereas + is an operator.

Different operators work with different numbers of operands like the + operator requires two operands or values.

C Arithmetic Operators

The C language supports all the basic arithmetic operators such as addition, subtraction, multiplication, division, etc.

The following table shows all the basic arithmetic operators along with their descriptions.

Operator Description

Example

(where a and b are variables with some integer value)

+ adds two operands (values) a+b
- subtract second operands from first a-b
* multiply two operands a*b
/ divide numerator by the denominator, i.e. divide the operand on the left side with the operand on the right side a/b
% This is the modulus operator, it returns the remainder of the division of two operands as the result a%b
++ This is the Increment operator - increases integer value by one. This operator needs only a single operand. a++ or ++a
-- This is the Decrement operator - decreases integer value by one. This operator needs only a single operand. --b or b--

To learn in what order the arithemtic operators are executed, visit C Operator Precedence.

Example: Basic Arithmetic Operators

Let's see a code example to understand the use of the basic arithmetic operators in C programs.

#include <stdio.h>

int main() {

    int a = 50, b = 23, result;

    // addition
    result = a+b;
    printf("Addition of a & b = %d \n",result);

    // subtraction
    result = a-b;
    printf("Subtraction of a & b = %d \n",result);

    // multiplication
    result = a*b;
    printf("Multiplication of a & b = %d \n",result);

    // division
    result = a/b;
    printf("Division of a & b = %d \n",result);

    return 0;

}


Addition of a & b = 73
Subtraction of a & b = 27
Multiplication of a & b = 1150
Division of a & b = 2

Run Code →

Example: Using Modulus Operator (%)

The modulus operator returns the remainder value after the division of the provided values.

#include <stdio.h>

int main() {

   int a = 23, b = 20, result;

   // Using Modulus operator
   result = a%b;

   printf("result = %d",result);

   return 0;

}


result = 3

Run Code →

Example: Using Increment and Decrement Operators

The increment operator is used to increase the value of any numeric value by 1, whereas the decrement operator is used to decrease the value of any numeric value by 1.

#include <stdio.h>

int main() {

   int a = 10, b = 20, c, d;

   /* 
      Using increment operator
   */
   printf("Incrementing value of a = %d \n", ++a);

   /* 
      Using decrement operator
   */
   printf("Decrementing value of b = %d \n", --b);

   // first print value of a, then decrement a
   printf("Decrementing value of a = %d \n", a--);
   printf("Value of a = %d \n", a);

   // first print value of b, then increment b
   printf("Incrementing value of b = %d \n", b++);
   printf("Value of b = %d \n", b);

   return 0;

}


Incrementing value of a = 11
Decrementing value of b = 19
Decrementing value of a = 11
Value of a = 10
Incrementing value of b = 19
Value of b = 20

Run Code →

In the code example above, we have used the increment operator as ++a and b++, while the decrement operator as --b and a--.

When we use the increment and decrement operator as a prefix (means before the operand), then first the increment operation is done and that value is used, like in the first two printf() functions, we get the updated values of a and b.

Whereas when we use the increment and decrement operators as postfix (means after the operand), then first the larger expression is evaluated which is printf() in this case and then the value of the operand is updated.

C Relational operators

The relational operators (or comparison operators) are used to check the relationship between two operands. It checks whether two operands are equal or not equal or less than or greater than, etc.

It returns 1 if the relationship checks pass, otherwise, it returns 0.

For example, if we have two numbers 14 and 7, if we say 14 is greater than 7, this is true, hence this check will return 1 as the result with relationship operators. On the other hand, if we say 14 is less than 7, this is false, hence it will return 0.

The following table shows all relational operators supported in the C language.

Operator Description

Example

(a and b, where a = 10 and b = 11)

== Check if two operands are equal a == b, returns 0
!= Check if two operands are not equal. a != b, returns 1 because a is not equal to b
> Check if the operand on the left is greater than the operand on the right a > b, returns 0
< Check operand on the left is smaller than the right operand a < b, returns 1
>= check left operand is greater than or equal to the right operand a >= b, returns 0
<= Check if the operand on left is smaller than or equal to the right operand a <= b, returns 1

To learn in what order the relational operators are executed, visit C Operator Precedence.

Example: Relational Operators

When we use relational operators, then based on the result of the comparison done, if it's true, then the output is 1 and if it's false, then the output is 0. We will see the same in the example below.

#include <stdio.h>

int main() {

   int a = 10, b = 20, result;

   // Equal
   result = (a==b);
   printf("(a == b) = %d \n",result);

   // less than
   result = (a<b);
   printf("(a < b) = %d \n",result);

   // greater than
   result = (a>b);
   printf("(a > b) = %d \n",result);

   // less than equal to
   result = (a<=b);
   printf("(a <= b) = %d \n",result);

   return 0;

}


(a == b) = 0
(a < b) = 1
(a > b) = 0
(a <= b) = 1

Run Code →

In the code example above, a has value 10, and b has value 20, and then different comparisons are done between them.

In the C language, true is any value other than zero. And false is zero.

C Logical Operators

C language supports the following 3 logical operators.

Operator Description

Example

(a and b, where a = 1 and b = 0)

&& Logical AND a && b, returns 0
|| Logical OR a || b, returns 1
! Logical NOT !a, returns 0

These operators are used to perform logical operations and used with conditional statements like C if-else statements.

  1. With AND operator, only if both operands are true, the result is true.

  2. With the OR operator, if a single operand is true, then the result will be true.

  3. The NOT operator changes true to false, and false to true.

Example: Logical Operators

In the code example below, we have used the logical operators.

#include <stdio.h>

int main() {

   int a = 1, b = 0, result;

   // And
   result = (a && b);
   printf("a && b = %d \n",result);

   // Or
   result = (a || b);
   printf("a || b = %d \n",result);

   // Not
   result = !a;
   printf("!a = %d \n",result);

   return 0;

}


(a && b) = 0
(a || b) = 1
(!a) = 0

Run Code →

Bitwise Operators in C

Bitwise operators perform manipulations of data at the bit level. These operators also perform the shifting of bits from right to left. Bitwise operators are not applied to float or double, long double, void, etc.(Learn about C float and double datatype).

The following table contains the bitwise operators. There are 6 bitwise operators in the C language.

Operator Description Example
& Bitwise AND  
| Bitwise OR  
^ Bitwise Exclusive OR (XOR)  
~ One's complement (NOT)  
>> Shift right  
<< Shift left  

The bitwise AND, OR, and NOT operator works the same way as the Logical AND, OR, and NOT operators, except that the bitwise operators work bit by bit.

Below we have a truth table for showing how these operators work with different values.

a b a & b a | b a ^ b
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Bitwise operators can produce any arbitrary value as result. It is not mandatory that the result will either be 0 or 1.

Bitwise >> and << operators

The bitwise shift operator shifts the bit value, either to the left or right. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value have to be shifted. Both operands have the same precedence.

Understand, how bits shift from left to right and vice versa.

a = 00010000
b = 2
a << b = 01000000 
a >> b = 00000100

In case of a << b, 2 bits are shifted to left in 00010000 and additional zeros are added to the opposite end, that is right, hence the value becomes 01000000

And for a >> b, 2 bits are shifted from the right, hence two zeros are removed from the right and two are added on the left, hence the value becomes 00000100

Please note, shift doesn't work like rotating, which means, the bits shifted are not added at the other end. The bits that are shifted are lost.

Bitwise One's Complement (~) Operator

The one's complement operator, will change all the 1's in the operand to 0, and all the 0's are set to 1.

For example, if the orginal byte is 00101100, then after one's complement it will become 11010011.

Example: Bitwise Left & Right shift Operators

Let's see an example to understand the bitwise operators in C programs.

#include <stdio.h>

int main() {

   int a = 0001000, b = 2, result;

   // <<
   result = a<<b;
   printf("a << b = %d \n",result);

   // >>
   result = a>>b;
   printf("a >> b = %d \n",result);

   return 0;

}


a << b = 2048
a >> b = 128

Run Code →

C Assignment Operators

The sssignment operators are used to assign value to a variable. For example, if we want to assign a value 10 to a variable x then we can do this by using the assignment operator like: x = 10; Here, = (equal to) operator is used to assign the value.

In the C language, the = (equal to) operator is used for assignment however it has several other variants such as +=, -= to combine two operations in a single statement.

You can see all the assignment operators in the table given below.

Operator Description

Example

(a and b are two variables, with where a=10 and b=5)

= assigns values from right side operand to left side operand a=b, a gets value 5
+= adds right operand to the left operand and assign the result to left operand a+=b, is same as a=a+b, value of a becomes 15
-= subtracts right operand from the left operand and assign the result to left operand a-=b, is same as a=a-b, value of a becomes 5
*= mutiply left operand with the right operand and assign the result to left operand a*=b, is same as a=a*b, value of a becomes 50
/= divides left operand with the right operand and assign the result to left operand a/=b, is same as a=a/b, value of a becomes 2
%= calculate modulus using two operands and assign the result to left operand a%=b, is same as a=a%b, value of a becomes 0

When we combine the arithmetic operators with the assignment operator =, then we get the shorthand form of all the arthmetic operators.

Example: Using Assignment Operators

Below we have a code example in which we have used all the different forms of assignment operator, starting from the basic assignment.

#include <stdio.h>

int main() {

   int a = 10;

   // Assign
   int result = a;
   printf("result = %d \n",result);

   // += operator
   result += a;
   printf("result = %d \n",result);

   // -= operator
   result -= a;
   printf("result = %d \n",result);

   // *= operator
   result *= a;
   printf("result = %d \n",result);

   return 0;

}


result = 10
result = 20
result = 10
result = 100

Run Code →

C Ternary Operator (?)

The ternary operator, also known as the conditional operators in the C language can be used for statements of the form if-then-else.

The basic syntax for using ternary operator is:

(Expression1)? Expression2 : Expression3;

Here is how it works:

  • The question mark ? in the syntax represents the if part.

  • The first expression (expression 1) returns either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3)

  • If (expression 1) returns true then the (expression 2) is executed.

  • If (expression 1) returns false then the expression on the right side of : i.e (expression 3) is executed.

Example: Using Ternary Operator

Here is a code example to show how to use ternary operator.

#include <stdio.h>

int main() {

   int a = 20, b = 20, result;

   /* Using ternary operator
      - If a == b then store a+b in result
      - otherwise store a-b in result
   */
   result = (a==b)?(a+b):(a-b);

   printf("result = %d",result);

   return 0;

}


result = 40

Run Code →

C Special Operator - &, *, sizeof, etc.

Apart from arithmetic, relational, logical, assignment, etc. operators, C language uses some other operator such as:

  1. sizeof operator

  2. & operator

  3. * operator

  4. The . (dot) and -> (arrow) operators

  5. [] operator, etc.

sizeof to find size of any entity(variable, array, etc.), & operator to find address of a variable, etc. You can see a list of such operators in the below table.

Operator Description Example
sizeof returns the size(length in bytes) of entity, for eg. a variable or an array, etc. sizeof(x) will return size of the variable x
& returns the memory address of the variable &x will return address of the variable x
* represents pointer to an object. The * operator returns the value stored at a memory address.

m = &x (memory address of variable x)

*m will return the value stored at memory address m

. (dot) operator used to access individual elements of a C structure or C union. If emp is a structure with an element int age in it, then emp.age will return the value of age.
-> (arrow) operator used to access structure or union elements using a pointer to structure or union. If p is a pointer to the emp structure, then we can access age element using p->age
[] operator used to access array elements using indexing if arr is an array, then we can access its values using arr[index], where index represents the array index starting from zero

We will learn about *, dot operator, arrow operator and [] operator as we move on in this tutorial series, for now let's see how to use the sizeof and & operators.

Example: Using sizeof and & Operators

Here is a code example, try running in the live code compiler using the Run code button.

#include <stdio.h>

int main() {

   int a = 20;
   char b = 'B';
   double c = 17349494.249324;

   // sizeof operator
   printf("Size of a is: %d \n", sizeof(a));
   printf("Size of b is: %d \n", sizeof(b));
   printf("Size of c is: %d \n", sizeof(c));

   // & operator
   printf("Memory address of a: %d \n", &a);

   return 0;

}


Size of a is: 4
Size of b is: 1
Size of c is: 8
Memory address of a: 1684270284

Run Code →


Frequently Asked Questions

Here are some frequently asked questions for C operators.

Q1. What are operators in C?

Operators are symbols known to the C compiler, which are used to perform operations on data. Operators can be used to perform operation directly on some value(C Literals) or on C variables. In the C language we can perform arithmetic operations, logical and relational operations, bitwise operations, etc. using the C operators on data.

Q2. What are the different types of operators C supports?

The C language supports the following type of operators:

  • Arithmetic operators

  • Relational operators

  • Logical operators

  • Bitwise operators

  • Assignment operators

  • Ternary operator

  • Special operators like sizeof, &, *, . (dot) operator, -> (arrow) operator, [] (square bracket) operator, etc.

Q3. What does * operator do in C?

The * operator in the C language is a unary operator that returns the value of the object located at the address, specified after the * operator. For example q = *m will store the value stored at memory address m in the q variable, if m contains a memory address.

The * operator is also used to perform multiplication of two values, where it acts as an arithmetic operator.

Q4. What does != mean in C?

It is a symbol of not equal to(!=) operator and used to check whether two values are not equal to each other or not. It is a relational operator and its opposite operator is an equal(==) operator which is used to check equality between two values or variables.

If two values are not equal, then we will get 1 as the result of the comparison.

Q5. What is & and * operators in C?

Both are special types of operators and are used to perform memory-related operations. The & operator is used to get the address of a variable and the * operator is the complement of the & operator and is used to get the value of the object for located at a memory address.

Q6. What does %d do in C?

It is a format specifier that is used to print formatted output to the console. In the C language, it is used with the printf() function(C Input Output) to display integer value to the console. To print float, C provides %f, for char we use %c, for double we use %lf, etc.


Conclusion:

In this tutorial, we learned all about the C language operators in detail along with a lot of code examples to see all the operators in action.