C: Operators

Alopix | Αλώπηξ
9 min readFeb 2, 2023

Without doubt operators are essential in programming. Operators are symbols that we use in order to determine specific actions. Here you can see a chart with all operators used in C.

Binary Operators

Assignment Operators

  • The ‘=’ operator is used in order to assign a value to a variable.
    Syntax:
variable = value;

Example:

num = 10;

Note: We can assign the same value to multiple variables by placing them one after the other by interfering ‘=’ symbols.

Example:

int num1, num2, age;
num1 = num2 = age = 43; //now all three variables are equal to 43

Note: The ‘=’ operator is applied from left to right and not the other way around. So, the line 43=num1 is WRONG.

Note: The type of the value that will be assigned must be the same type as the variable. If it’s not then the correct form of the value will be applied(for numerical data). For example:

int num1;
float num2;
num1 = num2 = 44.76
//the value of num1 will be 44 and the value of num2 will be 44.76
  • The ‘+=’ operator is used in order to assign a value equal to another but also increased by a certain value. This operator is used quite frequently in loops. It is important to note that in order to use this operator, the variable already must have a preassigned value in order to avoid an error.
    Syntax
data_type variable;
variable = value1;
variable += value2
//in the end, the value of the variable will be value1 + value2

Example:

int num;
num = 8;
num += 5;
//the value of num will be equal to 8+5=13
  • The ‘-=’ operator is the exact opposite of the ‘+=’. Instead of increasing the value of a variable, we decrease it by a certain value.
    Syntax:
data_type variable;
variable = value1;
variable -= value2
//in the end, the value of the variable will be value1 - value2
  • The ‘*=’ and ‘%=’ operators are no exception to the above principle. The ‘*=’ operator increases the value of a variable by the times of a certain value while on the other hand the ‘%=’ operator decreases the value of a variable by dividing it by a certain value.
    Syntax:
data_type variable;
variable = value1;
variable *= value2
//in the end, the value of the variable will be value1 * value2

data_type variable;
variable = value3;
variable %= value4
//in the end, the value of the variable will be value3 / value4

Arithmetic Operators

  • The ‘+’ operator is simply used for addition.
  • The ‘-’ operator is used for subtraction.
  • The ‘*’ operator is used for multiplication.
  • The ‘/’ operator is used for division (div).
  • The ‘%’ operator is used for the remainder of integer division(modulo).

Examples:

float num, value1, value2;
value1 = 68343.453
printf("Enter a number:");
scanf("%f", value2);
num = value1 + value2;
//now the value of num will be equal to 68343.453 + value2
float num, value1, value2;
value1 = 68343.453
printf("Enter a number:");
scanf("%f", value2);
num = value1 - value2;
//now the value of num will be equal to 68343.453 - value2
float num, value1, value2;
value1 = 68343.453
printf("Enter a number:");
scanf("%f", value2);
num = value1 * value2;
//now the value of num will be equal to 68343.453 * value2
float num, value1, value2;
value1 = 68343.453
printf("Enter a number:");
scanf("%f", value2); //the value2 must be unequal to 0
num = value1 / value2;
//now the value of num will be equal to 68343.453 div value2
int num, value1, value2; //we only use '%' on integers
value1 = 6834
printf("Enter a number:");
scanf("%d", value2); //the value2 must be unequal to 0
num = value1 % value2;
//now the value of num will be equal to 6834 mod value2

Relational Operators

  • The ‘==’ operator is used in conditions in order to check if a value is equal to another. In programming it is a very common and hard to find logical mistake since it is usually confused with ‘=’. The compiler does not recognize it as a mistake as it supposes we are trying to assign a variables value to another variable.
    Syntax:
if (value1 == vlaue2)
//if value1 is equal to value2 then the statement is True(1) otherwise it is False(0)
  • The ‘<’ and ‘>’ operators are used in order to determine if a value is greater or smaller than another. One can be used instead of the other just by swapping the position of the values we need to check.
    Syntax:
if(value1 > value2)
//if value1 is greater than value2 then the statement is True(1) otherwise it is False(0)
if(value1 < value2)
//if value1 is smaller than value2 then the statement is True(1) otherwise it is False(0)

Note: value1>value2 and value2>value1 are exactly the opposite conditions but expressed with the same operator.

  • The ‘>=’ and ‘<=’ are used in order to determine if a value is equal or greater than another or equal or smaller than another. The syntax is the exact same as the operators we just explained above and the same thing about swapping the values position applies here as well.
  • The ‘!=’ operator is used in order to check if a value is unequal to another. In most programming languages (including C) the syntax is ‘!=’ but in others you may also see it as ‘<>’.
    Syntax:
if(value1 != value2)
//if value1 is NOT equal to value2 then the statement is True(1) otherwise it is False(0)

Logical Operators

  • The ‘&&’ operator represents the logic relation of AND between two statements. In order for a complex statement to be True(1) when using the ‘&&’ operator, both statements must be True(1). In case even one of them is not true then the outcome is False(0).

False && False == False
False && True == False
True && True == True

Syntax:

if (statement1 && statement2)
//the condition above is True if both stetements are True at the same time

Example:

int value1, value2;
value1 = 10;
value2 = 34;
if (value1 > 0 && value2 > 0)
printf("Both numbers are positive!");
//the message will be displayed because both statements are true

Output:

Both numbers are positive!
  • The ‘||’ operator represents the logic relation OR between two statements. In order for a complex statement to be True(1) when using the ‘||’ operator, at least one of the statements must be True(1). The only chance the condition is False(0) is when both statements are False(0).

