Operaters in C

This post will teach you about operaters in C, how they are used and why they are important.

Johnson Tang

Operators are symbols that tell the computer to perform operations on data. There are many types of operators in C, however, they are mostly quite intuitive. Firstly, there are arithmetic operators. These are the operators that you would use to perform arithmetic operations. Below is a table of the arithmetic operators in C.

Operator Description
+ Adds ints, floats, doubles and longs
- Minuses ints, floats, doubles and longs
* Multiplies ints, floats, doubles and longs
/ Divides ints, floats, double and longs
% Named modulo operator, outputs remainder of division of two numbers. (5 % 2 = 1)
++ This operator increments the value of a variable by 1.
-- This operator decrements the value of a variable by 1.

Now we have learnt the arithmetic operators, we can move on to the relational operators. These operators are used to compare two values. These values return Booleans, meaning true (1) or false (0). These help us find comparisons and then allows us to make decisions based on these comparisons.

Operator Description
== Checks if values are equal
!= Checks if values are NOT equal
> Checks if first value is greater than the second (9>3)
< Checks if first value is lesser than the
% Named modulo operator, outputs remainder of division of two numbers. (5 % 2 = 1)

The last two operators have variants where you can add an equal symbol (=) after the operater to check for greater than/lesser than OR equal to. This is important to note as if the two values are equal the comparison operation will return true. Next there are assignment operaters. These operators are used to assign values to variables.

Operator Description
+= Adds a specified number to the variable (x+=7 is the same as x = x + 7)
-= Minuses a specified number to the variable (x-=5 is the same as x = x - 5)
*= Multiplies a specified number to the variable (x*=2 is the same as x = x * 2)
/= Divids a specified number to the variable (x/=4 is the same as x = x/4)
%= Applies modulo with a specified number to a variable (x%=3 is the same as x = x % 3)

There are a few more of these assignment operators, however they are a little bit more advanced and may be difficult for beginners to understand. If you are curious, you can check out this link from w3schools, where you can try these operators out. There are also logical operators. These operators are used to find the logic between two values.

Operator Description
&& Logical AND operator (returns 1 (true) if both values are true e.g. if x = 3, x > 1 %% x < 5, this would return true)
|| Logical OR operator (returns 1 (true) if ONE value are true e.g. if x = 3, x < 1 || x < 5, this would return true)
! Logical NOT operator (returns 1 (true) if logic is not true e.g. x = 3, !(x = 3) would return true as x does equal 3, however the logic is reversed from the operator

The amount of operaters you have just been introduced to may be a little bit daunting, however for this section, it is recommended just to play around with these operators. Assign numbers to variables with these operators, do arithmetic with the stored values, the possibilities are endless.