Operators in C

C programming language has a rich set of built-in operatorsAn operator is a symbol that instructs the computer to perform certain mathematical or logical operations. It is used in a program as an expression to manipulate data and variables.

The C operators can be classified into several different categories:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • Increment and decrement Operators
  • Conditional Operators
  • Bitwise Operators
  • Special Operators

Arithmetic Operators

C programming language provides arithmetic operations on data and variable.

Operator

Use Example
+ Addition

a+b

Substraction a-b
* Multiplication

x*y

/

Division a/b
% Modulo division

n%2

Sample code:

#include <stdio.h>
int main()
{
  int a = 10;
  int b = 20;
  int c ;
  c = a + b;
  printf("Sum of a and b is %d\n", c );
   return 0;
}

Output:

Sum of a and b is 30

Relational Operators

This operator is used to compare two quantities. And depending on the relation of these quantities, we make certain decisions. Like if we compare the price of two items, we can use this operator.

We can use symbols for comparison as listed below:

Operator

Meaning Example
< is less than

a<b

<=

is less than or equal to i<=6
> is greater than

x>y

>=

is greater than or equal to j>=8
== is equal to

i==j

!=

is not equal to

n!=4

Logical Operators Relational expressions are used in decision-making statements like if and while for decision making during the execution of the program

In the C programming language, there are three types of logical operators:

  • && meaning logical AND
  • II meaning logical OR
  • ! meaning logical NOT

The logical operators && and II are used to test more than one condition and make decisions.

Example: 

a > b && x==10

Here, two relational expressions are combined to form a logical or compound relational expression. In a single relational expression, a logical expression yields value of one or zero, whereas in this example the above expression is true only if a>b is true and x==10 is true. If either or both of the expressions are false, the whole expression becomes false.

Assignment Operators

This operator is used to assign a value or the result of an expression to a variable.

Example:

x += y+1;

This is same as:

x = x + (y+1);

The shorthand operator += means ‘add y+1 to x’.

For y=2, the above statement becomes:

x += 3;

After the statement is executed, 3 is added to x.

Some commonly used shorthand assignment operators are listed below:

Simple assignment operator

Shorthand operator

a = a + 1

a +=1
a = a – 1

a -=1

a = a * (n+1)

a *=n+1

a = a / (n+1)

a /=n+1
a = a  % b

a %=b

Increment and decrement Operators

These two operators are unique to the C programming language. They are not generally found in other languages.

++ (Increment Operator)

— (Decrement Operator)

Increment Operator

Increment operator is used in incrementing the value of a variable before or after using it in an expression. E.g. ++a or a++.

Decrement Operator

Decrement operator is used in decrementing the value of a variable before or after using it in an expression. E.g. –a or a–.

The operator ++ adds 1 to the operand, while – – subtracts 1.

It takes the following form when written with an operand:

++a; or a++;

–a; or a–;

Here, ++a; is equivalent to a = a+1;

–a; is equivalent to a = a-1;

Increment and decrement operators are mostly used in for and while loop.

Sample code:

#include <stdio.h>
int main()
{
    int i;
    for(i=1;i<10;i++)
    {
        printf("%d ",i);
    }
    return 0;
}

Output:

1 2 3 4 5 6 7 8 9

In line 5 of the above code, the increment operator is used in for loop. The i++ increment operator adds 1 to the operand i after each iteration.

Rules for using increment and decrement operators:

  • They are unary operators and they require variables as their operand.
  • When prefix ++(or –) is used, the variable is incremented(or decremented) first and then the expression is evaluated by the new value of the variable.
  • When postfix ++(or –) is used, the variable is first evaluated using the original value and the variable is incremented(or decremented).
  • The precedence and associativity of increment and decrement operators are same as unary + and unary –.

Conditional Operators

This operator is used to construct conditional expressions in a program.

It has the form:

exp1 ? exp2 : exp3

The operator ? : works as follows:

  • exp1 is evaluated first.
  • If it is true, exp2 is evaluated and becomes the value of the expression.
  • If it is false, exp3 is evaluated and becomes the value of the expression.

Example:

a = 10;

b = 15;

x = (a > b) ? a : b;

In the above example, x will be assigned the value of b.

Bitwise Operators

This operator is used to manipulate data at bit level. It is used for testing the bits, or shifting them left to right.

The meaning of the bitwise operators are given in the table below:

Operators

Meaning Example
& bitwise AND

A & B

I

bitwise OR A I B
^ bitwise exclusive OR

A^B

<<

shift left A<<B
>> shift right

A>>B

Special Operators

C programming language supports few more special operators such as comma operator, sizeof operator, pointer operator (& and *) and member selection operators(. And ->).

Comma operator

It is used when we have to assign multiple values in a variable using a comma ‘,’.

E.g. x=1,2,3;

Sizeof operator

It is used to compute the size of its operand. It can be used to find the size of a variable of data type integer, float, character, etc.

E.g. sizeof(int),sizeof(char),sizeof(a),sizeof(float), etc.

Pointer operator

When we use a pointer variable to store the address of another variable, we also use “Address of” (&) operator and “Value at address” (*) operators to perform different operations on it.

“Address of” (&) operator is used to access the address of a variable through a pointer.

“Value at address” (*) operator is used to access the value of a variable through a pointer.

Member selection operators

Member selection operators are used to access the members of their operands.

E.g. x.y,x->y,x[i],*a,&n