1.1.1. Compiling and Executing Our Program
The value returned from main is accessed in a system-dependent manner. On both UNIX and Windows systems, after executing the program, you must issue an appropriate echo command.
On UNIX systems, we obtain the status by writing
$ echo $?
To see the status on a Windows system, we write
$ echo %ERRORLEVEL%
By default, the command to run the GNU compiler is g++:
$ g++ -o prog1 prog1.cc
The -o prog1 is an argument to the compiler and names the file in which to put the executable file.
The command to run the Microsoft Visual Studio 2010 compiler is cl:
C:\Users\me\Programs> cl /EHsc prog1.cpp
The cl command invokes the compiler, and /EHsc is the compiler option that turns on standard exception handling.
Compilers usually include options to generate warnings about problematic constructs. Our preference is to use -Wall with the GNU compiler, and to use /W4 with the Microsoft compilers.
1.2. A FIRST LOOK AT INPUT/OUTPUT
C++ includes an extensive standard library that provides IO (and many other facilities).
Fundamental to the iostream library are two types named istream and ostream , which represent input and output streams, respectively. A stream is a sequence of characters read from or written to an IO device.
standard input, istream object cin
standard output, ostream object cout
standard error, warning and error messages ostream object cerr
general information, clog
Because the operator << returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second. As a result, we can chain together output requests.
std::cout << "Enter two numbers:" << std::endl;
is equivalent to
(std::cout << "Enter two numbers:") << std::endl;
The second operator prints endl, which is a special value called a manipulator. Writing endl has the effect of ending the current line and flushing the buffer associated with that device. Flushing the buffer ensures that all the output the program has generated so far is actually written to the output stream, rather than sitting in memory waiting to be written.
The prefix std:: indicates that the names cout and endl are defined inside the namespace named std . Namespaces allow us to avoid inadvertent collisions between the names we define and uses of those same names inside a library. All the names defined by the standard library are in the std namespace.
Writing std::cout uses the scope operator (the :: operator) to say that we want to use the name cout that is defined in the namespace std.
Comment pairs:
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */; <- only this line is illegal
std::cout << /* "*/" /* "/*" */;
No comments:
Post a Comment