0% found this document useful (0 votes)
10 views

Module 2 - Part A

Uploaded by

savagegamer1289
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Module 2 - Part A

Uploaded by

savagegamer1289
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Module 2 – Part A

Operators in C, Type conversion and typecasting


Decision control and looping statements: Introduction to decision control, Conditional branching
statements, Iterative statements, nested loops
break and continue statements, goto statements

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

< less than


<= less than or equal to
> greater than
>= greater than or equal to
3. Equality Operator

4. Logical operator

Operators Meaning

&& Logical AND

|| 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(&&)

Usage: Exp1 && Exp2


 If expression on both sides of the logical operator is true then the whole expression is

true.

b) Logical OR operator (||)

Usage: Exp1 || Exp2

 If one or both expression on the left and right side of the logical operator is true then the

whole expression is true.

c) Logical NOT

 It takes a single expression and negates the value of the expression.

 It produce Zero if the expression evaluate to a non-zero value and produce 1 if the

expression produce a zero

5. Unary Operator

 It acts on single operands.

 C supports 3 types of unary operator.

1. Unary minus

2. Unary increment

3. Unary decrement
1. Unary minus (-)

 It negates the value of operand.


 For example, if a number is positive then it becomes negative when preceded with a
unary minus operator. Similarly, if the number is negative, it becomes positive after
applying the unary minus operator
int a, b = 10;
a = - (b)
The result of this expression is a = - 10, because a variable b has a positive value.
After applying unary minus operator (-) on the operand b, the value becomes -10,
which indicates it as a negative value.

2. Increment (++) and Decrement (--) Operator

 ++ increase the value of its operand by 1.


 -- Decrease the value of its operand by 1.
 Increment (++) and Decrement (--) Operator have two variants - Prefix and Postfix
 In a Prefix (++x, --x) expression, the operator is applied before an operand is fetched
for computation and thus, the altered value is used for the computation of the
expression in which it occurs.
 In a Postfix (x++,x--) expression, the operator is applied after an operand is fetched
for computation. Therefore, the unaltered value is used for the computation of the
expression in which it occurs.
 Examples:
m = 5;
y = ++m; // prefix increment
After Execution: y=6, m=6
m = 5;
y = m++; // postfix increment
After Execution: y=5, m=6
6. Conditional Operators/Ternary Operator (?:)

 General Syntax: exp1?exp2:exp3;


 Exp1 is evaluated first. If exp1 evaluates to a true (i.e any non-zero value) then exp2 is
evaluated and becomes the result of the expression. Otherwise, exp3 is evaluated and
becomes the result of the expression.
 For example,
a = 10; b=15;
x = (a>b)?a:b; // x ⇐ 15; i.e The expression finds larger of two numbers.
 Conditional Operator is a concise way of doing if-else condition.
 It can reduce the number of lines of code in a program
7. Bitwise Operator
 Act on bit level in a data item (integer variable, int constant etc.)
 Can’t work on floating point number(real numbers)

Operators Meaning

<< Shift left

>> Shift right

& Bitwise AND

| 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.

 int a = sizeof(int); // a=2


 int b = sizeof(float); // b=4
 int c = sizeof(a); // c=1

Operator Precedence Chart


II. TYPE CONVERSION AND TYPECASTING

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 type conversion is performed automatically by the compiler without programmer's


intervention; it is called implicit 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.

Conversion hierarchy of datatypes

 Example:
float x;
int y=3;
x=y;
 Now, x=3.0, as automatically integer value is converted into its equivalent floating point
representation.

Let us summarize how the conversion is done


1. float operands are converted to double

2. char or short operands whether it is signed or unsigned are converted to int

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.

Consider the following group of statements.

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.

 For example, to convert x to float, we write:

(float) x

For example, in the above example, the cast operator is (float). In general, the cast operator is
denoted as (type).

Consider the examples below:

int c = (int) 10.5; // float converted to int

char d = (char) c; // int converted to char

The explicit type specification applies only to the first operand , not to the entire expression.

Thus in the statement


a=(float) x/y

the cast operator is applicable only to x.

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.

Consider the code below:


float a=6.35;
int x=6,y=4;
a=(float) x/y;
After executing these statements the values will be
x=6
y=4
a = 1.5
In the above code, while evaluating the expression a=(float) x/y, following take place:
value 6 of x gets promoted to float - type casting
value 4 of y gets promoted to float - type coercion
Note that data types of x and y remain unchanged; only the values get promoted.

You might also like