The compiler automatically terminates this array of characters with an extra piece of storage containing the value 0, to indicate the end of the character array.
Inside a character array, you can insert special characters by using escape sequences. These consist of a backslash (\) followed by a special code. For example \n means newline. Your compiler manual or local C guide gives a complete set of escape sequences; others include \t (tab), \\ (backslash) and \b (backspace).
Notice that the entire statement terminates with a semicolon.
Character array arguments and constant numbers are mixed together in the above cout
statement. Because the operator << is overloaded with a variety of meanings when used with cout, you can send cout a variety of different arguments, and it will “figure out what to do with the message.”
Chapter 1: Data Abstraction
76
Throughout this book you’ll notice that the first line of each file will be a comment that starts with the characters that start a comment (typically //), followed by a colon. This is a technique I use to allow easy extraction of information from code files (the program to do this is in the last chapter of the book). The first line also has the name and location of the file, so it can be referred to in text and in other files, and so you can easily locate it in the source code for this book (which is freely downloadable from http://www.BruceEckel.com, where you’ll find
instructions on how to unpack it).
Running the compiler
After downloading and unpacking the book’s source code, find the program in the
subdirectory CO2. Invoke the compiler with Hello.cpp as the argument. For simple, one-file programs like this one, most compilers will take you all the way through the process. For
example, to use the Gnu C++ compiler (which is freely available on the Internet), you say:
g++ Hello.cpp
Other compilers will have a similar syntax; consult your compiler’s documenation for details.
More about iostreams
So far you have seen only the most rudimentary aspect of the iostreams class. The output
formatting available with iostreams also includes features like number formatting in decimal, octal and hex. Here’s another example of the use of iostreams:
//: C02:Stream2.cpp
// More streams features
#include <iostream>
using namespace std;
int main() {
// Specifying formats with manipulators:
cout << "a number in decimal: "
<< dec << 15 << endl;
cout << "in octal: " << oct << 15 << endl;
cout << "in hex: " << hex << 15 << endl;
cout << "a floating-point number: "
<< 3.14159 << endl;
cout << "non-printing char (escape): "
<< char(27) << endl;
} ///:~
This example shows the iostreams class printing numbers in decimal, octal and hexadecimal
using iostream manipulators (which don’t print anything, but change the state of the output Chapter 1: Data Abstraction
77
stream). The formatting of floating-point numbers is determined automatically, by the compiler. In addition, any character can be sent to a stream object using a cast to a char (a char is a data type which holds single characters). This cast looks like a function call: char( ), along with the character’s ASCII value. In the above program, the char(27) sends an “escape”
to cout.
Character array concatenation
An important feature of the C preprocessor is character array concatenation. This feature is used in some of the examples in this book. If two quoted character arrays are adjacent, and no punctuation is between them, the compiler will paste the character arrays together into a
single character array. This is particularly useful when code listings have width restrictions:
//: C02:Concat.cpp
// Character array Concatenation
#include <iostream>
using namespace std;
int main() {
cout << "This is far too long to put on a single "
"line but it can be broken up with no ill effects\n"
"as long as there is no punctuation separating "
"adjacent character arrays.\n";
} ///:~



 

 

 

 

 

 

 

 

 

 

 

 

   
 
  The compiler creates storage for character arrays and stores the ASCII equivalent for each character in this storage...
Pomodliłem się do każdego boga jaki istniał bym był w wstanie wkurzyć tę kobietę do granic możliwości.