Operators in C
Operators in C
OPERATORS IN C
● C language supports a lot of operators to be used in expressions.
These operators can be categorized into the following major groups:
● Arithmetic operators
● Relational Operators
● Equality Operators
● Logical Operators
● Unary Operators
● Conditional Operators
● Bitwise Operators
● Assignment operators
● Comma Operator
● Sizeof Operator
OPERATION OPERATOR SYNTAX COMMENT RESULT*
Multiply * a * b result = a * b 27
Divide / a / b result = a / b 3
ARITHMETIC
OPERATORS Addition + a + b result = a + b 12
Subtraction - a - b result = a – b 6
Modulus % a % b result = a % b 0
EQUALITY OPERATORS
● C language supports two kinds of equality operators to compare their operands for
strict equality or inequality. They are equal to (==) and not equal to (!=) operator.
● The equality operators have lower precedence than the relational operators.
OPERATOR MEANING
0 1 0 0 1 1 0 1
1 0 0 1 0 1
1 0
1 1 1 1 1 1
UNARY OPERATORS
Unary operators act on single operands. C language supports three unary operators.
They are unary minus, increment and decrement operators.
When an operand is preceded by a minus sign, the unary operator negates its value.
The increment operator is a unary operator that increases the value of its operand by
1. Similarly, the decrement operator decreases the value of its operand by 1. For
example,
int x = 10, y;
y = x++;
is equivalent to writing
y = x;
x = x + 1; whereas, y = ++x;
is equivalent to writing
x = x + 1;
y = x;
CONDITIONAL OPERATOR
● The conditional operator (?:) is just like an if .. else statement that can be written within
expressions.
● The syntax of the conditional operator is
exp1 ? exp2 : exp3
Here, exp1 is evaluated first. If it is true then exp2 is evaluated and becomes the result of the
expression, otherwise exp3 is evaluated and becomes the result of the expression. For
example,
large = ( a > b) ? a : b
● Conditional operators make the program code more compact, more readable.
● Conditional operator is also known as ternary operator as it is neither a unary nor a binary
operator; it takes three operands.
BITWISE OPERATORS
● Bitwise operators perform operations at bit level. These operators include: bitwise AND, bitwise
OR, bitwise XOR and shift operators.
● The bitwise AND operator (&) is a small version of the boolean AND (&&) as it performs
operation on bits instead of bytes, chars, integers, etc.
● The bitwise OR operator (|) is a small version of the boolean OR (||) as it performs operation on
bits instead of bytes, chars, integers, etc.
● The bitwise NOT (~), or complement, is a unary operation that performs logical negation on
each bit of the operand. By performing negation of each bit, it actually produces the 1’s
complement of the given binary value.
● The bitwise XOR operator (^) performs operation on individual bits of the operands.
SIZEOF OPERATOR
● sizeof is a unary operator used to calculate the sizes of data types.
● It can be applied to all data types.
● The operator returns the size of the variable, data type or expression in bytes.
● 'sizeof' operator is used to determine the amount of memory space that the
variable/expression/data type will take. For example,
sizeof(char) returns 1, that is the size of a character data type. If we have,
int a = 10;
unsigned int result;
result = sizeof(a);
then result = 2,
TYPE CONVERSION AND TYPE CASTING
● Type conversion and type casting of variables refers to changing a variable of one data type
into another.
● While type conversion is done implicitly, casting has to be done explicitly by the programmer.
We will discuss both of them here.
● Type conversion is done when the expression has variables of different data types. So to
evaluate the expression, the data type is promoted from lower to higher level where the
hierarchy of data types can be given as: double, float, long, int, short and char.
● For example, type conversion is automatically done when we assign an integer value (lower
level) to a floating point (higher level) variable. For example,
float x;
int y = 3;
x = y;
Now, x = 3.0,
● Type casting is also known as forced conversion. It is done when the value of a
higher data type has to be converted in to the value of a lower data type. For example,
we need to explicitly type cast an integer variable into a floating point variable.
float salary = 10000.00;
int sal;
sal = (int) salary;
● Typecasting can be done by placing the destination data type in parentheses
followed by the variable name that has to be converted.
● It is always a good practice to include the destination data type in
parentheses followed by the variable name that has to be converted for both
Typeconversion (lower to higher level) and Typecasting (higher to lower level).
Operator Precedence and Associativity
x = 3 * 4 * 10 + 5 * 6 ; is evaluated as
x = 12 *10 + 5 * 6 = 120 + 5 * 6 = 120 + 30 = 150, i.e. the evaluation is done
from left to right based on precedence order.
Operator Precedence and Associativity
● It is always a good practice to include use braces () in the expressions while
writing the C programs.