From STREAM.H to IOSTREAM.H

Chapter 6 - Working with Data

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Standard C and C++ Data Types
All programs deal with some kind of information that you can usually represent by using one of the eight basic C and C++ types: text or char, integer values or int, floating-point values or float, double floating-point values or double (long double), enumerated or enum, valueless or void, pointers, and bool. Following is an explanation of the types:
  Text (data type char) is made up of single characters, such as a, Z, ?, and 3; and strings, such as “There is more to life than increasing its speed”. (Usually, 8 bits, or 1 byte per character, with the range of 0 to 255.)
  Integer values are those numbers you learned to count with (1, 2, 7, -45, and 1,345). (Usually, 16 bits wide, 2 bytes, or 1 word, with the range of -32,768 to 32,767. Under Windows 98 and Windows NT, integers are now 32 bits wide with a range from -2147483648 to 2147483647.)
  Floating-point values are numbers that have a fractional portion, such as pi (3.14159), and exponents (7.5633.4E-38 to 3.4E+38.)
  Double floating-point values have an extended range (usually, 64 bits, 8 bytes, or 4 words, with the range of 1.7E-308 to 1.7E+308). Long double floating-point values are even more precise (usually, 80 bytes, or 5 words, with the range of +/- 1.18E-4932 to 1.18E+4932).
  Enumerated data types allow for user-defined types.
  The type void is used to signify values that occupy zero bits and have no value. (This type can also be used for the creation of generic pointers, as discussed in Chapter 10.)
  The pointer data type doesn’t hold information in the normal sense of the other data types; instead, each pointer contains the address of the memory location holding the actual data. (This is also discussed in Chapter 10.)
  The new bool data type, which can be assigned the two constants, true
and
false.
Characters
Every language uses a set of characters to construct meaningful statements. For instance, all books written in English use combinations of 26 letters of the alphabet, the ten digits, and the punctuation marks. Similarly, C and C++ programs are written using a set of characters consisting of the 26 lowercase letters of the alphabet:
abcdefghijklmnopqrstuvwxyz
the 26 uppercase letters of the alphabet:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
the ten digits:
0 1 2 3 4 5 6 7 8 9
and the following symbols:
+ - * / =, . _ : ; ? \ “ ‘ ~ | ! # % $ & ( ) [ ] { } ^ @
C and C++ also use the blank space, sometimes referred to as white space. Combinations of symbols, with no blank space between them, are also valid C and C++ characters. In fact, the following is a mixture of valid C and C++ symbols:
++ - - == && || <<  >> >= <= += -= *= /= ?: :: /* */ //
The following C program illustrates how to declare and use char data types:
/*
*   char.c
*   A C program demonstrating the char data type and showing
*   how a char variable can be interpreted as an integer.
*   Copyright (c) Chris H. Pappas and William H. Murray, 1997
*/

#include <stdio.h>
#include <ctype.h>

