Programming in c++ Simple Data Types: Built-in and Use- Defined Dale/eems/Headington
1 Simple Data Types: Built-in and Use-Defined
Programming in C++ Chapter 10 Topics Built-In Simple Types Integral and Floating Point Data Types Using Combined Assignment Operators Prefix and Postfix Forms of Increment and Decrement Operators Additional C++ Operator Character Sets s Using an Enumeration Type Creating and Including User-Written Header Files
2 Chapter 10 Topics ❖ Built-In Simple Types ❖ Integral and Floating Point Data Types ❖ Using Combined Assignment Operators ❖ Prefix and Postfix Forms of Increment and Decrement Operators ❖ Additional C++ Operator ❖ Character Sets ❖ Using an Enumeration Type ❖ Creating and Including User-Written Header Files
Programming in C++ Built-In Simple Types o Simple(atomic)data type A data type in which each value is atomic(indivisible) 心 Example: a single character of type char is atomic, but the string Good Morning is not
3 Built-In Simple Types ❖Simple(atomic)data type: A data type in which each value is atomic(indivisible). ❖Example: a single character of type char is atomic, but the string “Good Morning” is not
Programming in C++ C++ Simple Data Types simple types ntegral floating char short int long bool enum float double long double unsigned
4 C++ Simple Data Types simple types integral floating char short int long bool enum float double long double unsigned
Programming in C++ By definition the size of a C++ char value is always 1 byte exactly one byte of memory space Sizes of other data type values in C++ are machine-dependent
5 By definition, the size of a C++ char value is always 1 byte. exactly one byte of memory space Sizes of other data type values in C++ are machine-dependent. ‘A’
Programming in C++ Example On one machine, it maybe the case that sizeof(char)=1, sizeof(short)=2 sizeof(int)=4; sizeof(long)=8 On other machine, the size might be as follows sizeof(char)=1, sizeof(short)=2 sizeof(int)=2, sizeof(long)=4
6 Example ❖On one machine,it maybe the case that sizeof(char)=1; sizeof(short)=2; sizeof(int)=4; sizeof(long)=8; ❖On other machine, the size might be as follows: sizeof(char)=1; sizeof(short)=2; sizeof(int)=2; sizeof(long)=4;
Programming in C++ The only guarantees made by C++ are 1= sizeof( char)<= sizeof(short)<= sizeof(int )<= sizeof( long) 1<=sizeof bool)<=sizeof long) sizeof float )< sizeof double )<= sizeof long double) char is at least 8 bits short is at least 16 bits long is at least 32 bits
7 The only guarantees made by C++ are . . . 1 = sizeof( char ) <= sizeof( short ) <= sizeof( int ) <= sizeof( long ) 1 <= sizeof ( bool ) <= sizeof ( long ) sizeof ( float ) <= sizeof ( double ) <= sizeof ( long double ) char is at least 8 bits short is at least 16 bits long is at least 32 bits
Programming in C++ Using one byte(=8 bits ) 01 000 HOW MANY DIFFERENT NUMBERS CAN BE REPRESENTED USING Os and 1s? Each bit can hold either a 0 or a 1. So there are just two choices for each bit. and there are 8 bits 2x2x2x2x2x2x2x2=28=256
8 Using one byte ( = 8 bits ), HOW MANY DIFFERENT NUMBERS CAN BE REPRESENTED USING 0’s and 1’s? Each bit can hold either a 0 or a 1. So there are just two choices for each bit, and there are 8 bits. 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256 0 1 1 0 0 0 1 1
Programming in C++ Similarly, using two bytes(=16 bits) 0110001 0100 1010 16 65536 DIFFERENT NUMBERS CAN BE REPRESENTED If we wish to have only one number representing the integer zero, and half of the remaining numbers positive, and half negative, we can obtain the 65, 536 numbers in the range below -32,768 0 32767
9 Similarly, using two bytes ( = 16 bits), 2 16 = 65,536 DIFFERENT NUMBERS CAN BE REPRESENTED. If we wish to have only one number representing the integer zero, and half of the remaining numbers positive, and half negative, we can obtain the 65,536 numbers in the range below : -32,768 . . . . 0 . . . . 32,767 0 1 1 0 0 0 1 1 0 1 0 0 1 0 1 0
Programming in C++ Range of Values The interval within which values of a numeric type must fall, specified in terms of the largest and smallest allowable values 10
10 Range of Values The interval within which values of a numeric type must fall,specified in terms of the largest and smallest allowable values