Main Page

Previous Page
Next Page

[Page 775 (continued)]

15.3. Stream Output

Formatted and unformatted output capabilities are provided by ostream. Capabilities for output include output of standard data types with the stream insertion operator (<<); output of characters via the put member function; unformatted output via the write member function (Section 15.5); output of integers in decimal, octal and hexadecimal formats (Section 15.6.1); output of floating-point values with various precision (Section 15.6.2), with forced decimal points (Section 15.7.1), in scientific notation and in fixed notation (Section 15.7.5); output of data justified in fields of designated widths (Section 15.7.2); output of data in fields padded with specified characters (Section 15.7.3); and output of uppercase letters in scientific notation and hexadecimal notation (Section 15.7.6).

15.3.1. Output of char * Variables

C++ determines data types automatically, an improvement over C. Unfortunately, this feature sometimes "gets in the way." For example, suppose we want to print the value of a char * to a character string (i.e., the memory address of the first character of that string). However, the << operator has been overloaded to print data of type char * as a null-terminated string. The solution is to cast the char * to a void * (in fact, this should be done to any pointer variable the programmer wishes to output as an address). Figure 15.3 demonstrates printing a char * variable in both string and address formats. Note that the address prints as a hexadecimal (base-16) number. [Note: The reader who wants to learn more about hexadecimal numbers should read Appendix D, Number Systems.] We say more about controlling the bases of numbers in Section 15.6.1, Section 15.7.4, Section 15.7.5 and Section 15.7.7. [Note: The memory address shown in the output of the program in Fig. 15.3 may differ among compilers.]

Figure 15.3. Printing the address stored in a char * variable.
(This item is displayed on page 776 in the print version)

 1  // Fig. 15.3: Fig15_03.cpp
 2  // Printing the address stored in a char * variable.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 7  int main()
 8  {
 9     char *word = "again";
11     // display value of char *, then display value of char *
12     // static_cast to void *
13     cout << "Value of word is: " << word << endl
14        << "Value of static_cast< void * >( word ) is: "
15        << static_cast< void * >( word ) << endl;
16     return 0;
17  } // end main

 Value of word is: again
 Value of static_cast< void * >( word ) is: 00428300

15.3.2. Character Output using Member Function put

We can use the put member function to output characters. For example, the statement

cout.put( 'A' );

[Page 776]

displays a single character A. Calls to put may be cascaded, as in the statement

cout.put( 'A' ).put( '\n' );

which outputs the letter A followed by a newline character. As with <<, the preceding statement executes in this manner, because the dot operator (.) evaluates from left to right, and the put member function returns a reference to the ostream object (cout) that received the put call. The put function also may be called with a numeric expression that represents an ASCII value, as in the following statement

cout.put( 65 );

which also outputs A.

Previous Page
Next Page