Top Banner
Copyright © 2007-2010 – Curt Hill Types What they do
21

Copyright © 2007-2010 – Curt Hill Types What they do.

Jan 17, 2016

Download

Documents

Clinton Preston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Copyright © 2007-2010 – Curt Hill Types What they do.

Copyright © 2007-2010 – Curt Hill

Types

What they do

Page 2: Copyright © 2007-2010 – Curt Hill Types What they do.

Introduction• People are smart, computers are

dumb• This is obvious when we speak

about types• The type is usually understood

when people work in algebra• We do not care if the number is an

integer, fraction or real – it is just a number

• Not so in C++ – we must explicitly state the type

Copyright © 2007-2010 – Curt Hill

Page 3: Copyright © 2007-2010 – Curt Hill Types What they do.

Standard types in C programs

• A type determines legal values and legal operations

• Most of the standard types are reserved words

• In the rest of this presentation the sizes and precisions given are for our machines

• Others compilers may be different

Copyright © 2007-2010 – Curt Hill

Page 4: Copyright © 2007-2010 – Curt Hill Types What they do.

Integer types

• int - a standard integer– Integers in range + or - 2.14 billion– Occupies four bytes

• short - half the size– Range is -32768 to +32767

• long – In our case same as int

Copyright © 2007-2010 – Curt Hill

Page 5: Copyright © 2007-2010 – Curt Hill Types What they do.

Integer Operators

• The integer types allow the standard mathematical operators:– Addition +– Subtraction -– Multiplication *– Integer division /

• 5 / 2 = 2

– Integer remainder %• 5 % 2 = 1

Copyright © 2007-2010 – Curt Hill

Page 6: Copyright © 2007-2010 – Curt Hill Types What they do.

Integer division

• Integer operators always give integer results

• Remembering division and remainder– Do long division– Do not go past the decimal point – Quotient is obtained with /– Remainder with %

• Integer constants are strings of digits with or without a sign

Copyright © 2007-2010 – Curt Hill

Page 7: Copyright © 2007-2010 – Curt Hill Types What they do.

Division operators

• If a is 22 and b is 5• a/b gives 4• a%b gives 2

Copyright © 2007-2010 – Curt Hill

5 ) 224 r 2

Page 8: Copyright © 2007-2010 – Curt Hill Types What they do.

Floating point types

• Also known as real numbers• Consist of exponent and mantissa

– Similar to scientific notation

• Float– Regular floating point numbers– About seven decimal digits

• Double– Twice as many mantissa digits as float– About 14 decimal digits

Copyright © 2007-2010 – Curt Hill

Page 9: Copyright © 2007-2010 – Curt Hill Types What they do.

Constants

• There are two forms for a real constant

• Floating point constants have either a decimal point or an exponent or both

• Scientific notation is something like this: 3.2 × 104

• Since the exponent is hard to type we use E notation:3.2E4

Copyright © 2007-2010 – Curt Hill

Page 10: Copyright © 2007-2010 – Curt Hill Types What they do.

Double Preferred

• Our machines have plenty of memory so saving space is not the issue

• The Pentium only does double arithmetic

• The standard math library accepts only double parameters

• Therefore we routinely use double instead of float

Copyright © 2007-2010 – Curt Hill

Page 11: Copyright © 2007-2010 – Curt Hill Types What they do.

Floating Point Operators

• The real types allow the standard mathematical operators:– Addition +– Subtraction -– Multiplication *– Real division /

• 5.0 / 2.0 = 2.5

– No remainder operator

Copyright © 2007-2010 – Curt Hill

Page 12: Copyright © 2007-2010 – Curt Hill Types What they do.

Overloaded operators

• The / operator is overloaded, which means that it has different meanings depending on what operands it has

• With two integers it means integer division, with two floating point numbers it means real division

• We will later consider what it means when the types do not match

Copyright © 2007-2010 – Curt Hill

Page 13: Copyright © 2007-2010 – Curt Hill Types What they do.

Character types

• Represents an ASCII character• char• One character• Constants are enclosed in single

quotes– As opposed to string constants which

are in double quotes

• Small integers can be used to specify as well

Copyright © 2007-2010 – Curt Hill

Page 14: Copyright © 2007-2010 – Curt Hill Types What they do.

Characters as numbers?

• There is some interchangability between characters and small integers

• Each character is represented on the machine by an integer in the range -128 to +127

• The character set of a machine is the convention that connects the number to the character– We will use ASCII on Windows

Copyright © 2007-2010 – Curt Hill

Page 15: Copyright © 2007-2010 – Curt Hill Types What they do.

Booleans

• Type name is bool• Holds a true or false

– Both are reserved words

• Will discuss this one more seriously later

Copyright © 2007-2010 – Curt Hill

Page 16: Copyright © 2007-2010 – Curt Hill Types What they do.

Modifiers• Before types we can apply several

modifiers that alter some of the characteristics of the type

• long– Usually means that the item is twice as

long as normal for more precision– Not applicable to characters

• short– Usually means that the item is half the

length– Not applicable to characters

Copyright © 2007-2010 – Curt Hill

Page 17: Copyright © 2007-2010 – Curt Hill Types What they do.

More modifiers

• signed– Explicitly must have a sign

• unsigned– May not have a sign– Not allowed on floating point types– Gives one more bit of precision for

integers or characters

Copyright © 2007-2010 – Curt Hill

Page 18: Copyright © 2007-2010 – Curt Hill Types What they do.

Examples

• A double is a long float• Sometimes there is a long double• Characters, may be by default

either signed or unsigned– Ours are signed

• Any particular compiler may ignore some of these

Copyright © 2007-2010 – Curt Hill

Page 19: Copyright © 2007-2010 – Curt Hill Types What they do.

For our purposes we will only consider the

following:• char• int• double• bool

Copyright © 2007-2010 – Curt Hill

Page 20: Copyright © 2007-2010 – Curt Hill Types What they do.

Mixed type expressions

• When an arithmetic expression has two types in it, we call this a mixed type expression

• In general the weaker type is converted to the stronger type and then the operation proceeds

• Shorter items are weaker than longer

• Integer items are weaker than floating

Copyright © 2007-2010 – Curt Hill

Page 21: Copyright © 2007-2010 – Curt Hill Types What they do.

Examples:

• Add a short int to an int– The short int is converted to an int and

the operation proceeds

• Add an int to a float– Convert the int to a float and continue

• To convert: – The original form and value of the

variable are unchanged– For the purpose of the expression

change the value from one type to another

Copyright © 2007-2010 – Curt Hill