False || False == False
False || True == True
True || True == True

Syntax:

if (statement1 || statement2)
//the condition above is False if both stetements are False at the same time

Example:

int value1, value2;
value1 = 10;
value2 = 34;
if (value1 < 0 || value2 > 10)
printf("The statement is True!");
//the message will be displayed because one of the statements is true

Output:

The statement is True!
  • The ‘!’ operator represents the logic relation of NOT. This means that when the ‘!’ operator is used we get the exact opposite logical value of a statement. For example, if a value was True(1), when the ‘!’ operator is applied the value becomes False(0) and vice versa.

!False == True
!True == False

Syntax:

!statement

Example:

int value1, value2;
value1 = 10;
value2 = 34;
if (!(value1 < 0 || value2 > 10) == 1)
printf("The statement is True!");
//the message will not be displayed because the original statement is True
//but it becomes False(0) after the application of the ! operator.

Bit-Wise Operators
The bit-wise operators are used in order to handle the bits of integer variables/constants. To be more specific, the bit-wise operators take every bit of an integer and perform the desired logic action between them and the bits of another integer. The operators are the following:

  • [AND -> & ] Assigns the value of 1 only if both bits are one otherwise it assigns the value of 0.
    Example:

137 & 182

10001001 & 10110110

1 0 0 0 1 0 0 1
&&&&&&&&
1 0 1 1 0 1 1 0
— — — — — — —
1 0 0 0 0 0 0 0

  • [OR -> | ] Assigns the value of 0 only if both bits are 0 otherwise it assigns the value of 1.
    Example:

240 | 404
11110000 | 110010100
011110000
| | | | | | | |
110010100
— — — — — —
111110100

  • [NOT -> ~ ] Is only used on a single value and assigns the opposite value of a bit.
    Example:

~653
~1010001101
— — — — — — —
0101110010

  • [XOR -> ^ ] Performs the action of exclusive or and assigns the value of 1 only if the two bits are opposites otherwise it assigns the value of 0.
    Example:

112 ^ 164
1110000 ^ 10100100
0 1 1 1 0 0 0 0
^ ^ ^ ^ ^ ^ ^ ^
1 0 1 0 0 1 0 0
— — — — — — —
1 1 0 1 0 1 0 0

  • [LSHIFT -> << ] Let’s suppose we have two values, the value k and the value n. The << operator shifts the bits of k value by n positions to the left and fills with 0s the space left at the ‘end’ of the value.
    Example:

value = 140
shift_value = 3
new_value = value << shift_value
— — — — — — — — — — —
140 = 1000110
10001100 << 3
The first 3 digits are shifted (1000)
0110000 = 48
new_value = 48

Note: The Left Shift is the equivalent of binary multiplication.

  • [RSHIFT -> >> ] Same as the LSHIFT, the RSHIFT does the same thing with two values but instead of moving the bits to the left, it moves the bits to the right and fill with 0s the space left at the ‘front’ of the value.
    Example:

value = 78
shift_value = 2
new_value = value >> shift_value
— — — — — — — — — — —
78 = 1001110
1001110 << 2
The last 2 digits are shifted (10)
0010011 = 19
new_value = 19

Note: The Right Shift is the equivalent of binary division.

Unary Operators

  • The ‘++’ operator is used in order to increase an already existing value by a unit. It is most frequently used in loops. We can apply this operator either before or after a variable. The positioning plays an important role in the amplification of the value. To be precise, when the operator is used after a variable (++i) the value of the variable increases first and it is used right after in the appropriate condition. On the other hand, when the operator is placed after a variable (i++) then the current value of the variable is used in the appropriate condition and then it is raised by a unit.
    Syntax:
int a, b;
a++;
++b;

Example:

int i;
for (i = 0; i < 10; i++);
printf("This is a loop");
//this message will be displayed 10 times
  • The ‘--’ operator is used in order to decrease an already existing value by a unit. This operator follows the same rules as the ‘++’ operator.
    Syntax:
int a, b;
a--;
--b;

Example:

int count = 10;
printf("%d", count); //the number that will be displayed will be 10
count--; //this is equivelant to count = count -1;
printf("%d", count); //the number that will be displayed will be 9

Conditional Operator

As the name suggests, the conditional operator is similar to the if…else statement. Its use is to take less space when a simple condition is needed. It is not suggested for starter programmers to used this form of expression but more advanced programmers find it an easy and clean way to handle simple conditions without unnecessary code.
The syntax is relatively simple. The ‘?’ part stands for the if statement and the ‘:’ stands for the else. Let’s see how to use it.
Syntax:

variable = expression1?expression2:expression3

where...

varriable = (condition)?(value = value1):(value = value2)

is equal to...

if (condition){
value = value1;
}
else{
value = value2;
}

Example:

int value, num, value2, value2;
printf("Enter a number neg or pos:");
scanf("%d", num); //we ask the user for a number positive or negative
value1 = 1;
value2 = -1;
value = (num > 0)?(value = value1):(value = value2);
/*if the number is positive then we assign to the value the number 1
otherwise we assign it the value -1*/

Honorable Mention

The comma ‘ , ’ operator is one of the most frequent but silent operators in every programming language. It is used in order to separate variables or statements. We have seen this operator being used when we want to declare multiple variables of the same type. Also it is used in order to execute ‘chained’ commands. The only problem with the comma operator is that it usually creates a complexity in code.

In the next course we will discuss about Selection Structures.

If you enjoyed this article you can buy me a coffee, aka programmers fuel!

--

--

Alopix | Αλώπηξ

Informatics and Telecommunications | Tech and Space Enthusiast