int main( )
{
 char csinglechar, cuppercase, clowercase;

 printf(“\nPlease enter a single character: ”);
 scanf(“%c”,&csinglechar);

 cuppercase = toupper(csinglechar);
 clowercase = tolower(csinglechar);
 printf(“The UPPERcase character \’%c\’ has a decimal ASCII”
        “ value of %d\n”,cuppercase,cuppercase);
 printf(“The ASCII value represented in hexadecimal”
        “ is %X\n”,cuppercase);

 printf(“If you add sixteen you will get \’%c\’\n”,
         (cuppercase+16));
 printf(“The calculated ASCII value in hexadecimal”
        “ is %X\n”,(cuppercase+16));
 printf(“The LOWERcase character \’%c\’ has a decimal ASCII”
        “ value of %d\n”,clowercase,clowercase);

 return(0);
}
The output from the program looks like this:
Please enter a single character: d
The UPPERcase character ‘D’ has a decimal ASCII value of 68
The ASCII value represented in hexadecimal is 44
If you add sixteen you will get ‘T’
The calculated ASCII value in hexadecimal is 54
The LOWERcase character ‘d’ has a decimal ASCII value of 100
The %X format control instructs the compiler to interpret the value as an uppercase hexadecimal number.
Three Integers
Microsoft Visual C/C++ supports three types of integers. Along with the standard type int, the compiler supports short int and long int. These are most often abbreviated to just short and long. While the C language is not hardware dependent (syntax, etc.), data types used by the C language are. Thus, the actual sizes of short, int, and long depend upon the implementation. Across all C compilers, the only guarantee is that a variable of type short will not be larger than one of type long. Microsoft Visual C/C++ allocates 2 bytes for both short and int. (Under Windows 98 and Windows NT, integers are now 32 bits). The type long occupies 4 bytes of storage.
Unsigned Modifier
All C and C++ compilers allow you to declare certain types to be unsigned. Currently, you can apply the unsigned modifier to four types: char, short int, int, and long int. When one of these data types is modified to be unsigned, you can think of the range of values it holds as representing the numbers displayed on a car odometer. An automobile odometer starts at 000..., increases to a maximum of 999..., and then recycles back to 000.... It also displays only positive whole numbers. In a similar way, an unsigned data type can hold only positive values in the range of zero to the maximum number that can be represented.
For example, suppose you are designing a new data type called my_octal and have decided that my_octal variables can hold only 3 bits. You have also decided that the data type my_octal is signed by default. Since a variable of type my_octal can only contain the bit patterns 000 through 111 (or zero to 7 decimal), and you want to represent both positive and negative values, you have a problem. You can’t have both positive and negative numbers in the range zero to 7 because you need one of the 3 bits to represent the sign of the number. Therefore, my_octal‘s range is a subset. When the most significant bit is zero, the value is positive. When the most significant bit is 1, the value is negative. This gives a my_octal variable the range of -4 to +3.
However, applying the unsigned data type modifier to a my_octal variable would yield a range of zero to 7, since the most significant bit can be combined with the lower 2 bits to represent a broader range of positive values instead of identifying the sign of the number. This simple analogy holds true for any of the valid C data types defined to be of type unsigned. The storage and range for the fundamental C/C++ data types are summarized in Table 6-2.
Table 6-2: ANSI C/C++ Standard Data Types and Sizes
Type Name
Bytes
Other Names
Range of Values
int
*
signed, signed int
System dependent
unsigned int
*
unsigned
System dependent
__int8
1
char, signed char
–128 to 127
__int16
2
short, short int, signed short int
–32,768 to 32,767
__int32
4
signed, signed int
–2,147,483,648 to 2,147,483,647
__int64
8
none
–9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
char
1
signed char
–128 to 127
unsigned char
1
none
0 to 255
short
2
short int, signed
short int
–32,768 to 32,767
unsigned short
2
unsigned short int
0 to 65,535
long
4
long int, signed
long int
–2,147,483,648 to 2,147,483,647
unsigned long
4
unsigned long int
0 to 4,294,967,295
enum
*
none
Same as int
float
4
none
3.4E +/- 38 (7 digits)
double
8
none
1.7E +/- 308 (15 digits)
long double
10
none
1.2E +/ - 4932 (19
The long double data type (80-bit, 10-byte precision) is mapped directly to double (64-bit, 8-byte precision) in Windows NT and Windows 98. Signed and unsigned are modifiers that can be used with any integral type. The char type is signed by default, but you can specify /J to make it unsigned by default. The int and unsigned int types have the size of the system word. This is 2 bytes (the same as short and unsigned short) in MS-DOS and 16-bit versions of Windows, and 4 bytes in 32-bit operating systems. However, portable code should not depend on the size of int. Microsoft C/C++ also features support for sized integer types. Table 6-3 lists the valid data type modifiers in all of the various legal and abbreviated combinations.
Table 6-3: Valid Data Type Modifiler Abbreviations
Type Specifier
Equivalent(s)
signed char
char
signed int
signed, int
signed short int
short, signed short
signed long int
long, signed long
unsigned char
none
unsigned int
unsigned
unsigned short int
unsigned short
unsigned long int
unsigned long
float
none
long double
none
Floating Point
Visual C/C++ uses the three floating-point types: float, double, and long double. While the ANSI C standard does not specifically define the values and storage that are to be allocated for each of these types, the standard did require each type to hold a minimum of any value in the range 1E-37 to 1E+37. As you saw in Table 6-2, the Microsoft Visual C/C++ environment has greatly expanded upon this minimum requirement. Historically, most C compilers have always had the types float and double. The ANSI C committee added the third type, long double. Here are some examples of floating-point numbers:
float altitude = 47000;
double joules;
long double budget_deficit;
You can use the third type, long double, on any computer, even those that have only two types of floating-point numbers. However, if the computer does not have a specific data type of long double, then the data item will have the same size and storage capacity as a double.
The following C++ program illustrates how to declare and use floating-point variables:
//
//  float.cpp
//  A C++ program demonstrating using the float data type.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1997
//

#include <iostream.h>
#include <iomanip.h>
int main( )
{
 long loriginal_flags=cin.flags( );
 float fvalue;

 cout << “Please enter a float value to be formatted: ”;
 cin >> fvalue;

 cout << “Standard Formatting:   ” << fvalue << “\n”;
 cout.setf(ios::scientific);
 cout << “Scientific Formatting: ” << fvalue << “\n”;

 cout.setf(ios::fixed);
 cout << “Fixed Formatting:      ” << setprecision
      << fvalue;

 cout.flags(loriginal_flags);

 return(0);
}
The output looks like this:
Please enter a float value to be formatted: 1234.5678
Standard Formatting:   1234.57
Scientific Formatting: 1.234568e+003
Fixed Formatting:      0x004010191234.57
Notice the different value printed depending on the print format specification standard, scientific, or fixed.
Enumerated
When an enumerated variable is defined, it is associated with a set of named integer constants called the enumeration set. (These are discussed in Chapter 13.) The variable can contain any one of the constants at any time, and the constants can be referred to by name. For example, the following definition creates the enumerated type air_supply; the enumerated constants EMPTY, USEABLE, and FULL; and the enumerated variable instructor_tank:
enum air_supply { EMPTY,
                 USEABLE,
                 FULL=5 } instructor_tank;
All the constants and variables are type int, and each constant is automatically provided a default initial value unless another value is specified. In the preceding example, the constant name EMPTY has the integer value zero by default since it is the first in the list and was not specifically overridden. The value of USEABLE is 1 since it occurs immediately after a constant with the value of zero. The constant FULL was specifically initialized to the value 5, and if another constant were included in the list after FULL, the new constant would have the integer value of 6.
Having created air_supply, you can later define another variable, student_tank, as follows:
enum air_supply student_tank;
After this statement it is legal to say
instructor_tank = FULL;
student_tank    = EMPTY;
This places the value 5 into the variable instructor_tank and the value of zero into the variable student_tank.
  Note When defining additional enumerated variables in C++, it is not necessary to repeat the enum keyword. However, both syntaxes are accepted by the C++ compiler.
One common mistake is to think that air_supply is a variable. It is a “type” of data that can be used later to create additional enumerated variables like instructor_tank or student_tank.
Since the name instructor_tank is an enumerated variable of type air_supply, instructor_tank can be used on the left of an assignment operator and can receive a value. This occurred when the enumerated constant FULL was explicitly assigned to it. EMPTY, USEABLE, and FULL are names of constants; they are not variables and their values cannot be changed.
Tests can be performed on the variables in conjunction with the constants. The following is a complete C program that uses the preceding definitions:
/*
*   enum.c
*   A C program demonstrating the use of enumeration variables
*   Copyright (c) Chris H. Pappas and William H. Murray, 1997
*/

#include <stdio.h>
#include <stdlib.h>

int main( )
{
 enum air_supply { EMPTY,
                   USEABLE,
                   FULL=5 }  instructor_tank;
 enum air_supply student_tank;

 instructor_tank = FULL;
 student_tank = EMPTY;
 printf(“The value of instructor_tank is %d\n”,instructor_tank);

 if (student_tank < USEABLE) {
   printf(“Refill this tank.\n”);
   printf(“Class is cancelled.\n”);
   exit(1);
 }
 if (instructor_tank >= student_tank)
   printf(“Proceed with lesson\n”);
 else
   printf(“Class is cancelled!\n”);

 return(0);
}
In C, an enum type is equivalent to the type int. This technically allows a program to assign integer values directly to enumerated variables. C++ enforces a stronger type check and does not allow this mixed-mode operation. The output from the program looks like this:
The value of instructor_tank is 5
Refill this tank.
Class is cancelled.
And, the New C++ Type—bool
This keyword is an integral type. A variable of this type can have values true and false. All conditional expressions now return a value of type bool. For example, myvar! = 0 now returns true or false depending on the value of myvar.
The values true and false have the following relationship:
!false == true
!true == false
Look at the following statement:
if (myexpression)
 statement1;
If myexpression evaluates to true, statement1 is always executed; if myexpression evaluates to false, statement1 is never executed. An important fundamental to keep in mind when writing test expressions is this: Both C and C++ view any non-zero (!0) value as true, and any expression evaluating to zero (0) as false.
  Note When a postfix or prefix ++ operator is applied to a variable of type bool, the variable is set to true. The postfix or prefix — operator cannot be applied to a variable of this type. Also, the bool type participates in integral promotions. An r-value of type bool can be converted to an r-value of type int, with false becoming zero and true becoming 1.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net