LAB 1a
C++ Simple Data Types

Objectives: Review C++ type statements

In this laboratory assignment we will consider the following:

A. Review of C++ Type Statements

A. Review of C++ Type Statements

Simple Data TYPES

Type statements in C++ are used to allow the compiler to:

a) reserve blocks of memory to store information and
b) give the reserved blocks of memory a symbolic name so that the data contained in this block of memory can be manipulated by referring to this name in future C++ statements.

The simple data types which have been previously discussed include the int type, the float type and the char type. These types differ mainly in "how" the data is stored. For example, in the previous lab we saw that the char type will use 8 bits (1 byte) of memory while the int type will use 16 or more bits (2 bytes or more) of memory. These simple data types can be further specified to enable the compiler to have better control of the range of values to be stored. For example, the unsigned int type can be used to store non-negative integers only; therefore, memory is not allocated to store the sign of the integer. Thus, we can see that the bit normally used to store the sign could be used as an additional bit for the actual integer. This would allow storage of possibly "larger" positive integers in the unsigned int type than in the int type. Other integer TYPES include: short int, unsigned long and long int. Other types for floating point numbers are double and long double. The following table contains common types in non-decreasing order by size (the number of bytes required for storage).

short int float
unsigned int double
int long double
unsigned long  
long int  

Exercise 1:

Determine how the following variables should be declared in order to maintain good memory usage by the compiler.

  1. the number of students in a class
  2. the distance to the moon (the distance to the moon is over 200,000 miles)
  3. last month's checking account balance
  4. a counter used to count the number of lines in a text file
  5. number of people living in the US
  6. the temperature used in a chemistry formula

The ENUM Statement

The enum (short for enumerated type) keyword can be used to define new data types. In programming where advanced data structures will be required, the enum statement is essential. The enum type should be used to provide good internal documentation. This makes the program more readable and easier to understand (more self-documenting). Also, another purpose for the enum statement is to help reduce logical errors in a program.

The enum statement provides a way to name integer constants and allows the programmer to invent a "new" type and to specify what values this new type can support. The syntax of the enum appears as:

enum user-defined_type {listing of data values};

enum {listing of integer constants};

Note the following examples:

enum {FALSE, TRUE};

Now we have two new data types: Day and Video. Note that these are types, not variables! Therefore, declarations similar to the following would be appropriate.

Day returnDay;
Video oneVideo;

The third enum statement listed above is equivalent to the following:

const int FALSE = 0;
const int TRUE = 1;

Enumerated variables are treated as "integers" by the compiler which means that these values are ordinal (ordered). In the Day list of data items, MONDAY would be stored as 0 and THURSDAY would be stored as 3, for example.

There are disadvantages of using enumerated data types, however. One major disadvantage is the fact that variables of this type cannot be input or output directly (using the input and output operators). We will learn how to get around this later!

Using typedef

Another tool which is often used to aid in self documentation is the typedef specification. The typedef declaration is used to re-define an existing data type and is invaluable in making a program easier to modify. This will be extremely important to us later in the semester.

The syntax for the typedef follows:

typedef ExistingDataType NewDataType;

For example,

typedef char CharacterType;

re-names the type char as CharacterType. Now we have a "new" type. It is called a CharacterType and we could declare variables as follows:

CharacterType letter;
CharacterType name[20];

Exercise 2:

a) Show how to declare Real as a synonym for float (float and Real can be used interchangeably).

b) Show how to declare the following constants using an enum statement:
WINTER =0, SPRING = 1, SUMMER = 2 and FALL = 3.

Topic attachments
I Attachment Action Size Date Who Comment
Datdat customers.dat manage 0.5 K 2011-02-10 - 21:58 JimSkon Customers data file
Cppcpp lab1.cpp manage 2.0 K 2011-02-03 - 20:23 JimSkon lap 1 sample code
Cppcpp lab2.cpp manage 2.3 K 2011-02-18 - 17:31 JimSkon Demo Program
Datdat videos.dat manage 0.2 K 2011-02-03 - 20:25 JimSkon Video list for Lab 1
Topic revision: r7 - 2011-02-21 - JimSkon
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback