空格和基本格式,c++里面的一些知识!

来源:互联网 发布:iptables禁止端口访问 编辑:程序博客网 时间:2024/06/12 00:35
空白是指字符用于格式化目的。在C + +,这主要是指空格、制表符和换行(有时)。c++编译器通常忽略空白,只有少许例外。

因此,下面的语句都作同样的事情:

cout << "Hello world!"; cout               <<            "Hello world!";         cout <<       "Hello world!"; cout    << "Hello world!";

即使最后声明在它编译的换行符刚刚好。
下面的函数都做同样的事情:

int add(int x, int y) { return x + y; } int add(int x, int y) {    return x + y; } int add(int x, int y){    return x + y; } int add(int x, int y){    return x + y;}

一个例外,c++编译器并注意空格内引用的文本,比如“Hello world !”。
“Hello world !”
相比是不同的
“Hello world !”

和每个打印出你想象的完全一样。换行不允许引用文本:

cout << "Hello     world!" << endl; // Not allowed!

另一个例外,c++编译器注意空格与/ /注释。单行注释只持续到这一行的结束。因此在做这样的事情会让你遇到麻烦:

cout << "Hello world!" << endl; // Here is a single-line commentthis is not part of the comment