Operators in C

In this tutorial, you will learn about different operators in C programming.

An operator is a symbol that instructs a computer to perform certain operations. Operators are typically used as part of mathematical or logical expressions. C supports a rich set of operators to perform various operations.

C Arithmetic Operators

An arithmetic operator performs mathematical operators such as addition, subtraction etc. C supports all basic arithmetic operators. The list of all arithmetic operators in C is listed in the following table.

OperatorMeaningExample
+Addition or Unary plus8 + 2 = 10
Subtraction or Unary minus8 – 2 =6
*Multiplication8 * 2 = 16
/Division8 / 2 = 4
%Modulo division13 % 2 = 1

The unary plus and unary minus operators are used to specify the sign of a number. The modulo division (%) operator produces the remainder of an integer division. For example `10 % 3 = 1`.

The variables in which the operation is performed is called operand. That is, in `a + b`, a and b are operands and `+` is the operator.

When the operands in a single arithmetic expression are both integers, then the operation is known as integer arithmetic. Integer arithmetic always produces an integer value as output. All the examples in the above table are integer arithmetic. For example:` 12 / 10 = 1`

Real arithmetic refers to arithmetic operations that will use only real operands. The operator % cannot be used with real operands. For example: `3.0 / 2.0 = 1.5`

Mixed-mode arithmetic expressions are those in which one of the operands is real and the other is an integer. Since one of the operands is of real type, then the result is always a real number. For example: `12 / 10.0 = 1.2`

Example: Arithmetic operators
```#include <stdio.h>
int main()
{
int a = 8,b = 2, c;

c = a+b;
printf("Sum = %d \n",c);
c = a-b;
printf("Difference = %d \n",c);
c = a*b;
printf("Product = %d \n",c);
c = a/b;
printf("Quotient = %d \n",c);
c = a%b;
printf("Remainder after division = %d \n",c);

return 0;
}```

Output

```Sum = 10
Difference = 6
Product = 16
Quotient = 4
Remainder after division = 0```

C Relational Operators

The relationship operators are used to check the relationship between two operands. The value of a relational operator is either 1 (true) or 0 (false). The list of relational operators supported in C is given in the table. Suppose a = 6 and b = 2.

OperatorMeaningExample
<is less thana < b is true.
<=is less than or equal toa <= b is true
>is greatera > b is false
>=is grater than an equal toa >= b is false
==is equal toa == b is false
!-is not equal toa != b is true

Note that the arithmetic operators have a higher priority over relational operators. For example, in the expression` 10 < 7 + 5` , 7 + 5 is evaluated first and the result 12 is compared with 10.

Relational statements are generally used in decision-making statements and loops.

Example: Relational operators
```#include <stdio.h>
int main()
{
int a = 5, b = 4;

printf("%d == %d is %d \n", a, b, a == b);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d <= %d is %d \n", a, b, a <= b);

return 0;
}```

Output

```5 == 4 is 0
5 > 4 is 1
5 < 4 is 0
5 != 4 is 1
5 >= 4 is 1
5 <= 4 is 0 ```

C Logical Operators

The logical operators are used to make a decision by testing one or more conditions. The three logical operators supported in C are:

• `&&` – logical AND – True only if all operands are true.
• `||` – logical OR – True only if either one operand is true.
• `!` – logical NOT – True only if the operand is 0

The value of a logical operator is also one or zero according to the truth table given below:

ABA && BA || B!A
00001
01011
10010
11110

C Assignment Operator

Assignment operators are used to assigning values to a variable. The assignment operator in c is `=`. C also has a set of shorthand assignment operators as shown in the table:

OperatorExampleSame as
+=a += ba = a + b
-=a -= ba = a – b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b

C Increment and Decrement Operator

In C, the operators `++` and `--` are called the increment and decrement operators and are used to change the value of an operand by 1.

The increment operator `++` increases the value of an operand by 1 and the decrement operator `--` decreases the value of an operand by 1. They both are unary operators.

These operators can be used as prefixes like `++a` and can be used as postfixes like `a++`. You can learn the difference in increment and decrement operators when used as prefix and postfix here.

C Bitwise Operator

For manipulation of data at bit level, C supports special operators known as bitwise operators. Bit wise operators and their meaning is given in the following table:

OperatorMeaning
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR
`Bitwise complement
<<Shift left
>>Shift right

C Conditional Operator

C has a special ternary operator `?:` called conditional operator. The conditional operator behaves similarly to the ‘if-else’ statement. The syntax of a conditional operator is as follows:

`condition ? true-statement : false-statement;`

Here `expression1` is a boolean condition that can be either true or false. The conditional operator will return `expression2`, if the value of `expression1` is true. Similarly, if the value of `expression1` is false, then the operator will return `expression3`. For example, consider the following example:

`max = (a > b) ? a : b ;`

First, the expression a > b is evaluated. If it is true, the statement will be equivalent to `max = a;`. If `a > b` is false, then the statement will be equivalent to `max = b`.

Special Operators

C also support some special operators such as comma operator, sizeof operator, pointer operators, etc.

The Comma Operator

The comma operator is used to link the related expression together. For example:

`float a, b, c;`

The sizeof Operator

The sizeof operator return the number of bytes a operand occupies. It can be used to know the size of variables, constants, arrays, structures etc. For example:

`x = sizeof(mark);`

Other operators

The reference operator` &` and deference operator `*` is used in pointer operations. The member selection operator` . `and `->` are used to select members of a structure. These operators will be discussed in later tutorials.

Subscribe
Notify of