Danh mục tài liệu

Module 2 Introducing Data Types and Operators

Số trang: 32      Loại file: pdf      Dung lượng: 671.49 KB      Lượt xem: 16      Lượt tải: 0    
Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

At the core of a programming language are its data types and operators. These elements define the limits of a language and determine the kind of tasks to which it can be applied. As you might expect, C++ supports a rich assortment of both data types and operators, making it suitable for a wide range of programming. Data types and operators are a large subject. We will begin here with an examination of C++’s foundational data types and its most commonly used operators. We will also take a closer look at variables and examine the expression....
Nội dung trích xuất từ tài liệu:
Module 2 Introducing Data Types and Operators Module 2 Introducing Data Types and OperatorsTable of ContentsCRITICAL SKILL 2.1: The C++ Data Types ....................................................................................................... 2Project 2-1 Talking to Mars ......................................................................................................................... 10CRITICAL SKILL 2.2: Literals ......................................................................................................................... 12CRITICAL SKILL 2.3: A Closer Look at Variables ........................................................................................... 15CRITICAL SKILL 2.4: Arithmetic Operators................................................................................................... 17CRITICAL SKILL 2.5: Relational and Logical Operators ................................................................................ 20Project 2-2 Construct an XOR Logical Operation ........................................................................................ 22CRITICAL SKILL 2.6: The Assignment Operator ........................................................................................... 25CRITICAL SKILL 2.7: Compound Assignments .............................................................................................. 25CRITICAL SKILL 2.8: Type Conversion in Assignments ................................................................................. 26CRITICAL SKILL 2.9: Type Conversion in Expressions .................................................................................. 27CRITICAL SKILL 2.10: Casts........................................................................................................................... 27CRITICAL SKILL 2.11: Spacing and Parentheses ........................................................................................... 28Project 2-3 Compute the Regular Payments on a Loan .............................................................................. 29At the core of a programming language are its data types and operators. These elements define thelimits of a language and determine the kind of tasks to which it can be applied. As you might expect, C++supports a rich assortment of both data types and operators, making it suitable for a wide range ofprogramming. Data types and operators are a large subject. We will begin here with an examination ofC++’s foundational data types and its most commonly used operators. We will also take a closer look atvariables and examine the expression. 1 C++ A Beginner’s Guide by Herbert SchildtWhy Data Types Are ImportantThe data type of a variable is important because it determines the operations that are allowed and therange of values that can be stored. C++ defines several types of data, and each type has uniquecharacteristics. Because data types differ, all variables must be declared prior to their use, and a variabledeclaration always includes a type specifier. The compiler requires this information in order to generatecorrect code. In C++ there is no concept of a “type-less” variable.A second reason that data types are important to C++ programming is that several of the basic types areclosely tied to the building blocks upon which the computer operates: bytes and words. Thus, C++ letsyou operate on the same types of data as does the CPU itself. This is one of the ways that C++ enablesyou to write very efficient, system-level code.CRITICAL SKILL 2.1: The C++ Data TypesC++ provides built-in data types that correspond to integers, characters, floating-point values, andBoolean values. These are the ways that data is commonly stored and manipulated by a program. As youwill see later in this book, C++ allows you to construct more sophisticated types, such as classes,structures, and enumerations, but these too are ultimately composed of the built-in types.At the core of the C++ type system are the seven basic data types shown here:C++ allows certain of the basic types to have modifiers preceding them. A modifier alters the meaning ofthe base type so that it more precisely fits the needs of various situations. The data type modifiers arelisted here:signedunsignedlongshortThe modifiers signed, unsigned, long, and short can be applied to int. The modifiers signed and unsignedcan be applied to the char type. The type double can be modified by long. Table 2-1 shows all valid 2 C++ A Beginner’s Guide by Herbert Schildtcombinations of the basic types and the type modifiers. The table also shows the guaranteed minimumrange for each type as specified by the ANSI/ISO C++ standard.It is important to understand that minimum ranges shown in Table 2-1 are just that: minimum ranges. AC++ compiler is free to exceed one or more of these minimums, and most compilers do. Thus, the rangesof the C++ data types are implementation dependent. For example, on computers that use two’scomplement arithmetic (which is nearly all), an integer will have a range of at least −32,768 to 32,767. Inall cases, however, the range of a short int will be a subrange of an int, which will be a subrange of along int. The same applies to float, double, and long double. In this usage, the term subrange means arange narrower than or equal to. Thus, an int and long int can have the same range, but an int cannot belarger than a long int.Since C++ specifies only the minimum range a data type must support, you should check your compiler’sdocumentation for the actual ranges supported. For example, Table 2-2 shows typical bit widths andranges for the C++ d ...

Tài liệu được xem nhiều:

Tài liệu có liên quan: