Computer Programming Part 2-Computer Architecture and Programming-Lecture Slides, Slides of Computer Architecture and Organization

Chanchal Mahanthapa delivered this lecture at Chandra Shekhar Azad University of Agriculture and Technology for Programming and Computer Architecture. It includes: Expressions, Explicit, Casting, Operators, Mixed, Statement, Operand, Type, Terminology

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekraj
ekraj 🇮🇳

4.1

(11)

123 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Programming
Expressions / mixed expressions and casting (Implicit
and Explicit casting), Post fix and Pre fix Operators
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Computer Programming Part 2-Computer Architecture and Programming-Lecture Slides and more Slides Computer Architecture and Organization in PDF only on Docsity!

Computer Programming

Expressions / mixed expressions and casting (Implicitand Explicit casting), Post fix and Pre fix Operators

docsity.com

Mixed Expressions

How expressions involving mixed types ofdata are evaluated. int value1=10;long value2 =25L;float value3= 30.0f;double result=value1+value2+value3;

The value of result is calculated as the sum ofthree different types of variables.

For each add operation, one of the operands willbe converted to the type of the other beforeaddition can be carried out.

docsity.com

Mixed Expressions(Contd.)

The rules for dealing with mixed expressionsapply only when the types of the operands for abinary operations are different. Rules in thesequence in which they apply are as following:

If either operand is of type

long double

, the

other us converted to

long double

.

If either operand is of type

double

, the other us

converted to

double

.

If either operand is of type

float,

the other us

converted to

float

.

docsity.com

Mixed Expressions(Contd.)

Any operand of type

char,

signed

char,

unsigned

char,

short

or

unsigned

short

is converted to type

int

, as long as type

int

can represent all values of the original

operand type. Otherwise, the operand isconverted to

unsigned

int

An enumeration type is converted to the first int,

unsigned

int,

long

or

unsigned

long

that accomodates the range

of

enum

If either operand is of type

unsigned

long

the other us converted to

unsigned

long

docsity.com

Mixed Expressions(Contd.)

The basic idea is very simple.

With two operands of

different types the type with the lesser range ofvalues is converted to the other.

The formal rules

roughly boil down to:

if the operation involves two different floating point types,the one with the lesser precision will be promoted to theother.

If the operations involves an integer and a floating pointvalue, the integer will be promoted to the floating pointtype.

If the operation involves mixed integer types, the type withthe more limited range will be promoted to the other.

If the operation involves enumerations they will beconverted to a suitable integer type.

docsity.com

Terminology

The term

conversion/implicit casting

means an automatic conversion of one typeto another.

The term

promotion

generally means a

conversion of a data value from a type withlesser range to a type with a greater range.

Explicitly conversion from one data type toanother is referred to as

cast

and the action

of explicitly converting a value is called casting

.

docsity.com

Difference between Implicit andExplicit casting

Expression

LHS Value

int1 = 7; int2 = 2;float1 = int1 / int2 + 5;

int1 = 3.8;

3

float2 = float(int1)/int2 +

float1 = 5;

Implicit castingType casting

docsity.com

Post fix and Pre fix Operators

Both the increment operator (++) and thedecrement operator(--) come in twovarieties:

Prefix and postfix.

The prefix variety is written before the variable name

(++myAge);

Variable is changed, then the expression it is in isevaluated.

The postfix variety is written after

(myAge++)

Expression the variable is in executes, then the variableis changed.

docsity.com

Post fix and Pre fixOperators(Contd.)

If

c

=

5

, then

cout << ++c;^ 

c

is changed to

6

, then printed out

cout << c++;^ 

Prints out

5

(

cout

is executed before the

increment.

c

then becomes

6

docsity.com

docsity.com

docsity.com

docsity.com