C Operators

Summary: in this tutorial, you will learn how to use C operators including arithmetic, assignment, relational, bit-wise, increment, logical and ternary operators.

C supports many operators to perform various kinds of operations. With C operators, you can do arithmetic operations, comparing data, modifying variables, combining relationship logically, etc.

C operators operates on one or more operands to produce a value.

  • The operators that take one operand are called unary operators.
  • The operators that require two operands are called binary operators.
  • The operator that accepts three operands is called ternary operator. C  has only one ternary operator.

C Arithmetic Operators

C supports almost common arithmetic operators such as +,-,*, / and modulus operator %. The modulus operator (%) returns the remainder of integer division calculation. Note that the modulus operator cannot be applied to a double or float.

C Arithmetic Operators Precedence

The C arithmetic operators have precedence rules that are similar to the rules in math. The + and – operators have the same precedence. The precedence of *, / and % operators are higher than the + and – operators. The C arithmetic operators associate from left to right.

The following program demonstrates the C arithmetic operators:

Below is the output of the demo program:

C Assignment Operators

C assignment operators are used to assign the value of a variable or expression to another variable. The syntax of assignment operators is as follows:

Besides the assignment operator, C also supports another short hand format that works the same assignment operator with an additional operator such as +=, -=, *=, /=, %=.

Each assignment operator has a priority. And they are evaluated from right to left based on its priority. The following is the assignment operator and its priority: =, +=, -=, *=, /=, %=.

A simple C program that demonstrates assignment operators:

Here is the output:

C Bitwise Operators

C provides six bitwise operators for manipulating bit. The bitwise operator can only be applied to integral operands such as char, short, int and long. The following table illustrates C bitwise operators:

C Bitwise OperatorsDescription
&Bitwise AND
|Bitwise inclusive OR
^Bitwise exclusive OR
<<Bitwise left shift
>>Bitwise right shift
~one’s complement

The bitwise operators are preferred in some contexts because bitwise operations are faster than (+) and (-) operations and significantly faster than (*) and (/) operations.

Example of C bitwise Operators

Here is a simple program that demonstrates C bitwise operators:

And here is the output:

C Increment Operator

C provides two operators for incrementing and decrementing the value of variables.

  • The increment operator ++ adds 1 to its operand.
  • The decrement operator — subtracts 1 from its operand

The C increment and decrement operators can be used either as prefix operator or postfix operators as follows:

The C increment operator in both prefix or postfix contexts is to add 1 to a variable. But the expression ++variable increments variable before its value is used, whereas variable++ increments variable after its value has been used.

Example of C Increment Operator

Let’s take a look at a simple example that demonstrates prefix and postfix of C increment operators.

Note that in both cases x is 11 however y is 11 in the first case and 19 in the second case.

So in the case that the variable value is not used, there is no difference between prefix or postfix of the increment operator.

C Logical Operators

You use C logical operators to connect expressions and/or variables to form compound conditions. The C logical expression returns an integer (int). The result has value 1 if the expression is evaluated to true otherwise it returns 0. C uses the following symbols for the Boolean operations AND, OR, and NOT.

&&logical AND
||logical OR
!logical NOT
The AND operator (&&) and OR operator (||) has the essential peculiarity:
  • The || operator, their operands are evaluated in order from left to right, and if the value of the left operand is sufficient to determine the result of the operation, then the right operand is not evaluated at all.
  • The operator && evaluates the right operand only if the left operand yields 1;

The following table illustrates the truth table of the logical operators:


Example of C Logical Operators

The following program illustrates the C logical operators:

Below is the output of the C logical operators demo program:

C Relational Operators

C provides you a list of relational operators to allows you to compare data. The relational operators enable you to check whether two variables or expressions are equal, not equal, which one is greater or less than other…etc. The relational operators are used in Boolean conditions or expressions to return 0 or 1.The following table illustrates the relational operators in C:

Relational OperatorsDescription
==Equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
!=Not equal to

C relational operators example

The following program demonstrates the relational operators in C:

Here is the output of the demo program:

C Ternary Operator

C ternary operator is also referred as a conditional operator or trinary operator because:

  • It is only the operator that has three operands.
  • It is a shorthand of combination of the if-else and returns statement.

The syntax of C ternary operator is as follows:

First C evaluates the condition. Based on the return value of the condition, then it evaluates the second or third operand:

  • If the condition is evaluated to true (1), only the second operand (expression 1) is evaluated. Then the operator returns the value of the expression 1.
  • If the condition is evaluated to false (0), only the third operand (expression 2) is evaluated. The value of the expression 2 is returned.

The ternary operator can be rewritten as the if-else and return statement as follows:

Example of C Ternary Operator

In this example, we are going to write an inline function that uses C ternary operator to find the minimum number between two integers.

  • Was this tutorial helpful ?
  • YesNo