C Operators

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

Arithmetic operators perform arithmetic operations on operands. The following table shows all arithmetic operators supported by the C language.

OperatorNameDescriptionExample
+AdditionAdds together two valuesx + y
SubtractionSubtracts one value from anotherx – y
*MultiplicationMultiplies two valuesx * y
/DivisionDivides one value by anotherx / y
%ModulusReturns the division remainderx % y
++IncrementIncreases the value of a variable by 1++x
DecrementDecreases the value of a variable by 1–x

Bitwise Operators

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:

OperatorNameDescriptionExample
&Bitwise AndAdds together two valuesx & y
|Bitwise OrSubtracts one value from anotherx | y
^Bitwise XorMultiplies two valuesx ^ y
~Binary One’s complimentReverses every bit~x
<<Left shiftShift every bit to the left by ax << a
>>Right shiftShift every bit to the right by ax >> a

Assignment Operators

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:

OperatorExampleSame As
=x = 6x = 6
+=x += 6x = x + 6
-=x -= 6x = x – 6
*=x *= 6x = x * 6
/=x /= 6x = x / 6
%=x %= 6x = x % 6
&=x &= 6x = x & 6
|=x |= 6x = x | 6
^=x ^= 6x = x ^ 6
>>=x >>= 6x = x >> 6
<<=x <<= 6x = x << 6

Comparison Operators

Comparison operators specify the relationship between operands:

OperatorNameExample
==Equal tox == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Logical Operators

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:

OperatorNameDescriptionExample
&& Logical andReturns true if both statements are truex < 5 &&  x < 10
|| Logical orReturns true if one of the statements is truex < 5 || x < 4
!Logical notReverse the result, and returns false if the result is true!(x < 5 && x < 10)

 Special operators

In this section, we introduce some special operators. For this reason, these operators are special, and the way they are used is slightly different.

Operator ()

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;
  1.  First, 3 + g is done.
  2. In the next step, (a/(3+g) is performed.
  3. Then ((x+(a/(3+g) is done.
  4. Finally, (x+(a/(3+g)))*2 will be done.

Operator ,

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.

Operator ?

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);
  1. If the value of the variable x is greater than 5, the variable x is added by 2, and if the value of the variable x is smaller than 5, it is added by 3 and finally assigned to y.
  2. because the value of x is equal to 6; So it is added by 2 and placed in y.

Sizeof() operator

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);

Operator &

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.

Operator *

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.

Was this helpful?
[0]
Scroll to Top
Scroll to Top