An operator is a symbol that tells the compiler to execute a calculation, logic, etc., operation. For instance, when the addition operator, +, is placed between two numerical values , 10 and 24, it is 24+10. Its concept is the act of adding these two values , and the result of this is 34. The values that the operators operate on are called operands.
In fact, operators are factors that can be used to perform arithmetic, logical, comparison, bit, byte, etc. operations on numbers, variables, arrays, strings, etc. Operators exist in all programming languages and are part of the main parts of a programming language. C language is very strong in terms of internal structure and has powerful operators by default. Operators in C language are divided into several categories in the following.
Arithmetic operators perform arithmetic operations on operands. The following table shows all arithmetic operators supported by the C language.
Operator | Name | Description | Example |
+ | Addition | Adds together two values | x + y |
– | Subtraction | Subtracts one value from another | x – y |
* | Multiplication | Multiplies two values | x * y |
/ | Division | Divides one value by another | x / y |
% | Modulus | Returns the division remainder | x % y |
++ | Increment | Increases the value of a variable by 1 | ++x |
— | Decrement | Decreases the value of a variable by 1 | –x |
Bitwise operators operate on bits and perform operations bit by bit. The truth table for the operators are & (AND), | (OR), ^ (XOR), ~(One’s compliment), <<(left shift), and >>(right shift) respectively:
Operator | Name | Description | Example |
& | Bitwise And | Adds together two values | x & y |
| | Bitwise Or | Subtracts one value from another | x | y |
^ | Bitwise Xor | Multiplies two values | x ^ y |
~ | Binary One’s compliment | Reverses every bit | ~x |
<< | Left shift | Shift every bit to the left by a | x << a |
>> | Right shift | Shift every bit to the right by a | x >> a |
These operators are a combination of the equality operator and other operators. For example, consider the operator +=; if it is written as x += a, it is equal to x = x+a; That is, every time it adds the variable x with the variable a and puts it inside the variable x.
The same is true for other combinatorial operators. The following table shows all the compound assignment operators that the C language supports:
Operator | Example | Same As |
---|---|---|
= | x = 6 | x = 6 |
+= | x += 6 | x = x + 6 |
-= | x -= 6 | x = x – 6 |
*= | x *= 6 | x = x * 6 |
/= | x /= 6 | x = x / 6 |
%= | x %= 6 | x = x % 6 |
&= | x &= 6 | x = x & 6 |
|= | x |= 6 | x = x | 6 |
^= | x ^= 6 | x = x ^ 6 |
>>= | x >>= 6 | x = x >> 6 |
<<= | x <<= 6 | x = x << 6 |
Comparison operators specify the relationship between operands:
Operator | Name | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Logical operators operate on logical expressions. Logical expressions have both true and false values and are used when a condition needs to be checked. In C language, an incorrect value is specified by zero, and a correct value by non-zero values.
The following table shows the logical operators supported by the C programming language:
Operator | Name | Description | Example |
---|---|---|---|
&& | Logical and | Returns true if both statements are true | x < 5 && x < 10 |
|| | Logical or | Returns true if one of the statements is true | x < 5 || x < 4 |
! | Logical not | Reverse the result, and returns false if the result is true | !(x < 5 && x < 10) |
In this section, we introduce some special operators. For this reason, these operators are special, and the way they are used is slightly different.
In an equation or a line of code, if there are multiple parentheses, priority is always given to the innermost parentheses. Look at the following example:
y = (x + (a / (3 + g) )) * 2;
If it is necessary to perform several operations in one line, the comma operator (,) is used. Look at the following example:
y = (x = 2, x * 7);
In the above relationship, first, the variable x is equal to 2, and then it is multiplied by 7. Then it is placed in the Y variable.
This operator is a conditional assignment and performs the assignment operation from two values according to whether a condition is true or not. This operator is also called compressed if. How to define it is as follows:
variable = condition ? first value : second value;
Now consider the following example:
int x = 6, y;
y = (x > 5) ? (x += 2) : (x += 3);
This operator determines the length of a variable or data type in bytes. You can see how to use this operator below:
int x, y;
float m;
x = sizeof(y);
y = sizeof(m);
This operator, called ampersand, returns the address of a variable. For example, &a gives the address of variable a. We used this operator in the pointers section.
This operator, called asterisk, is used both in defining the pointer and in obtaining the content of the address pointed to. In a complete example, we will check the special operators. Pay attention to the following example:
#include <stdio.h>
main(){
int a = 4;
short b;
double c;
int* ptr;
/* example of sizeof operator */
printf("Line 1 - Size of variable a = %d\n", sizeof(a) );
printf("Line 2 - Size of variable b = %d\n", sizeof(b) );
printf("Line 3 - Size of variable c= %d\n", sizeof(c) );
/* example of & and * operators */
ptr = &a;
/* 'ptr now contains the address of 'a */
printf("value of a is %d\n", a);
printf("*ptr is %d.\n", *ptr);
/* example of ternary operator */
a = 10;
b = (a == 1) ? 20: 30;
printf( "Value of b is %d\n", b );
b = (a == 10) ? 20: 30;
printf( "Value of b is %d\n", b );
}
The words between /* */ are comments that you can learn in the comment lesson. Moreover, ptr is a pointer we will discuss in the C pointer lesson. After compiling and running the above program, the following results are obtained:
value of a is 4
*ptr is 4.
Value of b is 30
Value of b is 20
According to the above description, try to check the correctness of the code results first manually and then by the compiler.