Comments

From Rosetta Code
Revision as of 10:39, 30 March 2007 by 12.207.80.36 (talk) (Added Common Lisp section.)
Task
Comments
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.

Ada

-- All Ada comments begin with "--" and extend to the end of the line


BASIC

100 REM Standard BASIC comments begin with "REM" (remark) and extend to the end of the line

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 between any tokens. It 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
  (no more macro text)

the line (no 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. Note however that while the compiler doesn't see that text, the preprocessor does. Therefore some minimal rules still have to be followed. For example, the following code is not valid:

#if 0
This isn't valid.
#endif

That's because the preprocessor will interpret the apostrophe as beginning of a character constant, and will complain because that character constant isn't terminated with another apostrophe.

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. Note that the problem mentioned above cannot occur if there's valid code between the #if 0 and #endif.

Conditional compile "comments" can be nested:

#if 0
This is not compiled.
#if 0
Nor is this.
#endif
And this still is not compiled.
#endif

Common Lisp

; This is a single-line comment.
(defun foo ()
  "This function has a documentation string."
  (do-something))

Documentation strings are optional and may span multiple lines as string literals do. In addition to defun, many other constructs may also include documentation strings. Among these are defmacro, defmethod, defstruct, defclass, defvar, defparameter, and defconstant.

E

# This is a regular comment.
? "This is an Updoc comment, which
> is an executable example or test case.".split(" ")
# value: ["This", "is", "an", "Updoc", "comment,", "which
#        is", "an", "executable", "example", "or", "test", "case."]

Fortran

Compiler: ANSI F77 or compatible (like g77 -strict)

The first six columns in Fortran are traditionally reserved for labels and certain special characters. In particular a letter in the first column indicates a comment. This can be any character but is usually a "C" by convention:

C     This would be some kind of comment
C     Usually one would avoid columns 2-6 even in a comment.

Some Fortran compilers have the extension that comments starting with D are treated as non-comments if a special debugging flag is given at the compiler invocation. For example:

C     If compiled in debugging mode, print the current value of I
D     PRINT *, I

IDL

The comment character in IDL is the semicolon - everything starting with it and to the end of the line is a comment. Like this:

; The following computes the factorial of a number "n"
fact = product(indgen( n )+1) ; where n should be an integer

Java

Java comments are the same as C++.

JavaScript

JavaScript comments are the same as C++.

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.

Tcl

TCL follows the usual scripting language rules: a comment starts at a "#" symbol, which can be placed after a command if that is terminated by a semicolon:

# comment on a line by itself. The next is a command by itself:
set var1 $value1
set var2 $value2 ; # comment that follows a line of code