<<

LAB 2.2 IDENTIFYING DATA TYPES The objective of a C++ program is to manipulate data. C++ categorizes data into different types, and only certain operations can be performed on particular types of data. Every has a different of values associated with it.The data type also determines the amount of memory used, and conse- quently,how a value or other data is represented. Three C++ data types are simple, structured, and pointers. Simple data types are categorized as inte- gral, floating-point, and enumeration. Integral types are integers or numbers without a decimal part. Floating-point are decimal numbers. Enumeration types are user-defined and will be discussed in Chapter 8. Integral data types are further classified into nine categories: char, short, int, long, bool, unsigned char, unsigned short, unsigned int, and unsigned long. Each type is associated with a different set of values. This chapter focuses on int, bool, and char integral types. Positive integers do not need a plus ( + ) sign in front of them. No commas are used with an integer. The int data type is used to represent integers between -2147483648 and 2147483647, and the data type short is used to represent integers between 032768 and 32767. Different may allow different ranges of values. The bool data type has only two values: true and false. (The data type bool is not available on all sys- tems.) The central purpose of the bool type is to manipulate logical (Boolean) expressions. The char data type deals with small numbers, and is used to represent characters and special symbols. The char data type is used to represent integers between -128 and 127. Additionally, the char data type can represent every key on your keyboard and every printable .When using the char data type, you enclose each character represented within single quotation marks. In C++, the char data type uses the American Standard Code for Information Interchange (ASCII) character set, which contains 128 values. Each character is listed in a predefined order, which is called a collating sequence.The col- lating sequence is used when you compare characters. Some character representations require the back- slash character combined with another character.These representations are still considered to be single characters. The floating-point data type is represented in the form of scientific notation called floating-point nota- tion. C++ has three data types to represent numbers with decimals.The data type float uses 4 , which holds numbers between –3.4E+38 and 3.4E+38, and has a maximum number of significant dig- its of six or seven.The data type double uses 8 bytes, which holds numbers between –1.7E+308 and 1.7E+308, and has a maximum number of significant digits of fifteen. On newer compilers, the data types double and are the same.The maximum number of significant digits is called the precision. For your exercises in this Lab Manual, mostly use the double type instead of the float type because certain compilers might give you a warning message when using the float type. The data type string is a programmer-defined data type.To use this data type, you need to access pro- gram components from the .A string is a sequence of zero or more characters enclosed in double quotation marks. Every character has a relative position in the string.The length of a string is the num- ber of characters in it.The data type string may not be available in Standard C++.To process strings effectively,C++ provides the library string, which contains various operations to manipulate a string. Every character in a string has a relative position in the string.The position of the first character is 0, the position of the second character is 1, and so on.The length of a string is the number of characters in it. Remember that a space is a character. Objectives In this lab, you become acquainted with the simple data types—int, bool, char, floating-point, and the user-defined string type. After completing this lab, you will be able to:

Identify integral, Boolean, character, floating-point, and string data types.

Estimated completion time: 15–20 minutes

Identifying Data Types Identify which simple data type or string type could be used to represent the value.

Value Integral Floating Boolean Character String Invalid 1. –2147483648

2. true

3. 1

4. 0

5. ‘?’

6. “Hello”

7. “123”

8. ‘1’

9. 66

10. 7.8E3

11. 0.1E-6

12. “”

13. ‘ ‘

14. ‘+’

15. “int”

16. “false”

17. +36

18. 0.0 Value Integral Floating Boolean Character String Invalid 19. 127.0

20. false

Question Answer (Circle the correct answer) 21. The following numbers are examples of integral numbers: T F 4.1 4.2 22. The data type int is a simple data type. T F 23. The data type double is a complex data type. T F 24. A simple data type can be assigned one and only one value, while a complex data type can be reassigned values. T F