fundamental data types - hampden-sydney collegepeople.hsc.edu/faculty-staff/robbk/coms261/lectures...

29
Fundamental Data Types Lecture 4 Sections 2.7 - 2.10 Robb T. Koether Hampden-Sydney College Mon, Sep 2, 2019 Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 1 / 29

Upload: others

Post on 17-Feb-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

  • Fundamental Data TypesLecture 4

    Sections 2.7 - 2.10

    Robb T. Koether

    Hampden-Sydney College

    Mon, Sep 2, 2019

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 1 / 29

  • 1 Integers

    2 Floating-Point Numbers

    3 The Character Type

    4 The String Type

    5 Assignment

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 2 / 29

  • Outline

    1 Integers

    2 Floating-Point Numbers

    3 The Character Type

    4 The String Type

    5 Assignment

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 3 / 29

  • Integer Types

    Integer Typecout

  • Integer Types

    Integer Typesint a = 123;int b = -456;unsigned int c = 789;

    The integer types.short – 2 bytesunsigned short – 2 bytesint – 4 bytesunsigned int – 4 byteslong – 4 bytesunsigned long – 4 bytes

    Each type can be either signed or unsigned.On some computers, a long can be 8 bytes.

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 5 / 29

  • Signed vs. Unsigned Integers

    Unsigned integers.An unsigned integer cannot be negative.Values range from 0 to 232 − 1.

    Signed integersA signed integer can be positive or negative.The high-order bit (sign bit) indicates the sign.Values range from −231 to 231 − 1.

    By default, integers are signed.

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 6 / 29

  • Example of Signed and Unsigned Integers

    Binary Unsigned Signed00000000 0 000000001 1 100000010 2 200000011 3 3

    ......

    ...01111111 127 12710000000 128 -12810000001 129 -12710000010 130 -12610000011 131 -125

    ......

    ...11111111 255 -1

    8-bit integers, unsigned and signed

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 7 / 29

  • Example of Signed and Unsigned Integers

    Binary Unsigned Signed0000000000000000 0 00000000000000001 1 10000000000000010 2 20000000000000011 3 3

    ......

    ...0111111111111111 32767 327671000000000000000 32768 -327681000000000000001 32769 -327671000000000000010 32770 -327661000000000000011 32771 -32765

    ......

    ...1111111111111111 65535 -1

    16-bit short, unsigned and signed

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 8 / 29

  • Example of Signed and Unsigned Integers

    Binary Unsigned Signed00000000000000000000000000000000 0 000000000000000000000000000000001 1 100000000000000000000000000000010 2 200000000000000000000000000000011 3 3

    ......

    ...01111111111111111111111111111111 2147483647 214748364710000000000000000000000000000000 2147483648 -214748364810000000000000000000000000000001 2147483649 -214748364710000000000000000000000000000010 2147483650 -214748364610000000000000000000000000000011 2147483651 -2147483645

    ......

    ...11111111111111111111111111111111 4294967295 -1

    32-bit int, unsigned and signed

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 9 / 29

  • Ranges of Values of Integer Type

    Type RangeFrom Tounsigned short 0 65535short −32,768 32,767unsigned int 0 4,294,967,295int −2,147,483,648 2,147,483,647

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 10 / 29

  • Integer Overflow

    What happens when an integer value becomes too large?That is, what if we assign to an integer the largest legal value, andthen add 1?Example

    IntLimitTest.cpp

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 11 / 29

  • Outline

    1 Integers

    2 Floating-Point Numbers

    3 The Character Type

    4 The String Type

    5 Assignment

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 12 / 29

  • Floating-Point Types

    Floating-Pointfloat a = 123.456f;double b = 123.456789012345;

    The floating-point types.float – 4 bytesdouble – 8 byteslong double – 8 bytes

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 13 / 29

  • Floating-Point Types

    Floating-Point Typecout

  • Floating-Point LiteralsFloating-point literals must have a decimal point and are doublesby default.To force a floating-point literal to be a float, we may cast is

    float(12.34)

    or we may append the letter f:12.34f

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 15 / 29

  • Single-Precision Numbers

    float Typefloat x = 12.34567f;

    The positive values of a float range from a minimum of±1.17549 × 10−38 to a maximum of ±3.40282 × 1038.Approximately 7 decimal-digit precision.

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 16 / 29

  • Double-Precision Numbers

    double Typedouble pi = 3.141592653589793;

    The positive values of a double range from a minimum of±2.22507 × 10−308 to a maximum of ±1.79769 × 10308.Approximately 16 decimal-digit precision.

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 17 / 29

  • Floating-Point Overflow and Underflow

    What happens if we begin with the largest possible float andthen double it?What happens if we begin with the smallest possible positivefloat and divide it by 2?Example

    FloatLimitTest.cpp

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 18 / 29

  • Outline

    1 Integers

    2 Floating-Point Numbers

    3 The Character Type

    4 The String Type

    5 Assignment

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 19 / 29

  • The Character Type

    char Typechar letter = ’a’;

    Characters (chars) are stored as one-byte integers using theASCII values (see p. 106).A character can have any of 256 different values.Character literals must use single quotation marks, e.g., ’A’.

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 20 / 29

  • ASCII Table (0 - 63)

    ASCII Char

    0 NULL1 SOTT2 STX3 ETY4 EOT5 ENQ6 ACK7 BELL8 BKSPC9 HZTAB

    10 NEWLN11 VTAB12 FF13 CR14 SO15 SI

    ASCII Char

    16 DLE17 DC118 DC219 DC320 DC421 NAK22 SYN23 ETB24 CAN25 EM26 SUB27 ESC28 FS29 GS30 RS31 US

    ASCII Char

    32 (space)33 !34 "35 #36 $37 %38 &39 ‘40 (41 )42 *43 +44 ’45 _46 .47 /

    ASCII Char

    48 049 150 251 352 453 554 655 756 857 958 :59 ;60 <61 =62 >63 ?

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 21 / 29

  • ASCII Table (64 - 127)

    ASCII Char

    64 @65 A66 B67 C68 D69 E70 F71 G72 H73 I74 J75 K76 L77 M78 N79 O

    ASCII Char

    80 P81 Q82 R83 S84 T85 U86 V87 W88 X89 Y90 Z91 [92 \93 ]94 ^95 _

    ASCII Char

    96 ‘97 a98 b99 c

    100 d101 e102 f103 g104 h105 i106 j107 k108 l109 m110 n111 o

    ASCII Char

    112 p113 q114 r115 s116 t117 u118 v119 w120 x121 y122 z123 {124 |125 }126 ~127 DEL

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 22 / 29

  • Characters as Integers

    char Typechar letter = ’a’;int value = letter;letter = value + 1;

    Characters are interchangeable with integers in the range 0 to255.The numerical value of a character is its ASCII value.

    Blank space (ASCII 32).Digits 0 - 9 (ASCII 48 - 57).Uppercase letters A - Z (ASCII 65 - 90).Lowercase letters a - z (ASCII 97 - 122).

    Characters are ordered according to their ASCII values.

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 23 / 29

  • Outline

    1 Integers

    2 Floating-Point Numbers

    3 The Character Type

    4 The String Type

    5 Assignment

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 24 / 29

  • The string Type

    string Typestring message = "Hello, World";

    A string is stored as a sequence of characters.A string may hold any number of characters, including none.String literals must use double quotation marks, e.g., "Hello".

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 25 / 29

  • The string Type

    string Typestring msg = "Hello, World";cout

  • Calculations with Characters

    ExampleCharCalcs.cpp

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 27 / 29

  • Outline

    1 Integers

    2 Floating-Point Numbers

    3 The Character Type

    4 The String Type

    5 Assignment

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 28 / 29

  • Assignment

    AssignmentRead Sections 2.7 - 2.10.

    Robb T. Koether (Hampden-Sydney College) Fundamental Data Types Mon, Sep 2, 2019 29 / 29

    IntegersFloating-Point NumbersThe Character TypeThe String TypeAssignment