In C Programming, operators are symbols or keywords used to perform operations on values and variables. These are fundamental to performing various operations in C programming and are essential for building complex algorithms and programs. We can use operators to perform a wide range of tasks, including arithmetic calculations, logical operations, and comparisons.

Table of Contents

1) Assignment Operators ( =, +=, -=, *=, /=, %= )

The assignment operator is the single equals sign (=).

The assignment operator copies the value from its right hand side to the variable on its left hand side. The assignment also acts as an expression which returns the newly assigned value. Some programmers will use that feature to write things like the following.

Other Assignment Operators (>>=, <<=, &=, |= and ^=)

In addition to the plain = operator, C includes many shorthand operators which represents variations on the basic =. For example “+=” adds the right hand side to the left hand side. x = x + 10; can be reduced to x += 10;. This is most useful if x is a long expression such as the following, and in some cases it may run a little faster.

Here’s the list of assignment shorthand operators…

2) Arithmetic Operators (+, -, /, * and %)

C includes the usual binary and unary arithmetic operators. These are used to perform basic arithmetic operations such as addition (+), subtraction (-), multiplication (*), division (/), and remainder after division (%) modulo division.

Personally, I just use parenthesis liberally to avoid any bugs due to a misunderstanding of precedence. The operators are sensitive to the type of the operands. So division (/) with two integer arguments will do integer division. If either argument is a float, it does floating point division. So (6/4) evaluates to 1 while (6/4.0) evaluates to 1.5 — the 6 is promoted to 6.0 before the division.

3) Unary Increment or Decrement Operators (++ and –)

The unary ++ and — operators increment or decrements the value in a variable. There are “pre” and “post” variants for both operators which do slightly different things (explained below)

4) Relational or Comparison Operators (==, !=, >, <, >= and <=)

These operate on integer or floating point values and return a 0 or 1 boolean value to show the comparisons between two operands.

Using both assignment (=) and comparison (==) operators together

An absolutely classic pitfall is to write assignment (=) when you mean comparison (==). This would not be such a problem, except the incorrect assignment version compiles fine because the compiler assumes you mean to use the value returned by the assignment. This is rarely what you want

This does not test if x is 3. This sets x to the value 3, and then returns the 3 to the if for testing. 3 is not 0, so it counts as “true” every time. This is probably the single most common error made by beginning C programmers.

The problem is that the compiler is no help — it thinks both forms are fine, so the only defense is extreme vigilance when coding.

5) Logical Operators ( !, && and ||)

In Logical operators in C, the value 0 is false, anything else is true. The operators evaluate left to right and stop as soon as the truth or falsity of the expression can be deduced. (Such operators are called “short circuiting”) In ANSI C, these are furthermore guaranteed to use 1 to represent true, and not just some random non-zero bit pattern.

6) Bit-wise Operators (~, &, |, ^, >> and <<)

C includes operators to manipulate memory at the bit level. This is useful for writing low level hardware or operating system code where the ordinary abstractions of numbers, characters, pointers, etc… are insufficient — an increasingly rare need. Bit manipulation code tends to be less “portable”. Code is “portable” if with no programmer intervention it compiles and runs correctly on different types of computers. The bit-wise operations are typically used with unsigned types. In particular, the shift operations are guaranteed to shift 0 bits into the newly vacated positions when used on unsigned values.

Do not confuse the Bit-wise operators with the logical operators. The bit-wise connectives are one character wide (&, |) while the Boolean connectives are two characters wide (&&, ||). The bit-wise operators have higher precedence than the Boolean operators. The compiler will never help you out with a type error if you use & when you meant &&. As far as the type checker is concerned, they are identical– they both take and produce integers since there is no distinct Boolean type.

7) Pointer Operators (*, & and ->)

In C, pointer operators are operators that are used in conjunction with pointers. There are several pointer operators in C, including:

  1. Address-of operator (&): Returns the memory address of a variable. When used with a variable, the & operator returns the address of the variable in memory.
  2. Dereference operator (*): Accesses the value stored at a memory address. When used with a pointer, the * operator returns the value stored at the address pointed to by the pointer.
  3. Indirection operator (->): Accesses a member of a structure through a pointer. The -> operator is used to access members of a structure when a pointer to the structure is used.

8) Conditional Operators (? :)

The conditional operator (also known as the ternary operator ) is a shorthand way of writing an if statement. It allows you to conditionally execute an expression based on the value of a condition. The syntax of the ternary operator is as follows:

9) Comma Operator ( , )

The comma operator , in C is used to separate expressions. It evaluates each expression from left to right and returns the value of the rightmost expression. It is often used in places where multiple expressions are syntactically allowed but only one is expected.

For example in the following example, the expressions a++ and b++ are evaluated first and then the expression a + b is evaluated, and the result is assigned to the variable c.

Operators Diagram (Infographics)

The following diagram shows a list of operators and their categories used in C.

C/C++ Operators
C/C++ Operators

If you want to see these operators in action, look at the following C Programs: