Skip to main content

C++ Primer Ch1. Introduction


Different compilers use different suffix conventions; the most common include .cc, .cxx, .cpp, .cp, and .C

To compile:

$ CC prog1.cc
prog1.exe is generated on Windows, a.out is generated on UNIX

To run:
$prog1 or $.\prog1



The command used to run the C++ compiler varies across compilers and operating systems.
$ g++ -o prog1 prog1.cc

Depending on the release of the GNU compiler you are using, you may need to specify -std=c++0x to turn on C++ 11 support.

Typically, we put all the #include directives for a program at the beginning of the source file.


std::cout << "Enter two numbers:" << std::endl; 
Our output statement uses the << operator twice. Because the operator returns its left-hand operand, the result of the first operator becomes the left-hand operand of the second.  

Equivalent to: (std::cout << "Enter two numbers:") << std::end;

 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.

Comments:

/* and */
//
Comment Pairs do not nest.



while (std::cin >> value)
Evaluating the while condition executes the expression std::cin >> value
The input operator (§ 1.2, p. 8) returns its left operand, which in this case is std::cin. This condition, therefore, tests std::cin.


When we use an istream as a condition, the effect is to test the state of the stream. If the stream is valid—that is, if the stream hasn’t encountered an error— then the test succeeds. An istream becomes invalid when we hit end-of-file or encounter an invalid input, such as reading a value that is not an integer. An istream that is in an invalid state will cause the condition to yield false.

 Entering an End-of-File from the Keyboard
When we enter input to a program from the keyboard, different operating systems use different conventions to allow us to indicate end-of-file. On Windows systems we enter an end-of-file by typing a control-z—hold down the Ctrl key and press z—followed by hitting either the Enter or Return key. On UNIX systems, including on Mac OS X machines, end-of-file is usually control-d.


Using File Redirection
It can be tedious to repeatedly type these transactions as input to the programs you are testing. Most operating systems support file redirection, which lets us associate a named file with the standard input and the standard output:
$ addItems <infile >outfile
Assuming $ is the system prompt and our addition program has been compiled into an executable file named addItems.exe (or addItems on UNIX systems), this command will read transactions from a file named infile and write its output to a file named outfile in the current directory.

 

Comments

Popular posts from this blog

C++ Primer Sec. 2.3 Compound Types

Compound Types References Reference is not an object. It is an alias. It is just another name for an already existing object. int ival = 1024; int &refVal = ival; //refVal referes to (is another name for) ival int &refVal2; // error: a reference must be initialized When we define a reference, instead of copying the initializer's value, we bind the reference to its initializer. Once initialized, a reference remains bound to its initial object. There is no way to rebind a reference to refer to a different object. Because there is no way to rebind a reference, references must be initialized. When we assign to a reference, we are assigning to the object to which the reference is bound. int i = refVal; //OKay i= 1024 Because references are not objects, we may not define a reference to a reference. We can define multiple references in a single definition. Each identifier that is a reference must be preceded by the & symbol: int ...

C++ Primer Sec. 2.1 2.2 Primitive Build-In Types and Variables

Primitive Build-in Types void type arithmetic type: bool, char, wchar_t, char16_t, char32_t, short, int, long, long long, float, double, long double Literals 'a' // character literal "Hello World!" // string literal L'a' // wide character literal, type is wchar_t u8"hi!" // utf-8 string literal (utf-8 encodes a Unicode character in 8 bits) 42ULL // unsigned integer literal, type is unsigned long long 1E-3F // single-precision floating-point literal, type is float 3.14159L // extended-precision floating-point literal, type is long double Variables Initialization is not assignment. Initialization happens when a variable is given a value when it is created. Assignment obliterates an object’s current value and replaces that value with a new one. Four different ways to define an int variable named units_sold and initialize it to 0. int units_sold = 0; int units_sold = {0}; int units_sold{0}; int units_sold(0); When used with va...