Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Copyright © 2007-2010 – Curt Hill Types What they do."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 Division operators If a is 22 and b is 5 a/b gives 4 a%b gives 2 Copyright © 2007-2010 – Curt Hill 5 ) 22 4 r 2

8 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

9 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 × 10 4 Since the exponent is hard to type we use E notation: 3.2E4 Copyright © 2007-2010 – Curt Hill

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 For our purposes we will only consider the following: char int double bool Copyright © 2007-2010 – Curt Hill

20 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

21 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


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

Similar presentations


Ads by Google