Search Result
Details here...

C Operators | Basics

Previous

Basic Input/Output in C | printf and scanf

Next

Type Conversion in C


An operator is a symbol that is used to perform an operation on one or more operands. Without operators, we cannot perform any operation on our data.

Operators in C

The C programming language has many operators that are used to perform operations on data. They can be classified into groups:

  • Arithmetic operators
  • Assignment operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Other operators

The following table lists the operators in C programming language.

Classification Operator Description
Arithmetic operators + Addition
- Subtraction
* Multiplication
/ Division
% Modulus division
++ Increment by 1
-- Decrement by 1
Assignment Operators = Assignment
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign
%= Modulus and assign
Relational Operators == Equality
!= Inequality
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Logical Operators && Logical AND
|| Logical OR
! Logical NOT
Bitwise Operators & Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
<< Left Shift
>> Right Shift
Other Operators & Address of operator
* Pointer Operator
sizeof() Returns the size of a variable
?: Conditional Operator (ternary operator or shorthand if-else)

Arithmetic Operators and Example

Arithmetic operators are used to perform the Mathematical operations on operands.

Let's have a look at an example

C

#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int add = a + b;
int sub = a - b;
int mul = a * b;
int div = a / b;
int mod = a % b;
printf("Addition of a and b is %d\n", add);
printf("Subtraction of a and b is %d\n", sub);
printf("Multiplication of a and b is %d\n", mul);
printf("Division of a and b is %d\n", div);
printf("Modulus of a and b is %d\n", mod);
printf("Pre-increment of a is %d\n", ++a);
printf("Pre-decrement of a is %d\n", --a);
printf("Post-increment of a is %d\n", a++);
printf("Now, a is post incremented to %d\n", a);
printf("Post-decrement of a is %d\n", a--);
printf("Now, a is post decremented to %d\n", a);
return 0;
}

Output

Addition of a and b is 30
Subtraction of a and b is -10
Multiplication of a and b is 200
Division of a and b is 0
Modulus of a and b is 10
Pre-increment of a is 11
Pre-decrement of a is 10
Post-increment of a is 10
Now, a is post incremented to 11
Post-decrement of a is 11
Now, a is post decremented to 10

Did you notice?

Division of a and b is 0, but it must have been 0.5? Isn't it? Well, this is not an error. It is a special case of integer division. It is called integer division because it is the division between integers and the result is rounded down to the nearest integer. For example, if a is 10 and b is 3, then the result of division is 3 (not 3.333). If you want to generate and store a floating point result, then you can use a float variable, and convert any of the operands to a floating point number using the type conversion.

In the above code, the div variable could have been declared as a float/double variable for better precision, and any of the operands could have been converted to a float/double variable.
float div = (float) a / b;
Then, for a = 10, and b = 20, the div variable would be 0.500000.

Didn't get it?

Don't worry, we will deal in detail about the type conversion in the coming chapters.

Explanation

The above code demonstrates the use of the arithmetic operators in C. +, -, * and / are used for addition, subtraction, multiplication and division respectively. % is used for modulus division, which is the remainder of the division of two numbers.

The ++ and -- operators are used for increment and decrement respectively. They operate only one variable at a time by incrementing or decrementing the value of the variable by 1. They are also used as postfix (a++ or a--) and prefix (++a or --a) increment and decrement respectively.

The prefix increment operator is used to increment the value of the variable by 1 and and then returns the value of the variable. Whereas, the postfix increment operator is used to return the value of the variable and then increments the value of the variable by 1.

It is the same with the prefix decrement operator and the postfix decrement operator.

Remember

The increment and decrement operators used as prefix and postfix are different, not same. However, in some compilers they are treated as same.

Assignment Operators and Example

Assignment operators are used to assign values to variables. We have already seen the assignment operator =.

Let's have a look at an example

C

#include <stdio.h>
int main()
{
int a = 10;
printf("a is assigned the value %d\n", a);
a += 20;
printf("a is added to 20 and assigned the value %d\n", a);
// a += 20 is same as a = a + 20
a -= 20;
printf("20 is subtracted from a and a is assigned the value %d\n", a);
// a -= 20 is same as a = a - 20
a *= 2;
printf("a is multiplied by 2 and assigned the value %d\n", a);
// a *= 2 is same as a = a * 2
a /= 2;
printf("a is divided by 2 and assigned the value %d\n", a);
// a /= 2 is same as a = a / 2
a %= 2;
printf("a is modulus divided by 2 and assigned the value %d\n", a);
// a %= 2 is same as a = a % 2
return 0;
}

