Operators
C is a rich language when it comes to the number of built-in operators. An operator is a symbol which gives instructions to perform any kind of operation, or action, on one or more operands. An operand is something upon which an operator acts to give a result. Operators that require two operands are called binary operators and the operators that require one operand are called unary operators. Individual constants, variables, array elements and function references can be joined together by various operators to form expression. There are many operators in C which can be categorized into various categories. They are listed below:
- Arithmetic Operators ( -, +, *, /, %)
- Assignment Operators
- Simple (=)
- Compound (+=, -=, *=, /=, %=)
- Unary Operators (-, ++, –, sizeof, &, *)
- Relational Operators (>, <, >=, <=)
- Equality Operators (==, !=)
- Logical Operators (&&, ||, !)
- Conditional Operators (? 🙂
- Bitwise Operators (&, |, ~)
C uses the operators mentioned above to provide a variety of services. An operator can be characterized by the number of operands it requires for operation, its precedence and its associativity. The last two qualities mentioned determine which operator is applied first when the two share an operand. Operators are combined with values to produce expressions, and every C expression has a value.
In this post, we will describe about arithmetic operators, which are one of the most widely used operators in C programming.
Arithmetic Operators
The operators that perform arithmetic operations like addition, subtraction, multiplication, division and modulus are called arithmetic operators. These are binary operators since they require two operands for operation.
In C, there exists no exponential operator. However, pow
, which is a library function, can carry out exponential operations. The operands acted upon by arithmetic operators must represent numeric values. The remainder operator or modulus operator (%) requires operands among which the numerator is an integer and the denominator is a non-zero integer. Likewise, the division operator (/) requires the denominator be a non-zero number. The act of dividing an integer quantity by another integer quantity is called integer division. The result of such division is always a truncated quotient, meaning that the decimal portion of the quotient is dropped. On the other hand, if a division operation is carried out with two floating point numbers or one floating point number and another integer, the result is a floating point number. The following points should be considered while using any arithmetic operator in C:
- Arithmetic operators can be applied only to operands having numeric values. Therefore, the operands can be integers, floating-point numbers and characters.
- Integer division truncates the fractional part.
- The expression (x%y) produces the remainder when x is divided by y, and thus, is zero when y divides x exactly. For example,
10 % 5 = 0
and11 % 5 = 1
. - The % operator cannot be applied to a float or a double.
- For / and %, the second operand, or denominator, must be non-zero.
- The interpretation of % operand is unclear, when one of the operands is negative.
The table given below summarizes the different arithmetic operators, their meaning and usage, through examples:
Operator | Symbol | Meaning | Example |
---|---|---|---|
Addition | + | Adds its two operands | a + b |
Subtraction | – | Subtracts the second operand from the first operand | a – b |
Multiplication | * | Multiplies its two operands | a * b |
Division | / | Divides the first operand by the second operand (numerator by denominator) | a / b |
Modulo Division | % | Gives the remainder when the first operand is divided by the second operand | a % b |