Module 2 - Part A
Module 2 - Part A
I. OPERATORS IN C
An operator is a symbol that tells the computer to perform some mathematical or
logical operation (E.g. +,-,*,/,%, <, > etc....).
C supports a lot of operator to be used in expression. These operators can be
categorized into the following major groups.
Classes of C Operators
1. Arithmetic operators
2. Relational Operators
3. Equality Operator
4. Logical Operators
5. Unary Operator
6. Conditional Operators
7. Bitwise Operators
8. Assignment Operators
9. Comma Operator
10. Sizeof Operator
1. Arithmetic operators
‘a’ and ‘b’ are called operands.
Arithmetic operator can be applied to any integer or floating point number.
Modulus Operator gives the remainder of an integer division. This operator can be
applied only to integer operands and cannot be used on float or double operands.
E.g: Result of 10%3 is 1, because if you do integer division 10/3 the remainder is 1.
15%5 is equal to 0, since 5 exactly divide 15 and remainder is 0.
Sign of the result is the sign of the numerator (The first operand)
-10 % 3 ==> -1
-10 % -3 ==> -1
10 % -3 ==> 1
2. Relational Operators
Used to compare two data values for taking decisions. Also known as comparison
operator.
Expression that contains relational operators called relational expression.
It returns true or false value, depending on whether the conditional relationship
between the two operands holds or not.
Operators Meaning
4. Logical operator
Operators Meaning
|| Logical OR
! Logical NOT
Mainly used to check more than one condition to take decisions (&& and ||).
Logical expressions are evaluated from left to right.
a) Logical AND operator(&&)
true.
If one or both expression on the left and right side of the logical operator is true then the
c) Logical NOT
It produce Zero if the expression evaluate to a non-zero value and produce 1 if the
5. Unary Operator
1. Unary minus
2. Unary increment
3. Unary decrement
1. Unary minus (-)
Operators Meaning
| Bitwise OR
^ Bitwise Exclusive OR
8. Assignment Operators
Basic assignment operator (=) Assign value of RHS expression to variable on LHS
General Syntax: var = exp;
9. Comma Operator
It takes 2 operands. It works by evaluating the first and discarding its value, and then
evaluates the second and return the value as the result of the expression.
Comma separated operands when chained together are evaluated in left-to-right sequence
with the right most value yielding the result of the expression.
Example
int a=2, b=3, x=0;
x= (++a, b+=a);
Now, the value of x=6.
10. sizeof Operator
It is a unary operator used to find the size (in bytes) of a data type or variable or constant.
Type conversion or Type casting of variable refers to changing a variable of one data type into
another.
Type conversion
Type conversion is done when the expression has variable of different data types. The
process of converting one data type into another is called type conversion.
The basic principle behind the implicit conversion is that if operands are of different
types, the lower type (ie. smaller in size) should be converted to a higher type (i.e. bigger
in size) so that there is no loss in value or precision.
Since a lower type is converted to a higher type, it is said that the lower type is promoted
to a higher type and the conversion is known as promotion.
Example:
float x;
int y=3;
x=y;
Now, x=3.0, as automatically integer value is converted into its equivalent floating point
representation.
3. If one of the operands is long double, the other will be converted to long double and the result
will be long double.
4. If one of the operands is double, the other will be converted to double and the result will be
double.
float f= 3.5;
int i;
i=f;
Here f is demoted to type int ie, The fractional part of f will be lost and i will contain 3 (not
3.5). In this case demotion is takes place, ie, a higher level data type is converted into a lower
type. Whenever demotion occurs, some information is lost.
Type Casting/forced conversion.
It is done when the value of a higher data type has to be converted into the value of lower
data type.
It is also possible to forcibly convert the data type of the value of an expression or
operand by the programmer. This is known as explicit type conversion (also called type
casting). To do so, the expression must be preceded by the name of the desired data type ,
enclosed in parentheses:
(type) expression
where, type is any valid C data type, and expression may be a constant, variable or
expression.
(float) x
For example, in the above example, the cast operator is (float). In general, the cast operator is
denoted as (type).
The explicit type specification applies only to the first operand , not to the entire expression.
An important aspect of type conversion is that the data type of the expressions is not changed by
casting. Rather it is value of the expression that undergoes type conversion.