Skip to content

C Operators

An introduction to C Operators and Operator precedence

C offers us a wide variety of operators that we can use to operate on data.

In particular, we can identify various groups of operators:

In this blog post I’m going to detail all of them, using 2 imaginary variables a and b as examples.

I am keeping bitwise operators, structure operators and pointer operators out of this list, as I will dedicate them a specific blog post.

Arithmetic operators

In this macro group I am going to separate binary operators and unary operators.

Binary operators work using two operands:

OperatorNameExample
=Assignmenta = b
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Moduloa % b

Unary operators only take one operand:

OperatorNameExample
+Unary plus+a
-Unary minus-a
++Incrementa++ or ++a
--Decrementa-- or --a

The difference between a++ and ++a is that a++ increments the a variable after using it. ++a increments the a variable before using it.

For example:

int a = 2;
int b;
b = a++ /* b is 2, a is 3 */
b = ++a /* b is 4, a is 4 */

The same applies to the decrement operator.

Comparison operators

OperatorNameExample
==Equal operatora == b
!=Not equal operatora != b
>Bigger thana > b
<Less thana < b
>=Bigger than or equal toa >= b
<=Less than or equal toa <= b

Logical operators

Those operators are great when working with boolean values.

Compound assignment operators

Those operators are useful to perform an assignment and at the same time perform an arithmetic operation:

OperatorNameExample
+=Addition assignmenta += b
-=Subtraction assignmenta -= b
*=Multiplication assignmenta *= b
/=Division assignmenta /= b
%=Modulo assignmenta %= b

Miscellaneous operators

The ternary operator

The ternary operator is the only operator in C that works with 3 operands, and it’s a short way to express conditionals.

This is how it looks:

<condition> ? <expression> : <expression>

Example:

a ? b : c

If a is evaluated to true, then the b statement is executed, otherwise c is.

The ternary operator is functionality-wise same as an if/else conditional, except it is shorter to express and it can be inlined into an expression.

sizeof

The sizeof operator returns the size of the operand you pass. You can pass a variable, or even a type.

Example usage:

#include <stdio.h>

int main(void) {
  int age = 37;
  printf("%ld\n", sizeof(age));
  printf("%ld", sizeof(int));
}

Operator precedence

With all those operators (and more, which I haven’t covered in this post, including bitwise, structure operators and pointer operators), we must pay attention when using them together in a single expression.

Suppose we have this operation:

int a = 2;
int b = 4;
int c = b + a * a / b - a;

What’s the value of c? Do we get the addition being executed before the multiplication and the division?

There is a set of rules that help us solving this puzzle.

In order from less precedence to more precedence, we have:

Operators also have an associativity rule, which is always left to right except for the unary operators and the assignment.

In:

int c = b + a * a / b - a;

We first execute a * a / b, which due to being left-to-right we can separate into a * a and the result / b: 2 * 2 = 4, 4 / 4 = 1.

Then we can perform the sum and the subtraction: 4 + 1 - 2. The value of c is 3.

In all cases, however, I want to make sure you realize you can use parentheses to make any similar expression easier to read and comprehend.

Parentheses have higher priority over anything else.

The above example expression can be rewritten as:

int c = b + ((a * a) / b) - a;

and we don’t have to think about it that much.


→ Get my C Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing [email protected]. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about clang: