Chapter 1. Getting Started(1)

1.1. Writing a Simple C++ Program

The operating system uses the value returned by main to determine whether the program succeeded or failed.

A return value of 0 indicates success.Usually a nonzero return indicates that an error occurred.

The main function is required to have a return type of int.

A function definition specifies four elements: the return type, the function name, a (possibly empty) parameter list enclosed in parentheses(圆括号), and the function body.

 

1.1.1. Compiling and Executing Our Program

Many PC-based compilers are run from an integrated development environment (IDE) that bundles the compiler with associated build and analysis tools. Most of these environments include a point-and-click interface(点击式界面) that allows the programmer to write a program and use various menus to compile and execute the program.

Most compilers, including those that come with an IDE, provide a command-line interface.

 

Invoking the GNU or Microsoft Compilers

The most common compilers are the GNU compiler and the Microsoft Visual Studio compilers. By default(默认地) the command to invoke the GNU compiler is g++:

 

1.2. A First Look at Input/Output

A stream is a sequence of characters intended to be read from or written to an IO device of some kind. The term "stream" is intended to suggest that the characters are generated, or consumed, sequentially over time.

 

1.2.1. Standard Input and Output Objects

 IO objects:

1、To handle input, we use an object of type istream named cin (pronounced "see-in"). This object is also referred to as the standard input.

2、For output, we use an ostream object named cout (pronounced "see-out"). 

3、The cerr(pronounced "see-err" ) object, referred to as the standard error, is typically used to generate warning and error messages to users of our programs.

4、The clog(pronounced "see-log") object is used for general information about the execution of the program.

Cout、cerr and clog are often referred to as the standard output.

 

Most operating systems give us a way of redirecting(重定向) the input or output streams when we run a program.

 

1.2.2. A Program that Uses the IO Library

In the case of the output operator, the result is the value of its left-hand operand(左操作数). That is, the value returned by an output operation is the output stream itself.

The statement that prints our prompt is equivalent to

(std::cout << "Enter two numbers:") << std::endl;
Because (std::cout << "Enter two numbers:") returns its left operand, std::cout, this statement is equivalent to

std::cout << "Enter two numbers:";
std::cout << std::endl;

endl is a special value, called a manipulator(操纵符), that when written to an output stream has the effect of writing a newline to the output and flushing the buffer(缓冲区) associated with that device.

endl is a special value, called a manipulator, that when written to an output stream has the effect of writing a newline to the output and flushing the buffer associated with that device.

 

Using Names from the Standard Library

 

Namespaces allow programmers to avoid inadvertent collisions with the same names defined by a library. Because the names that the standard library defines are defined in a namespace, we can use the same names for our own purposes.

 

When writing a C++ program, in most places that a space appears we could instead use a newline. One exception to this rule is that spaces inside a string literal cannot be replaced by a newline. Another exception is that spaces are not allowed inside preprocessor directives.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值