C Tutorial – Operators in C
An operator is a symbol that operates on a certain data type and produces the output as the result of the operation. In C, you can combine various operators of similar or different categories and perform an operation. In this case the C compiler tries to solve the expression as per the rules of precedence.
Category of operators
A unary operator is an operator, which operates on one operand.
A binary operator is an operator, which operates on two operands
A ternary operator is an operator, which operates on three operands.
Following are the different types of Operators
The arithmetic operator is a binary operator, which requires two operands to perform its operation of arithmetic. Following are the arithmetic operators that are available
|%||Modulo or remainder|
At the time of using the â€˜/â€™ division operator on two integers, the result will also be an integer. E.g. 100/8 will result in 12 and not 12.50.
A unary operator is an operator, which operates on one operand, i.e. it operates on itself.
Following are the unary operators available under C
! & * – + ~
Increment and Decrement Operators
These operators also fall under the broad category or unary operators but are quite distinct than unary minus.
The increment and decrement operators are very useful in C language. They are extensively used in for and while loops. The syntax of these operators is given below.
The ++ operator increments the value of the variable by one, whereas the â€“ operator decrements the value of the variable by one.
These operators can be used in either the postfix or prefix notation as follows:
Postfix: a++ or a–
Prefix: ++a or â€“a
In the postfix notation, the value of the operand is used first and then the operation of increment or decrement takes place, e.g. consider the statements below:
int a,b; a = 10; b = a++; printf(â€œ%d\nâ€,a); printf(â€œ%d\nâ€,b);
In the prefix notation, the operation of increment or decrement takes place first after which the new value of the variable is used.
int a,b; a = 10; b = ++a; printf(â€œ%d\nâ€,a); printf(â€œ%d\nâ€,b);
Relational operators compare between two operands and return in terms of true or false i.e. 1 or 0. In C and many other languages a true value is denoted by the integer 1 and a false value is denoted by the integer 0. Relational operators are used in conjunction with logical operators and conditional & looping statements.
Following are the various relational operators
|<=||Less than or equal to|
|>=||Greater than or equal to|
Closely related to the relational operators is the equality operator as follows:
|==||Equal to||!=||Not equal to|
Suppose we have three integer variables a, b and c with values 1,2 and 3 respectively.
A logical operator is used to compare or evaluate logical and relational expressions. There are three logical operators available in the C language.
Truth table for Logical AND
Truth table for Logical OR
Let us take three variables a, b, and c and assume a = 5,b=10 and c=7
a>b && a+b>c
Here a>b will return false hence 0 and a+b>c will return true hence 1. Therefore, looking at the truth table 0 AND 1 will be 0. From this we can understand, that this expression will evaluate to false.
Let us look at the same example using Logical OR (||)
a>b || a+b>c
Here a>b will return false hence 0 and a+b>c will return true hence 1. Therefore, looking at the truth table 0 OR 1 will be 1. From this we can understand, that this expression will evaluate to true.
The Logical NOT operator (!)
The ! (Logical NOT) operator is a unary operator. This operator inverses a logical result.
E.g. if a = 5 and b = 7, then
!(a == b)
will evaluate to be true. This expression works as follows, a==b evaluates to be false, now because of the ! (NOT) operator, the false is inverted to be true.
An assignment operator (=) is used to assign a constant or a value of one variable to another.
a = 5; b = a; interestrate = 10.5 result = (a/b) * 100;
Remember that there is a remarkable difference between the equality operator (==) and the assignment operator (=). The equality operator is used to compare the two operands for equality (same value), whereas the assignment operator is used for the purposes of assignment.
You can use the assignment for multiple assignments as follows:
a = b= c = 10;
At the end of this expression all variables a, b and c will have the value 10. Here the order of evaluation is from right to left. First 10 is assigned to c, hence the value of c now becomes 10. After that, the value of c (which is now 10) is assigned to b, hence the value of b becomes 10. Finally, the value of b (which is now 10) is assigned to a, hence the value of a becomes 10.
Arithmetic Assignment Operators
Arithmetic Assignment operators are a combination of arithmetic and the assignment operator. With this operator, the arithmetic operation happens first and then the result of the operation is assigned.
|Sum+=3||Sum = sum + 3|
|Count-=4||Count -= 4|
|Factorial*=num||Factorial = factorial * num|
|Num/=10||Num = num /10|
|A%=3||A = a % 3|
A conditional operator checks for an expression, which returns either a true or a false value. If the condition evaluated is true, it returns the value of the true section of the operator, otherwise it returns the value of the false section of the operator.
Its general structure is as follows:
Expression1 ? expression 2 (True Section): expression3 (False Section)
a=3,b=5,c; c = (a>b) ? a+b : b-a;
The variable c will have the value 2, because when the expression (a>b) is checked, it is evaluated as false. Now because the evaluation is false, the expression b-a is executed and the result is returned to c using the assignment operator.
Precedence of Operators
|Unary||Right to Left|
|Arithmetic Operators||Left to Right|
|Relational Operator||Left to Right|
|Equality Operator||Left to Right|
|Logical Operator||Left to Right|
|Conditional Operator||Right to Left|
|Assignment Operator||Right to Left|
|Comma operator||Right to Left|
When an operatorâ€™s operands are of different data types, C automatically converts them to the same data type. This can affect the results of mathematical expressions. This is called type conversion. In C type conversion can be done by two ways, viz.
Automatic Conversion (Arithmetic Promotion)
When a value is converted to a higher type, it is said to be promoted. Letâ€™s look at the specific rules that govern the evaluation of mathematical expressions.
Rule 1: chars, shorts, unsigned shorts are automatically promoted to int.
Rule 2: When an operator works with two values of different data types, the lower data type is promoted to a higher data type.
Type casting means to convert a higher data type to a lower data type. For the purpose of type casting we require to use the type case operator, which lets you manually promote or demote a value. It is a unary operator which appears as the data type name followed by the operand inside a set of parentheses. E.g.
val = (int)number;
This was about Operators in C, in the next C Tutorial you will learn about input and output in C.
Subscribe now to receive updates when a new C tutorial is released.