An operator is a character or string of characters used as a built-in function. An operator is so-called because it takes one or more values and operates on them to produce a result. For example, the addition operator + can operate on the values 4 and 5 to produce the result 9. Such a procedure is called an operation, and any value operated on is called an operand.
Types of operators in C language
There are various types of operators available in C are listed below.
1) Arithmetic Operators:
They are used in arithmetic operations.
+ Addition - Subtraction / Division * Multiplication % Remainder (mod)
2) Unary Increment and decrement Operators: ++ —
The unary ++ and — operators increment or decrement the value in a variable. There are “pre” and “post” variants for both operators which do slightly different things are being explained below
var++ increment "post" variant ++var increment "pre" variant var-- decrement "post" variant --var decrement "pre" variant
int i = 42; i++; // increment on i // i is now 43 i--; // decrement on i // i is now 42
3) Relational Operators
These operate on integer or floating point values and return a 0 or 1 boolean value.
== Equal to != Not Equal to > Greater Than < Less Than >= Greater or Equal <= Less or Equal
To see if x equals three, write something like:
if (x == 3)
4) Logical Operators
The value 0 is false, anything else is true. The operators evaluate left to right and stop as soon as the truth or falsity of the expression can be deduced. Such operators are called “short-circuiting”.
In C, these are furthermore guaranteed to use 1 to represent true, and not just some random non-zero bit pattern. However, there are many C programs out there that use values other than 1 for true (non-zero pointers for example), so when programming, do not assume that a true boolean is necessarily 1 exactly.
! Boolean not (unary) && Boolean and || Boolean or
5) Bitwise Operators
C includes operators to manipulate memory at the bit level. This is useful for writing low-level hardware or operating system code where the ordinary abstractions of numbers, characters, pointers, etc are insufficient.
Bit manipulation code tends to be less “portable”. Code is “portable” if with no programmer intervention it compiles and runs correctly on different types of computers.
The bitwise operations are typically used with unsigned types. In particular, the shift operations are guaranteed to shift 0 bits into the newly vacated positions when used on unsigned values.
~ Bitwise Negation (unary) & Bitwise And | Bitwise Or ^ Bitwise Exclusive Or >> Right Shift << Left Shift
Other Assignment Operators
In addition to the plain = operator, C includes many shorthand operators which represent variations on the basic.
For example “+=” adds the right-hand side to the left-hand side.
x = x + 10;
can be reduced to
x += 10;
This is most useful if x is a long-expression such as the following, and in some cases, it may run a little faster.
Here’s the list of assignment shorthand operators…
+ , - = Increment or decrement * , / = Multiply or divide % = Mod