Comments: Difference between revisions
m (→[[LaTeX]]: typo) |
No edit summary |
||
Line 54: | Line 54: | ||
\complete |
\complete |
||
Without the percent sign after <tt>\firstpart</tt>, it would have been the ''two'' words "understand able". |
Without the percent sign after <tt>\firstpart</tt>, it would have been the ''two'' words "understand able". |
||
==[[Perl]]== |
|||
[[Category:Perl]] |
|||
'''Interpreter:''' [[Perl]] 5.x |
|||
Perl officially only has single line comments |
|||
# this is commented |
|||
These may also be at the end of a line |
|||
my $var = 1; # this is the comment part |
|||
The following is a hack which to some degree simulate multi-line comments |
|||
<<COMMENT; |
|||
Here are my comments |
|||
this is multi-line |
|||
COMMENT |
|||
While this is not stripped out at compile time, making it an unwise choice to use for leaving comments in a program, you can easily use this to disable a block of code and prevent it from executing during the development and testing stages. |
Revision as of 15:58, 12 March 2007
You are encouraged to solve this task according to the task description, using any language you may know.
Demonstrate all ways to include text in a language source file which is completely ignored by the compiler or interpreter.
C++
C++ has several ways to comment out some text. The first one is the traditional C-style comment:
/* This is a comment */
The C-style comment starts at the /*, and ends at the */. A C-style comment may be used betweein any tokens. They cannot be used inside tokens, that is, given the code
struct charisma {}; void f(char/* comment */isma) {}
the function takes an argument of type char, named isma, not an unnamed argument of type charisma.
C-style comments cannot be nested; that is, if you write
/* some comment /* trying to nest some other comment */ inside */
the comment ends at the first */, and inside */ is again interpreted as C++ source code (almost certainly causing a compile error).
The second way to comment text in C++ are so-called C++-style comments
// This is a comment
C++-style start with // and reach up to, but not including, the end of line (more exactly, up to the next unescaped newline). While formally, C++-style comments cannot be nested either, in practice they can:
// This is a valid comment // with a "nested" comment
That's because starting with the first // everything in the line is ignored, including the second //. The fact that the newline is not part of the comment is important for multi-line macro defnitions. It means that in the code
#define FOO \ (macro text) // comment (more macro text)
the line (more macro text) is not part of the macro definition. Also escaping the line break at the end of the comment with '\' doesn't help, because that would make the third line part of the comment instead. Comments inside macros therefore have to be C-style.
Finally, conditional compilation also can be used to make the compiler ignore some text:
#if 0 While technically not a comment, this is also ignored by the compiler #endif
The trick is that 0 is always false, therefore the text between #if 0 and #endif is never compiled. While this should never be used for actual comments, it's an easy way to comment out some code, especially because it doesn't interfere with normal (documentation) comments.
Conditional compile "comments" can be nested:
#if 0 This is not compiled #if 0 Nor is this. #endif And this still isn not compiled. #endif
LaTeX
In LaTeX, comments look like this:
% This is a comment
LaTeX comments start with % and continue up to and including the line break. The fact that the line break itself is commented out as well makes it useful for adding line breaks in the source code of complex macros without LaTeX interpreting them (which may cause extra space or even a paragraph break in the resulting typeset text). For example, the following results in the one word "understandable":
\newcommand{\firstpart}{understand} \newcommand{\secondpart}{able} \newcommand{\complete}{% \firstpart% \secondpart} \complete
Without the percent sign after \firstpart, it would have been the two words "understand able".
Perl
Interpreter: Perl 5.x
Perl officially only has single line comments
# this is commented
These may also be at the end of a line
my $var = 1; # this is the comment part
The following is a hack which to some degree simulate multi-line comments
<<COMMENT; Here are my comments this is multi-line COMMENT
While this is not stripped out at compile time, making it an unwise choice to use for leaving comments in a program, you can easily use this to disable a block of code and prevent it from executing during the development and testing stages.