Output

a is assigned the value 10
a is added to 20 and assigned the value 30
20 is subtracted from a and a is assigned the value 10
a is multiplied by 2 and assigned the value 20
a is divided by 2 and assigned the value 10
a is modulus divided by 2 and assigned the value 0

Explanation

The assignment operators are used to assign values to variables. In the above example, initially, a is assigned the value 10.
a = 10

Then, a is added to 20 and assigned the value 30.
a = 30

Then, 20 is subtracted from a and a is assigned the value 10.
a = 10

Then, a is multiplied by 2 and assigned the value 20.
a = 20

Then, a is divided by 2 and assigned the value 10.
a = 10

Then, a is modulus divided by 2 and assigned the value 0, as the reminder is 0.
a = 0

Relational Operators and Example

Relational operators are used to compare values of two variables. The result of the comparison is either true or false. It is true, if the condition is satisfied and false, if the condition is not satisfied.

Let's have a look at an example

C

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

Output

The result of a==b is 0
The result of a!=b is 1
The result of a<b is 1
The result of a>b is 0
The result of a<=b is 1
The result of a>=b is 0

Explanation

Here, the relational operators are used to compare the values of a and b. The integer result of the comparison is either 0 or 1. 0 is returned if the condition is not satisfied and 1 is returned if the condition is satisfied.

Logical Operators and Example

Logical operators are used to do logical operations on one or more variables. The result of the logical operation is either true or false. It is true if the condition is satisfied and false if the condition is not satisfied.


  • AND (&&) Operator

    A && operator is used to check if both the operands are true. If both the operands are true, then the result is true. If one of the operands is false, then the result is false. It can be verified by the truth table.

    Variable A Variable B Result = A && B
    0 0 0
    0 1 0
    1 0 0
    1 1 1

    In C programming language, it can be interpreted as follows:

    0 && 0 // It results to 0
    0 && 1 // It results to 0
    1 && 0 // It results to 0
    1 && 1 // It results to 1
  • OR (||) Operator

    An || operator is used to check if one of the operands is true. If one of the operands is true, then the result is true. If both the operands are false, then the result is false. It can be verified by the truth table.

    Variable A Variable B Result = A || B
    0 0 0
    0 1 1
    1 0 1
    1 1 1

    In C programming language, it can be interpreted as follows:

    0 || 0 // It results to 0
    0 || 1 // It results to 1
    1 || 0 // It results to 1
    1 || 1 // It results to 1
  • NOT (!) Operator

    A ! operator is used to check if the operand is false. If the operand is false, then the result is true. If the operand is true, then the result is false. It can be verified by the truth table.

    Variable A Result = !A
    0 1
    1 0

    In C programming language, it can be interpreted as follows:

    !0 // It results to 1
    !1 // It results to 0

Let's have a look at an example

C

#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int c = a > b;
int d = a < b;
printf("The result of a AND b is %d\n", a&&b);
printf("The result of a AND 0 is %d\n", a&&0);
printf("The result of a AND 1 is %d\n", a&&1);
printf("The result of a OR b is %d\n", a||b);
printf("The result of a OR 0 is %d\n", a||0);
printf("The result of a OR 1 is %d\n", a||1);
printf("The result of NOT a is %d\n", !a);
printf("The result of NOT 0 is %d\n", !0);
printf("The result of NOT 1 is %d\n", !1);
printf("The result of c AND d is %d\n", c&&d);
printf("The result of c OR d is %d\n", c||d);
return 0;
}

Output

The result of a AND b is 1
The result of a AND 0 is 0
The result of a AND 1 is 1
The result of a OR b is 1
The result of a OR 0 is 1
The result of a OR 1 is 1
The result of NOT a is 0
The result of NOT 0 is 1
The result of NOT 1 is 0
The result of c AND d is 0
The result of c OR d is 1

Explanation

The first output is the result of a AND b. This is 1 because a is 10 and b is 20, as a and b are both non-zero.

The second output is the result of a AND 0. From the truth table of AND, it is clearly 0. The third output is the result of a AND 1. This results to a, which is non-zero and hence evaluates to 1.

The fourth output is the result of a OR b. This is 1 because a is non-zero and b is non-zero. The fifth and sixth output is 1 because a is non-zero.

The seventh output is the result of NOT a. This is 0 because a is non-zero. The eighth output is the result of NOT 0. This is 1 because the complement of 0 is 1. The ninth output is the result of NOT 1. This is 0 because 1 is non-zero.

