简体   繁体   English

评论是否被翻译成机器代码? C ++

[英]Do comments get translated to machine code? C++

When a program written in C++ has comments, are those comments translated into machine language or do they never get that far? 当用C ++编写的程序有注释时,这些注释是否被翻译成机器语言,或者他们从未达到那么远? If I write a C++ program with an entire book amount of comments between two commands, will my program take longer to compile or run any slower? 如果我编写一个C ++程序,在两个命令之间有大量的注释,我的程序需要更长的时间才能编译或运行得更慢吗?

Comments are normally stripped out during preprocessing, so the compiler itself never sees them at all. 注释通常在预处理期间被删除,因此编译器本身根本不会看到它们。

They can (and normally do) slow compilation a little though--the preprocessor has to read through the entire comment to find its end (so subsequent code will be passed through to the compiler. Unless you include truly gargantuan comments (eg, megabytes) the difference probably won't be very noticeable though. 它们可以(并且通常会)稍微减慢编译速度 - 预处理器必须通读整个注释以找到它的结束(因此后续代码将传递给编译器。除非你包含真正庞大的注释(例如,兆字节)但差异可能不会很明显。

Although I've never seen (or heard of) a C or C++ compiler that did it, there have been compilers (eg, for Pascal) that used specially formatted comments to pass directives to the compiler. 虽然我从未见过(或听说过)C或C ++编译器,但是有一些编译器(例如Pascal)使用特殊格式的注释将指令传递给编译器。 For example, Turbo Pascal allowed (and its successor probably still allows) the user to turn range checking on and off using a compiler directive in a comment. 例如,Turbo Pascal允许(并且其继任者可能仍然允许)用户使用注释中的编译器指令来打开和关闭范围检查。 In this case, the comment didn't (at least in the cases of which I'm aware) generate any machine code itself, but it could and did affect the machine code that was generated for the code outside the comment. 在这种情况下,评论没有(至少在我知道的情况下)生成任何机器代码本身,但它可能并且确实影响为评论之外的代码生成的机器代码。

No, they are simply ignored by the compiler. 不,编译器会忽略它们。 Comments' sole purpose is for human reading, not machine. 评论的唯一目的是人类阅读,而不是机器。

The preprocessor eliminates comments.. Why should the compiler read them anyway? 预处理器消除了注释..为什么编译器仍然会读取它们? They are there to make it easier for people to understand the code.. Haven't you heard the joke "It's hard to be a comment, you always get ignored" :p 它们是为了让人们更容易理解代码。你有没有听过这个笑话“很难成为评论,你总是被忽视”:p

In the 3rd translation phase 在第3个翻译阶段

  • The source file is decomposed into comments , sequences of whitespace characters (space, horizontal tab, new-line, vertical tab, and form-feed) , and preprocessing tokens. 源文件被分解为注释 ,空白字符序列(空格,水平制表符,换行符,垂直制表符和换页符)以及预处理标记。

  • Each comment is replaced by one space character. 每个注释都被一个空格字符替换

See this cpprefference article for more information about the phases of translation 有关翻译阶段的更多信息,请参阅此cpprefference文章

No , they are removed by the preprocessor .You can check this by using cpp: The C Preprocessor . 不,它们被preprocessor删除。您可以使用cpp: The C Preprocessor来检查它。 Just write a simple C-program with comment and then use cpp comment.c | grep "your comment" 只需编写带注释的简单C程序,然后使用cpp comment.c | grep "your comment" cpp comment.c | grep "your comment" . cpp comment.c | grep "your comment"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM