C++ Primer 5th - 1.3 关于注释
来源:互联网 发布:排版软件coreldraw 编辑:程序博客网 时间:2024/06/15 13:08
当我们的程序变得更加复杂之前,我们应该知道在C++中如何很好的处理注释。注释可以很好的帮助我们阅读代码。注释一般用于对一个算法的总结,声明变量的意图或是标明一段难以读懂的代码。编译器会无视注释,所以注释对程序的行为和效率没有任何影响。
C++里的注释
C++里有两种注释:一种是当行另一种是配对。当行注释由//开始已回车为结束。在//后面的所有代码将会被编译器无视。这种注释可以包括任何文本,包括额外的//。用/* */注释方法的注释继承自C语言。注释从/*开始到*/结束。这样的注释可以包括除了*/之外的所有文本。编译器会无视注释段里的所有代码。当这种注释包括了多行时,我们经常认为它是个多行注释。程序里通常会出现这两种风格的注释。注释对的形式通常用于多行注释,当行形式通常用于当行注释。
#include <iostream>/** Simple main function:Download at http://www.pin5i.com/C++ Primer, Fifth Edition* Read two numbers and write their sum*/int main(){ // prompt user to enter two numbers std::cout << "Enter two numbers:" << std::endl; int v1 = 0, v2 = 0; // variables to hold the input we read std::cin >> v1 >> v2; // read input std::cout << "The sum of " << v1 << " and " << v2 << " is " << v1 + v2 << std::endl; return 0;}
注释对不要嵌套
通常情况下,一个注释对不能出现在另一个注释对的内部。这种情况下,编译器会发出困惑的错误或警告信息。举个例子,在你的系统下编译如下代码:
/** comment pairs /* */ cannot nest.* ''cannot nest'' is considered source code,* as is the rest of the program*/int main(){ return 0;}
我们经常需要在debug时对注释掉一段代码。由于我们想要注释的代码段内部也有注释对,这样就会引起注释嵌套。最好的办法是在每行的开头加上//。如下所示:
// /*// * everything inside a single-line comment is ignored// * including nested comment pairs// */
0 0
- C++ Primer 5th - 1.3 关于注释
- C Primer Plus 5th 2.12.5
- C Primer Plus (5th Edition)
- 《C++Primer 5th》 读书笔记2--const
- C++Primer(5th)学习-数组篇
- C Primer Plus 5th 2.12.1
- C Primer Plus 5th 2.12.2
- C Primer Plus 5th 2.12.3
- C Primer Plus 5th 2.12.4*
- C Primer Plus 5th 2.12.6*
- C Primer Plus 5th 2.12.7*
- C++Primer 5th edition ex9_49
- C+++Primer+4th+中英文
- Primer C 4th(第5章习题)
- 《c.primer.plus 5th 》 P208页 菜单浏览
- C Primer Plus,5th Edition 复习(一)
- C Primer Plus,5th Edition 复习(二)
- C Primer Plus,5th Edition 复习(三) .
- XML的定义及应用
- 每一个认真的程序员都应该读的10本经典书籍
- Android Studio调试功能使用总结【转】
- C语言的宏定义(1)—— #if defined和#if !defined
- System.Web.HttpException: 超过了最大请求长度!解决办法[转帖]
- C++ Primer 5th - 1.3 关于注释
- bzoj2618凸多边形面积交
- Oracle Purge和drop的区别
- Android中dip、dp、sp、pt和px的区别
- USB设备驱动(1.1)——硬件特性
- 算法导论之红黑树的学习
- WebSocketClient
- 自动化测试和robot framework简介
- Memcached 缓存