The tenth output is the result of c AND d. Here, c is 0 and d is 1. So, the result is 0. The eleventh output is 1 because c is 0 and d is non-zero.

Bitwise Operators and Example

Bitwise operators perform the operation on each bit of the operands.


  • Bitwise AND (&) Operator

    The bitwise AND operator (&) performs a bitwise AND operation on each bit of the operands.
    The result is 1 if both bits are 1, otherwise it is 0.

    6 = 0110 (in binary)
    5 = 0101 (in binary)

    0110
    & 0101
    --------
    0100 = 4 (in decimal)
                        
This can be implemented using the following code:

C

#include <stdio.h>
int main()
{
int a = 6, b = 5;
printf("The result of a & b is %d\n", a&b);
return 0;
}

Output

The result of a & b is 4

  • Bitwise OR (|) Operator

    The bitwise OR operator (|) performs a bitwise OR operation on each bit of the operands.
    The result is 1 if either of the bits is 1, otherwise it is 0.

    6 = 0110 (in binary)
    5 = 0101 (in binary)

    0110
    | 0101
    --------
    0111 = 7 (in decimal)
                        
This can be implemented using the following code:

C

#include <stdio.h>
int main()
{
int a = 6, b = 5;
printf("The result of a | b is %d\n", a|b);
return 0;
}

Output

The result of a | b is 7

  • Bitwise XOR (^) Operator

    The bitwise XOR operator (^) performs a bitwise XOR operation on each bit of the operands.
    The result is 1 only if the two bits are opposite, otherwise it is 0. Hence, it is a odd selector operator.

    6 = 0110 (in binary)
    5 = 0101 (in binary)

    0110
    ^ 0101
    --------
    0011 = 3 (in decimal)
                        
This can be implemented using the following code:

C

#include <stdio.h>
int main()
{
int a = 6, b = 5;
printf("The result of a ^ b is %d\n", a^b);
return 0;
}

Output

The result of a ^ b is 3

  • Bitwise NOT (~) Operator

    The bitwise NOT operator (~) performs a bitwise NOT operation on each bit of the operand.
    The result is the complement of the operand. However, the result may be a negative number if the result is stored in a signed variable. This is because the negative numbers are stored in 2's complement form where the leftmost bit is the sign bit. Also, if the result is stored in an unsigned variable, the result will be a large positive number.

    0110 = 6 (in binary)

    ~ 0110
    --------
    1001 = 9 (in decimal)
    
    0110 (1's complement)
    +  1 (adding 1 for 2's complement)
    --------
    0111 = 7 (in decimal)
                        

    Here, 0 is added to the leftmost bit to make the number negative. Hence, the result is -7.
This can be implemented using the following code:

C

#include <stdio.h>
int main()
{
int a = 6;
printf("The result of ~a is %d\n", ~a);
return 0;
}

Output

The result of ~a is -7

A trick

The result of bitwise NOT (~) operator on N is -(N + 1). So, for 6, it is -(6+1) = -7.


  • Left Shift Operator (<<)

    The left shift operator (<<) performs a bitwise left shift operation on each bit of the operand and the number of times specified by the second operand. A zero is added to the vacant bits.

    0110 = 6 (in binary)

    0110
    << 1
    --------
    1100 = 12 (in decimal)
                        
This can be implemented using the following code:

C

#include <stdio.h>
int main()
{
int a = 6;
printf("The result of a << 1 is %d\n", a<<1);
return 0;
}

Output

The result of a << 1 is 12

Did you notice?

If the operand is shifted to left bitwise by 1, the result is the operand multiplied by 2.

  • Right Shift Operator (>>)

    The right shift operator (>>) performs a bitwise right shift operation on each bit of the operand and the number of times specified by the second operand. A zero is added to the vacant bits.

    0110 = 6 (in binary)

    0110
    >> 1
    --------
    0011 = 3 (in decimal)
                            
This can be implemented using the following code:

C

#include <stdio.h>
int main()
{
int a = 6;
printf("The result of a >> 1 is %d\n", a>>1);
return 0;
}

Output

The result of a >> 1 is 3

Did you notice?

If the operand is shifted to right bitwise by 1, the result is the operand divided by 2.

The other operators, the associativity and precedence of the operators will be explained when we revisit the operators.
By darpan.kattel
Previous

Basic Input/Output in C | printf and scanf

Next

Type Conversion in C