33 Multiple Declarations Variables with the same data type can be grouped

33 multiple declarations variables with the same data

This preview shows page 33 - 45 out of 45 pages.

33
Image of page 33
Multiple Declarations Variables with the same data type can be grouped together and declared in one statement – Format: dataType variableList; – Example: double grade1, grade2, total, average; Initialization: using a declaration statement to store a value in a variable – Good programming practice is to declare each initialized variable on a line by itself – Example: double grade2 = 93.5; A First Book of C++ 4th Edition 34
Image of page 34
Memory Allocation Each data type has its own storage requirements – Computer must know variable’s data type to allocate storage – Definition statements: declaration statements used for the purpose of allocating storage A First Book of C++ 4th Edition 35
Image of page 35
Memory Allocation (cont'd.) A First Book of C++ 4th Edition 36
Image of page 36
Common Programming Errors Forgetting to declare all variables used in a program Attempting to store one data type in a variable declared for a different type Using a variable in an expression before the variable is assigned a value Dividing integer values incorrectly A First Book of C++ 4th Edition 37
Image of page 37
Common Programming Errors (cont'd.) Mixing data types in the same expression without clearly understanding the effect produced – It is best not to mix data types in an expression unless a specific result is desired Forgetting to separate individual data streams passed to cout with an insertion (“put to”) symbol A First Book of C++ 4th Edition 38
Image of page 38
Summary Four basic types of data recognized by C++ – Integer, floating-point, character, boolean • cout object can be used to display all data types Every variable in a C++ program must be declared as the type of variable it can store A First Book of C++ 4th Edition 39
Image of page 39
Summary (cont'd.) A simple C++ program containing declaration statements has the format: #include <iostream> using namespace std; int main() { declaration statements; other statements; return 0; } A First Book of C++ 4th Edition 40
Image of page 40
Summary (cont'd.) Declaration statements: inform the compiler of function’s valid variable names Definition statements: declaration statements that also cause computer to set aside memory locations for a variable • sizeof() operator: determines the amount of storage reserved for a variable A First Book of C++ 4th Edition 41
Image of page 41
Chapter Supplement: Bits, Bytes, and Binary Number Representations This section explains how numbers are stored in a computer’s memory and different means of representing them A First Book of C++ 4th Edition 42
Image of page 42
Bits and Bytes Bit – A switch that can be open or closed Byte – Group of eight bits Character code – Collection of patterns used to represent letters, single digits, and other characters Number codes – Patterns used to store numbers Words and addresses A First Book of C++ 4th Edition 43
Image of page 43
Binary, Hexadecimal, and Octal Numbers A First Book of C++ 4th Edition 44
Image of page 44
Binary, Hexadecimal, and Octal Numbers (cont’d.) A First Book of C++ 4th Edition 45
Image of page 45

You've reached the end of your free preview.

Want to read all 45 pages?

  • Fall '15
  • Marinolent
  • Byte, escape sequence, Associativity

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture