Comments: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 1,475: Line 1,475:
- This is a comment
- This is a comment
</lang>
</lang>

=={{header|TXR}}==

<lang txr>@# comment to end of line</lang>


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==

Revision as of 00:11, 30 September 2011

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.

4D

<lang 4d>`Comments in 4th Dimension begin with the accent character and extend to the end of the line.</lang>

6502 Assembly

Note: syntax depends on the assembler software but use of a semicolon is fairly standard <lang 6502 Assembly> nop  ; comments begin with a semicolon</lang>

8086 Assembly

Note: syntax depends on the assembler software but use of a semicolon is fairly standard <lang 8086 Assembly> MOV AX, 4C00h ; go back to DOS INT 21h  ; BIOS interrupt 21 base 16</lang>

ActionScript

See Java

Ada

<lang ada>-- All Ada comments begin with "--" and extend to the end of the line</lang>

ALGOL 68

With Standard

Comments can be inserted in variety of ways:

Algol68 as typically published,

includes bold typeface.

Quote stropping,

like to Wiki markup.

Case stropping,

7-bit/ascii implementations.

Res stropping,

detecting reserved words.

Point stropping,

6-bits/byte implementations.

¢ The original way of adding your 2 cents worth to a program with the "cent" character ¢ ¢ TEXT ¢
co Style i comment co

comment text comment

'co' text 'co'

'comment' text 'comment'

CO text CO

COMMENT text COMMENT

co text co

comment text comment

.CO TEXT .CO

.COMMENT TEXT .COMMENT

# Style ii comment with the hash character # # TEXT #

Notes:

  • The # test # and ¢ text ¢ comment tends to be used for inline comments. And the COMMENT text COMMENT style tends to be used to comment out entire blocks.
  • The script algol68.vim can be used to highlight commented blocks while editing source code.

With Extensions

£ This is a hash/pound comment for a UK keyboard £

AmigaE

<lang amigae>/* multiline comment are like C ... */ -> this is a end of line comment</lang>

AutoHotkey

<lang AutoHotkey>Msgbox, comments demo ; end of line comment /* multiline comment1 multiline comment2

  • /</lang>

For multi line comments, the /* and */ must be on their own separate lines (i.e. nothing else can be on the same line).

AutoIt

<lang AutoIt>

  1. cs

Everything between the cs and and the ce is commented. Commented code is not used by the computer.

  1. ce
individual lines after a semicolon are commented.

</lang>

AWK

The hash symbol # start a comment; it ends at the end of line.

<lang awk>BEGIN { # this code does something

 # do something

}</lang>

BASIC

Works with: Applesoft BASIC
Works with: GW-BASIC
Works with: ZX Spectrum Basic

The only truly standard method of marking a comment in BASIC is using the REM keyword. This dates back to (at least) the late 1970's, and should work with most BASICs available today:

100 REM Standard BASIC comments begin with "REM" (remark) and extend to the end of the line
110 PRINT "this is code": REM comment after statement
Works with: QuickBASIC version 4.5

Most BASICs also support alternate comment characters, commonly an apostrophe (single quote):

'this is a comment
PRINT "this is code"  'comment after statement
Works with: DarkBASIC

Characters other than apostrophe are used in some BASICs. For example, DarkBASIC uses a back-tick character (`, a.k.a. grave accent):

`this is a comment
PRINT "this is code" `comment after statement
'this is NOT a comment!
Works with: FreeBASIC version 0.16 or later

In addition to single-line comments, a few BASICs support block comments. FreeBASIC was influenced by the C family's block comment characters:

/' This is a multi line comment.
Requires FreeBASIC 0.16 or later.
Last line of the comment block. '/

DIM a AS /' Comment in the middle of statement '/ Integer

Batch File

<lang dos>rem Single-line comment.</lang>

There is another (unsupported) option, using a double-colon ::. However, this has issues with some syntactic constructs and therefore may raise syntax errors. <lang dos>:: Another option, though unsupported and known

to fail in some cases. Best avoided.</lang>

Since comment lines are skipped entirely by the parser multi-line comments aren't possible even with line continuation.

BBC BASIC

<lang bbcbasic> REM This is a comment which is ignored by the compiler

     *| This is a comment which is compiled but ignored at run time</lang>

bc

<lang bc>/* This is a comment. */

2 + /* Comment between tokens. */ 3

"This is a string, /* NOT a comment */."

/*

* A comment can have multiple lines. These asterisks in the middle
* of the comment are only for style. You must not nest a comment
* inside another comment; the first asterisk-slash ends the comment.
*/</lang>

Works with: GNU bc
Works with: OpenBSD bc

<lang bc>#!/usr/bin/bc

  1. This form of comment is an extension, not part of standard bc.
  1. Your program must not have a #! shebang line
  2. unless your bc supports this form of comment.

2 + 3 # It can be after a statement.

a = 1 # The newline is not part of the comment. b = 2 # So this line is a different statement.</lang>


Befunge

Like Brainf***, all characters and whitespace which are not commands are ignored. Also, since the code/data-space is two-dimensional, comments can be placed anywhere that will be untouched by the instruction pointer and data access commands. Finally, in Funge-98, the ; instruction immediately skips to the next ; instruction, which allows to isolate comments from code.

Works with: Befunge version 93

<lang befunge>& read a number 2+ add two .@ display result and exit

 ^- inline comments -^     <-^- other comments</lang>
Works with: Befunge version 98

<lang befunge>&;read a number;2+;add two;.@;display result and exit;

 ^- inline comments -^     <-^- other comments
completely isolated comment block for the paranoid;
(almost - you can still skip into it.)  ;
</lang>

Blast

<lang blast># A hash symbol at the beginning of a line marks the line as a comment </lang>

Brainf***

<lang bf>This is a comment</lang>

Most ASCII characters may be used for comments; only the eight characters "+-<>[],." are Brainf*** commands. Extra care must be used when using punctuation, particularly the comma or period. These are I/O operators and are actually commands rather than comments, and are instead compiled into the program if used and may have to be "debugged" and removed if you forget this issue.

Brat

<lang brat># Single line comment

    • Multi
  Line
  Comment *#</lang>

Brlcad

<lang brlcad>

# Comments in mget scripts are prefixed with a hash symbol
ls   # comments may appear at the end of a line

</lang>

C

<lang c>/* This is a comment. */ /* So is this

  multiline comment.
*/</lang>

The comment starts at the /*, and ends at the */. A comment may be used between any tokens. It cannot be used inside tokens, that is, given the code <lang c>struct charisma {}; void f(char/* comment */isma) {}</lang> the function takes an argument of type char, named isma, not an unnamed argument of type charisma.

Comments cannot be nested; that is, if you write <lang c>/* some comment /* trying to nest some other comment */ inside */</lang> the comment ends at the first */, and inside */ is again interpreted as source code (almost certainly causing a compile error). Some compilers have the option to allow nested comments, but this is not a standard feature.

Conditional compilation also can be used to make the compiler ignore some text: <lang c>#if 0 While technically not a comment, this is also ignored by the compiler

  1. endif</lang>

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: <lang c>#ifdef UNDEFINED This is not compiled.

  1. if 0

Nor is this.

  1. endif

And this still is not compiled.

  1. endif</lang>
Works with: ANSI

Even though the compiler doesn't see #if 0 text, the preprocessor does. Therefore some minimal rules still have to be followed. For example, the following code is not valid: <lang c>#if 0 This isn't valid.

  1. endif</lang>

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.

Note that the problem mentioned above cannot occur if there's valid code between the #if 0 and #endif.

Works with: C99

<lang c>// C++ single-line comments were adopted in the C99 standard.</lang>

C++

See also C

Single line C++-style comments <lang cpp>// This is a comment</lang> C++-style comments 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: <lang cpp>// This is a valid comment // with a "nested" comment</lang> 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 definitions. It means that in the code <lang cpp>#define FOO \

 (macro text) // comment
 (no more macro text)</lang>

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.

C#

<lang csharp>//This is a comment. //This is other comment.

/* This is a comment too. */

/* This is a multi-line comment */</lang>

Chef

<lang Chef>Comment Stew.

This is a comment. The other comment is a loop, but you can name it anything (single word only). You can also name ingredients as comments This is pseudocode.

Ingredients. Ingredient list

Method. Methods. SingleWordCommentOne the Ingredient. Methods. SingleWordCommentTwo until SingleWordCommentOned. Methods.</lang>

Clean

Clean comments are similar to C++. <lang clean>Start = /* This is a multi-

          line comment     */ 17 // This is a single-line comment</lang>

In contrast to C++ comments can be nested. <lang clean>Start = /* This is a comment /* Nested comment */ still a comment */ 17</lang>

Clojure

Anything from a semicolon to the end of a line is a comment.

<lang lisp>;; This is a comment (defn foo []

 123) ; also a comment</lang>

The (comment) macro will prevent a form from being evaluated, returning nil no matter what is contained in the comment. However the forms inside the comment form must be properly parseable (parentheses balanced, etc.) or an exception will be thrown.

<lang lisp>(comment (println (foo)) "bar" :baz 123 (System/exit 0))  ;; does nothing, returns nil</lang>

Finally, the #_ reader macro will cause a form to be ignored by the reader. Unlike (comment), this does not return nil; the surrounding code is evaluated as though the ignored form isn't even there.

<lang lisp>(+ 1 (comment "foo") 3)  ;; Throws an exception, because it tries to add nil to an integer (+ 1 #_"foo" 3)  ;; Returns 4</lang>

CoffeeScript

<lang coffeescript># one line comment

      1. multi

line comment ###</lang>

ColdFusion

In tags: <lang coldfusion>As ColdFusion's grammar is based around HTML syntax, commenting is similar to HTML. Note that in the examples below, you will need to remove the space between the < and the !. < !--- This is a comment. Nothing in this tag can be seen by the end user.

      Note the three-or-greater dashes to open and close the tag. --->

< !-- This is an HTML comment. Any HTML between the opening and closing of the tag will be ignored, but any ColdFusion code will still run.

      Note that in the popular FuseBox framework for ColdFusion, the circuit.xml files require that you use this style of comment. --></lang>

In script: <lang coldfusion>/* This is a comment */ // This is also a comment</lang>

Common Lisp

Common Lisp provides line comments (;) and block comments (#|...|#).

Block comments can nest (#|...#|...|#...|#), unlike block comments in e.g. C.

In a common convention, header comments are prefaced with four semicolons, top-level (function level) comments use three, comments for sections of code use two, and margin comments use one.

<lang lisp>;;;; This code implements the foo and bar functions

The foo function calls bar on the first argument and multiplies the result by the second.
The arguments are two integers

(defun foo (a b)

  ;; Call bar and multiply
  (* (bar a) ; Calling bar
     b))
The bar function simply adds 3 to the argument

(defun bar (n)

  (+ n 3))</lang>

However, comments should not be used for inline documentation, as most defining constructs permit a documentation string (which is then available at runtime).

<lang lisp>(defun bar (n)

 "Add 3 to the argument."
 (+ n 3))

(defclass button (widget)

 (label action)
 (:documentation "This is a push-button widget."))</lang>

D

Comments mostly work similar to C. Newlines are irrelevant. <lang d>/* This is a simple C-style comment */ // this also is</lang> This type of comments can be nested. <lang d>/+ Nestable comment

 /+ See?
 +/

+/ Yay! I'm code!</lang>

Documentation Comments

D also has a built-in tool for generating documentation from comments (DDOC). Documentation comments look like this: <lang d>/// I'm documentation! /** I'm multi- line documentation. */ /++ And I

 /++ can be nested +/ +/

</lang> DDOC is designed to read like natural language. For example, this is a valid DDOC comment: <lang d> /*****************************************

  • This function solves world hunger.
  • Examples:
  • ----------------------------------------
  • feed_world (earth);
  • ----------------------------------------
  • Date: May 4, 2011
  • Bugs: Does not solve world thirst.
  • See_Also:
  • SoylentGreen
  • Authors: J. Random Hacker. */

</lang>

dc

There is no comment syntax in POSIX dc. The convention is to make a string on the stack and move it to an unused register; a no-op. <lang dc>[Making and discarding a string acts like a comment] sz</lang> GNU dc added the comment syntax of many other scripting languages. <lang dc># remainder of line is a comment</lang>

Deluge

Comments are only allowed in places such as "on load" scripts. You cannot put them in form or view definitions.

<lang deluge>// single line comment</lang>

Delphi

See also Pascal

In addition, Delphi also allows C++ style single line comments: <lang delphi>// single line comment</lang>

E

<lang e># This is a regular comment.

? "This is an Updoc comment, which > is an executable example or test case.".split(" ")

  1. value: ["This", "is", "an", "Updoc", "comment,", "which
  2. is", "an", "executable", "example", "or", "test", "case."]</lang>

All comments span to the end of the line; there are no paired-delimiter comment syntaxes. “#” begins a comment anywhere outside of quotes; “?” and “>” begin comments only if they are at the beginning of a line (except for whitespace), because those characters are also used for infix operators.

In Updoc, “?” indicates the beginning of a program fragment, “>” the continuation of one, and “#” begins the expected output from its evaluation; “??” indicates special directives.

Ela

<lang Ela>//single line comment

/*multiple line comment*/</lang>

Emacs Lisp

A comment is started by ; and reaches to the end of the line. <lang lisp>; This is a comment</lang> Another way to add comments is to use strings at places where the result of an expression is ignored, since they simply evaluate to themselves without any effect. Note that strings can be multi-line: <lang lisp>"This is effectively a comment, if used at a place where the result is ignored"</lang> Note that strings at the beginning of function definitions are interpreted as documentation strings for the function (i.e. Emacs will display them if asked for help about the function), e.g. <lang lisp>(defun subtract-second-from-first (x y)

 "This function subtracts its second argument from its first argument."
 (- y x))</lang>

Due to this, it's debatable if the string at that place can be considered as comment.

Erlang

<lang erlang>% Erlang comments begin with "%" and extend to the end of the line.</lang>

Euphoria

Single line comment: <lang Euphoria>-- This is a comment</lang>


Multiline C-style comment: <lang Euphoria>/* This is a comment

  • /</lang>
Works with: Euphoria version 4.0.0

Factor

<lang factor>! Comments starts with "! "

  1. ! Or with "#! "

! and last until the end of the line </lang>

Fancy

<lang fancy># Comments starts with "#"

  1. and last until the end of the line

</lang>

Falcon

Falcon supports C-language style single line and block comments. A single line comment begins with two slashes (//) and ends at the end of the line. A block comment begins with a slash followed by an asterisk, and terminates when an asterisk followed by a slash is met (/*...*/). <lang falcon> /* Start comment block

My Life Story
*/

// set up my bank account total bank_account_total = 1000000 // Wish this was the case </lang>

FALSE

<lang false>{comments are in curly braces}</lang>

Fish

Since ><> is a funge-like language, all characters not touched by the command pointer or modified by the p and g commands can be comments. Unlike Brainf***, unknown commands are not ignored by the compiler, they just raise an error. <lang Fish>v This is the Fish version of the Integer sequence task >0>:n1+v all comments here

 ^o" "< still here

And of course here :)</lang>

Forth

Standard Forth includes a number of ways to add comment text. As with everything in Forth, comment characters are actually words that control the compiler. <lang forth>\ The backslash skips everything else on the line ( The left paren skips everything up to the next right paren on the same line)</lang>

Traditionally, the paren comments are used for "stack effect" notation: <lang forth>: myword ( a b -- c ) ...</lang>

This comment means "myword takes two cells on the stack and leaves one". Sometimes, stack effect comment names give clues about the word's function: <lang forth>: add'em ( a b -- a+b ) + ;

strlen ( addr -- len ) count nip ;</lang>

Some Forth systems implement other commenting words, such as these words from Win32Forth: <lang forth>\s skips all remaining text in the file (( skips until the next double-paren,

  stretching across multiple lines ))

comment:

  Ignore all text in this section

comment; doc

  Another comment block

enddoc /* C-style comment */ (* Pascal-style comment *)</lang>

Fortran

Compiler: ANSI FORTRAN 77 or compatible (like g77 -strict)

The first six columns in Fortran are traditionally reserved for labels and certain special characters. In particular the letter "C" in the first column indicates a comment:

<lang fortran>C This would be some kind of comment C Usually one would avoid columns 2-6 even in a comment.</lang>

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:

<lang fortran>C If compiled in debugging mode, print the current value of I D PRINT *, I</lang>

ISO Fortran 90 or later have an inline comment (!) syntax:

<lang fortran>real :: a = 0.0  ! initialize A to be zero</lang>

In ISO Fortran 90 or later, "C in first column" comments are only allowed in the "fixed" source form familiar to FORTRAN 77 programmers. The "free" source form only has inline comments (!}.

ISO Fortran 95 or later has an optional conditional compilation syntax. If present, it can be used (abused?) to (in effect) comment out blocks of code:

<lang fortran>?? if (.false.) then do while (oh_no)

  a = bad_news()
  b = big_mistake()
  c = gigo()

end do ?? end if</lang>

F#

F# accepts C++ type line comments and OCaml type block comments <lang fsharp>// this comments to the end of the line (* this comments a region

  which can be multi-line *)</lang>

Gambas

In gambas, comments can be inserted by prefixing them with an apostrophe. The gambas interpreter will ignore the apostrophe and any other characters that follow it until the end of the line:

<lang gambas>

' This whole line is a comment and is ignored by the gambas interpreter
print "Hello" ' Comments after an apostrophe are ignored

</lang>

GAP

<lang gap># Comment (till end of line)</lang>

Gema

<lang gama>! comment starts with "!" and continues to end of line</lang> A shebang (#!) may be used as a comment in the first line of a file.

GML

single-line comment: <lang GML> // comment starts with "//" and continues to the end of the line</lang>

multi-line comment: <lang GML> /* a multi-line comment starts with slash-asterisk and, ends with asterisk-slash. also note:

* A multi-line comment is ignored inside a string
* A multi-line comment can be ended inside a line
  • /</lang>

Go

<lang go>// this is a single line comment /* this is

  a multi-line
  block comment.

/* It does not nest */</lang>

Golfscript

<lang golfscript># end of line comment</lang>

Groovy

See Java

GW-BASIC

Works with: GW-BASIC

<lang qbasic>100 REM Standard BASIC comments begin with "REM" (remark) and extend to the end of the line 110 PRINT "this is code": REM comment after statement</lang>

Haskell

<lang haskell>i code = True -- I am a comment.

{- I am also

  a comment. {-comments can be nested-}
  let u x = x x (this code not compiled)
  Are you? -}

-- |This is a Haddock documentation comment for the following code i code = True -- ^This is a Haddock documentation comment for the preceding code

{-|

 This is a Haddock documentation block comment

-} i code = True</lang>

haXe

<lang haxe>// Single line commment.

/*

  Multiple
  line
  comment.
  • /</lang>

HicEst

<lang hicest>! a comment starts with a "!" and ends at the end of the line</lang>

HTML

<lang html></lang>

Icon and Unicon

Any text after "#" is a comment. <lang Icon># This is a comment

procedure x(y,z) #: This is a comment and an IPL meta-comment for a procedure </lang> The The Icon Programming Library established conventions for commenting library additions and functions. This included both header block comments and meta comments on procedures within library files.

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:

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

Inform 7

<lang inform7>[This is a single-line comment.]

[This is a multi-line comment.]

[Comments can [be nested].]</lang>

Io

<lang io># Single-line comment

// Single-line comment

/* Multi-line

  comment */</lang>

J

<lang j>NB. Text that follows 'NB.' has no effect on execution.

0 : 0 Multi-line comments may be placed in strings, like this. )

Note 'example' Another way to record multi-line comments as text is to use 'Note', which is actually a simple program that makes it clearer when defined text is used only to provide comment. )</lang>

Java

Java has two ways to enter normal comments, plus a third type of comment that doubles as a way to generate HTML documentation.

C Style

<lang java>/* This is a comment */</lang>

<lang java>/*

* This is
* a multiple
* line comment.
*/</lang>

This C-style comment starts with /* and ends with */. The two delimiters may be on the same or separate lines. This style comment may be used anywhere white space is permitted.

C++ Style (inline)

<lang java>// This is a comment</lang> This C++-style comment starts with // and extends to the end of line.

Java Documentation (Javadoc)

<lang java>/** This is a Javadoc comment */</lang>

<lang java>/**

* This is
* a multiple
* line Javadoc comment
*/</lang>

Javadoc is a standardized documentation code for Java. Its comments begin with a forward slash and two stars. Javadoc comments have different tags that signify different things in the methods and classes that they precede.

JavaScript

See Java

Joy

<lang joy># this is a single line comment

(* this is a multi-line comment *)</lang>

Multi-line comments cannot be nested.

KonsolScript

<lang KonsolScript>//This is a comment. //This is another comment.

/* This is a comment too. */

/* This is a multi-line comment */</lang>

LaTeX

In LaTeX, comments look like this: <lang latex>% This is a comment</lang> 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": <lang latex>\newcommand{\firstpart}{understand} \newcommand{\secondpart}{able} \newcommand{\complete}{% \firstpart% \secondpart}

\complete</lang> Without the percent sign after \firstpart, it would have been the two words "understand able".

Liberty BASIC

<lang lb>'This is a comment REM This is a comment

print "This has a comment on the end of the line." 'This is a comment print "This also has a comment on the end of the line." : REM This is a comment</lang>

<lang logo>; comments come after a semicolon, and last until the end of the line</lang>

Logtalk

<lang logtalk>% single-line comment; extends to the end of the line</lang> <lang logtalk>/* multi-line comment */</lang>

LotusScript

LotusScript has two ways to enter comments. <lang lotusscript>' This is a comment</lang> Wherever the single quote (') is used, the rest of the line is treated as a comment and ignored. Multi-line comments would each need a single quote mark. This style of comment is usually used for making small in-line or single line comments. <lang lotusscript>%REM This is a multi- line comment. %END REM</lang> A %REM marker begins a comment block, and a %END REM marker ends the comment block. This style of comment is used for making longer multi-line comments, often at the beginning of a class, sub or function.

LSE64

<lang lse64># single line comment (space after # is required)</lang> The author of LSE comments the stack effect of words with header comments as follows: <lang lse64># arg1 arg2 yields result|nothing</lang>

Lua

<lang lua>-- A single line comment

--[[A multi-line

   comment --]]</lang>
Works with: Lua version 5.1 and above

<lang lua>--[====[ A multi-line comment that can contain many square brackets ]====]</lang>

M4

<lang M4>eval(2*3) # eval(2*3) "#" and text after it aren't processed but passed along dnl this text completely disappears, including the new line divert(-1) Everything diverted to -1 is processed but the output is discarded. A comment could take this form as long as no macro names are used. divert</lang>

Output:

6  # eval(2*3)  "#" and text after it aren't processed but passed along

Mathematica

<lang Mathematica>(*this is a comment*)</lang> It can be used everywhere and nested if necessary: <lang Mathematica>If[a(*number 1*)<(* is smaller than number 2*) b, True (*return value (*bool true*)*), False (*return bool false*)]</lang> evaluates to: <lang Mathematica>If[a < b, True, False]</lang>

MATLAB

<lang MATLAB>%This is a comment %% Two percent signs and a space are called a cell divider</lang>

MAXScript

<lang maxscript>-- Two dashes precede a single line comment

/* This is a

  multi-line comment */</lang>

MBS

<lang mbs>! A pling in a line starts a comment

INT n:=5  ! Comments can appear at the end of a line

/* A comment block can also be defined using climbstar and starclimb symbols.

 This allows comments to be stretched across several lines */</lang>

Metafont

<lang metafont>% this is "to-end-of-line" comment</lang>

Mirah

<lang mirah>puts 'code' # I am a comment /* This is

* a multiple
* line comment */

</lang>

Modula-2

<lang Modula-2>(* Comments (* can nest *)

  and they can span multiple lines.
*)</lang>

Modula-3

<lang modula3>(* Comments (* can nest *)

  and they can span multiple lines.
*)</lang>

MOO

<lang moo>"String literals are technically the only long-term comment format"; // Some compilers will, however, compile // one-liners to string literals as well (and vice-versa) /* Classical C-style comments are removed entirely during compile */</lang>

Nemerle

<lang Nemerle>// This comment goes up to the end of the line /* This is a multiline comment */</lang>

NetRexx

NetRexx supports block-comments and line comments. Block comments are started with a /* and termineded with a */. Line comments follow a -- sequence anywhere on a line. <lang NetRexx>/*

NetRexx comment block
  • /

-- NetRexx line comment

</lang>

NewLISP

A comment is started by ; and reaches to the end of the line. <lang lisp>; This is a comment</lang>

NSIS

<lang nsis>

  1. This is a comment that goes from the # to the end of the line.
This is a comment that goes from the ; to the end of the

/* This is a multi-line comment */ </lang>

Objective-C

See C

Objeck

<lang objeck>

  1. This is a comment.
  2. This is other comment.
  1. ~ This is a comment too. ~#
  1. ~ This is a

multi-line comment ~# </lang>

OCaml

<lang ocaml>(* This a comment

  (* containing nested comment *)
*)

(** This an OCamldoc documentation comment *)</lang>

Octave

<lang octave># I am a comment till the end of line % I am a comment till the end of line</lang>

Openscad

The openscad geometry compiler supports C++ style comments: <lang openscad> // This is a single line comment

/*

 This comment spans
 multiple lines
  • /

</lang>

Oz

<lang oz>% one line comment

%% often with double "%" because then the indentation is correct in Emacs

/* multi line

  comment
  • /

</lang>

PARI/GP

Comments are similar to C. The block comment is identical: /* comment */. The line comment uses backslashes instead of slashes: \\ comment.

Pascal

<lang pascal>(* This is a comment.

  It may extend across multiple lines. *)

{ Alternatively curly braces

 can be used. }

(* This is a valid comment in Standard Pascal,

  but not valid in Turbo Pascal. }

{ The same is true in this case *)</lang>

In Pascal, comments cannot be nested.

PASM

<lang pasm># This is a comment print "Hello\n" # This is also a comment end</lang>

Perl

Works with: Perl version 5.x

Single line comment

<lang perl># this is commented</lang>

These may also be at the end of a line

<lang perl>my $var = 1; # this is the comment part</lang>

Multi-line comments for inline documentation (Plain Old Documentation, or POD in Perl parlance) follow the format:

<lang perl>=pod

Here are my comments this is multi-line

=cut</lang>

Note that technically, both of the lines beginning with the equals sign must be surrounded on either side for compatibility with all "POD" parsers.

Note also that any string beginning with an equals sign, and that appears in the initial column of a line, begins a multi-line comment. It does not have to be a POD "command:" the following are all valid:

<lang perl>=head1 =head4 =over 4 =Any Old String</lang>

Such blocks always end in =cut.

For more info, type at a command prompt (or into a search engine): "perldoc perlpod"

Perl 6

Works with: Rakudo version #22 "Thousand Oaks"

<lang perl6>my $x = 2; # Single-line comment.

  1. `(
   Comments beginning with a backtick and one or more
   opening bracketing characters are embedded comments.
   They can span more than one line…

)

my $y = #`{ …or only part of a line. } 3;

  1. `{{
   Using more than one bracketing character lets you include
   an unmatched close bracket, like this: }

}}

  1. `⁅ Synopsis 2: "Bracketing characters are defined as any
   Unicode characters with either bidirectional mirrorings or
   Ps/Pe/Pi/Pf properties." ⁆ 

=begin comment

Pod is the successor to Perl 5's POD. This is the simplest way to use it for multi-line comments. For more about Pod, see Synopsis 26:

http://perlcabal.org/syn/S26.html

=end comment</lang>

PHP

Single line comment:

<lang php># this is commented // this is commented</lang>

These may also be at the end of a line:

<lang php>$var = 1; # this is the comment part $var = 1; // this is the comment part</lang>

Basic syntax for multi-line comments:

<lang php>/* Here are my comments this is multi-line

  • /</lang>

Note that; it is more common to see phpDocumentor styled multi-lined comments:

<lang php>/**

* phpdoc Comments
* @todo this is a todo stub
*/</lang>

PicoLisp

<lang PicoLisp># The rest of the line is ignored

  1. {
  This is a
  multiline comment

}# NIL Immediately stop reading this file. Because all text in the input file following a top-level 'NIL' is ignored.

This is typically used conditionally, with a read-macro expression like `*Dbg so that this text is only read if in debugging mode.</lang>

Pike

<lang pike>// This is a comment. /* This is a

  multi
  line
  comment */

int e = 3; // end-of-statement comment.</lang>

Plain TeX

The default raw/bare TeX assigns the category code 14 (comment character) to the character %, and plainTeX, as also LaTeX (see here Comments in LaTeX, does not change it; so the % starts a to-end-of-line comment in many TeX macro packages.

<lang tex>% this is a comment This is not.</lang>

The final newline character is eaten and since it normally behaves like a space, the comment can be used to hide the newline:

<lang tex>\def\firstpart{understand} \def\secondpart{able} \def\complete{\firstpart% \secondpart}

\complete</lang>

Outputs understandable; without % it would output understand able.

PL/I

<lang PL/I> /* This is a comment. */ </lang>

PL/SQL

Single line comment:

<lang plsql>--this is a single line comment</lang>

Multiline comment:

<lang plsql>/* this is a multiline comment

  • /</lang>

End of line comment:

<lang plsql>v_var number; --this is an end of line comment</lang>

Pop11

Pop11 has two kinds of comments: endline and C-like. Endline comment begins with tree consecutive semicolons and ends at the end of line:

<lang pop11>;;; This is a comment</lang>

C-like comments may be multiline:

<lang pop11>/* First line

  Second line */</lang>

C-like comments (unlike C) may be nested:

<lang pop11>/* This is a comment /* containing nested comment */ */</lang>

One can also use conditional compilation to comment out sections of code

<lang pop11>#_IF false some code

  1. _ENDIF</lang>

however, commented out part must consist of valid Pop11 tokens. In particular, C-like comments must balance and strings must be terminated. The following is an error:

<lang pop11>#_IF false This w'ont work

  1. _ENDIF</lang>

because apostrophe starts an unterminated string.

PostScript

<lang> %This is a legal comment in PostScript </lang>

PowerShell

<lang powershell># single-line comment</lang>

Works with: PowerShell version 2

<lang powershell><# multi-line

  comment #></lang>

Prolog

<lang prolog>% this is a single-line comment that extends to the end of the line</lang> <lang prolog>/* This is a multi-line comment */</lang>

Protium

Protium encloses all comments inside <@ OMT></@> (fixed length opcode) or <# OMIT></#> (variable length opcode) whether single- or multi- line. <lang html> <@ OMT>This is a multiline comment</@> </lang> OMT suppresses evaluation of everything contained. There are a variety of extra opcodes which can be used to control how OMT functions at run time.

PureBasic

PureBasic uses the ";" symbol to mark its comments. All text entered after ";" on a line is ignored by the compiler. <lang PureBasic>;comments come after an unquoted semicolon and last until the end of the line foo = 5 ;This is a comment c$ = ";This is not a comment" ;This is also a comment</lang>

Python

Python uses the "#" symbol to mark it's comments. After placing a "#", everything to the right of it in that line will be ignored.

# This is a comment
foo = 5 # You can also append comments to statements

Certain 'do nothing' expressions resemble comments <lang python>"""Un-assigned strings in triple-quotes might be used

  as multi-line comments

"""

  "triple quoted strings" can be delimited by either 'single' or "double" quote marks; and they can contain mixtures
  of other quote marks without any need to \escape\ them using any special characters.  They also may span multiple
  lines without special escape characters.

</lang>

Note that strings inserted among program statements in Python are treated as expressions (which, in void context, do nothing). Thus it's possible to "comment out" a section of code by simply wrapping the lines in "triple quotes" (three consecutive instances of quotation marks, or of apostrophes, and terminated with a matching set of the same).

Documentation Strings

Python makes pervasive use of strings which immediately follow class and function definition statements, and those which appear as the first non-blank, non-comment line in any module or program file. These are called "documentation" strings or "docstrings" for short; and they are automatically associated with the __doc__ attribute of the class, function, or module objects in which they are defined. Thus a fragment of code such as: <lang python>#!/usr/bin/env python

  1. Example of using doc strings

"""My Doc-string example"""

class Foo:

    Some documentation for the Foo class
    def __init__(self):
       "Foo's initialization method's documentation"

def bar():

   """documentation for the bar function"""

if __name__ == "__main__":

   print (__doc__)
   print (Foo.__doc__)
   print (Foo.__init__.__doc__)
   print (bar.__doc__)</lang>


... would print each of the various documentation strings in this example. (In this particular example it would print two copies of the first doc string which because __doc__ in the "current" name space is the same as __main__.__doc__ when our program is running as a script). If some other script were to import this file (under the name "example" perhaps) then "My Doc-string example" would be the value of example.__doc__

Python "docstrings" are used by a number of tools to automatically generate documentation (for most of the Python standard libraries, classes, functions, etc, as well as for user programs which define docstrings). They are also used by tools such as doctest to automatically derive test suites from properly formatted examples of class instantiations, function invocations and other usage samples. The standard pydoc utility can search through Python source trees generating documentation and can function as a local web server allowing a programmer to browse "live" hyperlinked documentation of their project.

(As noted above extraneous strings interspersed throughout a Python source file can be used as comments, though this is rarely done in practice; only those strings which lexically follow the definition of a class, function, module or package are assigned to __doc__ attributes in their respective name spaces).

R

<lang R># end of line comment</lang>

Raven

<lang raven> # this is a comment</lang>


REALbasic

<lang REALbasic>//Comments are denoted by a preceding double slash or or single quote 'and continue to the end of the line. There are no multi-line comment blocks Dim foo As Integer //Comments can also occupy the ends of code lines</lang>

REBOL

<lang REBOL>

This is a line comment.

{ Multi-line strings can

 be used as comments 
 if you like }

</lang>

Functions have special commenting options which make them self documenting:

<lang REBOL> plus2: func [

   "Adds two to a number."
   n [number!] "The number to increase."

][

   n + 2

] </lang>

If you say "help plus2" at REBOL's REPL, you'll get this help information:

   USAGE:
       PLUS2 n
   
   DESCRIPTION:
        Adds two to a number.
        PLUS2 is a function value.
   
   ARGUMENTS:
        n -- The number to increase. (Type: number)

Retro

<lang Retro>( comments are placed between parenthesis. A space must follow the opening parenthesis. )</lang>

REXX

<lang rexx>/* everything

  between a climbstar and a starclimb is a comment.
  /* Nested comments */
  can be used
  in Rexx */

hour = 12 /* high noon */ midnight=00 /*first hour of the day*/


/*Note:

  climbStar= /*     (slash asterisk)
  starClimb= */     (asterisk slash)     */

</lang>

Works with: oorexx

<lang rexx>-- this type of comment works only in ooREXX

hour = 0 -- midnight </lang>

RLaB

RLaB only has single line comment indicator, as in following examples

<lang RLaB> x = "code" # I am a comment x = "code" // Here I comment thee

  1. matlab-like document line

// C++ like document line </lang>

Ruby

<lang ruby>x = "code" # I am a comment

=begin hello I a POD documentation comment like Perl =end puts "code"</lang>

Sather

<lang sather>-- a single line comment</lang>

Scala

<lang scala>// A single line comment

/* A multi-line

  comment */</lang>

Scheme

<lang scheme>; Basically the same as Common Lisp

While R5RS does not provide block comments, they are defined in SRFI-30, as in Common Lisp
  1. | comment

... #| nested comment ... |# |#

See http://srfi.schemers.org/srfi-30/srfi-30.html

</lang>

Seed7

<lang seed7># A single line comment

(* A multi-line

   comment *)

(* In Seed7, (* comments can be nested. *) *)</lang>


Slate

<lang slate>"basically the same as smalltalk"</lang>

Smalltalk

<lang smalltalk>"Comments traditionally are in double quotes." "Multiline comments are also supported.

Comments are saved as metadata along with the source to a method.
A comment just after a method signature is often given to explain the
usage of the method. The class browser may display such comments
specially."</lang>

SNOBOL4

<lang SNOBOL4>

  • An asterisk in column 1 is the standard Snobol comment
  • mechanism, marking the entire line as a comment. There
  • are no block or multiline comments.
  • Comments may begin at
  • any position on the line.

- A hyphen in column 1 begins a control statement. - Unrecognized control statements are ignored and - may also mark comment lines. Not recommended.

                  ;* The semicolon statement separator
   output = 'FOO' ;* begins a new statement. This idiom
   output = 'BAR' ;* simulates an asterisk in the first
                  ;* column, allowing end of line comments.

END

Any text after the required END label is ignored.</lang>

SNUSP

As with Brainf*** and Befunge, any character that is not part of the language is ignored and can be used as commentary, and you can add comments anywhere the instruction pointer is not expected to traverse. Reserved characters are:

  • Core: + - > < , . ? ! / \ $ #
  • Modular: @ #
  • Bloated: : ; & %

As a matter of convention, the characters '=' and '|' are used for spacing to indicate horizontal and vertical flow of control, respectively.

Standard ML

<lang sml>(* This a comment

  (* containing nested comment *)
*)</lang>

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:

<lang tcl># 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</lang>

The reason for the need for a semi-colon on a trailing comment is this:

"If a hash character (“#”) appears at a point where Tcl is expecting the first character of the first word of a command, then the hash character and the characters that follow it, up through the next newline, are treated as a comment and ignored. The comment character only has significance when it appears at the beginning of a command." (from the Tcl man page -- emphasis mine)

The "#" symbol has no special meaning if it is not where a command would appear -- it's just data. (Syntax highlighters often get this wrong.)

<lang tcl>set aList {foo} lappend aList # bar puts $aList  ;# ==> prints "foo # bar" puts [llength $aList] ;# ==> 3</lang>

TCL has no native multi-line comment format. However, in most circumstances, a multi-line comment can be faked by wrapping it within a block that will never be executed:

<lang tcl>if 0 {

  Comments...

}</lang>

TI-83 BASIC

There is no 'proper' way of adding comments in TI-BASIC, however there are ways to add text to a program that will be ignored by the calculator.

One common approach is to put the comment in a string which is not stored anywhere: <lang ti83b>:"THIS IS A COMMENT</lang> However this will change the Ans variable.

This approach, while messier, does not affect the Ans variable: <lang ti83b>:If 0

THIS IS A COMMENT</lang>


TI-89 BASIC

<lang ti89b>© This is a comment. Everything from © to the end of the line is ignored.</lang>

Toka

There are two ways to add comments in Toka. For full lines, or at the end of a line, the shebang is normally used:

<lang toka>#! Everything on this line (after the shebang to the left) will be ignored.</lang>

The shebang comments can not be used inside of functions.

In addition, Toka also accepts parenthetical comments. These are enclosed in parenthesis, and are often used for stack comments or comments inside functions.

<lang toka>[ ( a b -- c )

 ... ] is myword</lang>

In addition, parenthetical comments can span multiple lines.

<lang toka>( This is a

 simple, multi-line
 comment )</lang>

Since comments are provided by actual functions, the comment function must be whitespace delimited, just as with all other functions in Toka.

A final way to include text in a file is to mark a false ending with end.

<lang toka>... code .... end. Nothing following the end. will be evaluated by Toka.</lang>

TUSCRIPT

<lang tuscript> $$ MODE TUSCRIPT - This is a comment </lang>

TXR

<lang txr>@# comment to end of line</lang>

UNIX Shell

Works with: Bourne Shell

<lang bash>#!/bin/sh

  1. A leading hash symbol begins a comment.

echo "Hello" # Comments can appear after a statement.

  1. The hash symbol must be at the beginning of a word.

echo Not#Comment

  1. Comment</lang>

C Shell

<lang csh>#!/bin/csh -f

  1. C Shell has a similar comment syntax, but only allows comments in a
  2. script file, not in terminal input.

echo Hello#With C Shell, the hash can also be in the middle of a word.</lang>

Unlambda

Unlambda comments start with # and extend to the end of the line:

# this is a comment.

Note that comments don't need to start at the beginning of a line, e.g.

`  # apply
.a # output "a"
i  # identity

is equivalent to

`.ai

Ursala

There are lots of ways to have comments in Ursala. Here are the conventional ones. <lang Ursala># this is single line a comment

  1. this is a\

continued comment

(# this is a multi-line comment #)

(# comments in (# this form #) can (# be (# arbitrarily #) #) nested #)


this is also a comment\

and can be continued

The whole rest of the file after three hashes is a comment.</lang>

Commenting out code

There are also ways to comment out sections of code during testing. An individual item of a syntactically correct list or aggregate is commented out like this. <lang Ursala>x = <1,## 2,3></lang> The 2 is ignored but 1 and 3 aren't. This also works with nested aggregates and multiple lines. <lang Ursala>a =

<

  'to',
  ## <
     'be',
     'or'>,
  'not',
  'to',
  ## 'be'></lang>

A syntactically correct declaration can be commented out like this. <lang Ursala>foo = 1

bar = 2

baz = 3</lang> As far as the compiler is concerned, bar is not defined, but foo and baz are. It wouldn't matter if bar took multiple lines.

Comments in compiled files

The compiler can be directed to embed comments in executable files and libraries it generates without affecting their semantics. <lang Ursala>#comment -[ I document the source text but will also be embedded in the output library or executable file.]-

  1. comment gpl'3'</lang>

The latter comment puts the standard GPL license notification in the output file.

Comments as diagnostics

A function f annotated with a crash dump wrapper expressed like this during debugging <lang Ursala>my_input_type%C f</lang> is equivalent to just f when changed to this in the production code. <lang Ursala>my_input_type%Ck f</lang>

Comments as hooks

Compiling with the --depend command line option makes the compiler only scan for the #depend'ed expressions and send them to standard output. <lang Ursala>#depend <this,expression> is (parsed)* but {

  otherwise,
  ignored}</lang>

This way, scripts and source management tools can have information passed to them from the programmer by running the compiler instead of re-implementing their own parsers.

Verilog

<lang Verilog>// Single line commment.

/*

  Multiple
  line
  comment.
  • /</lang>

VHDL

<lang vhdl>-- Single line commment in VHDL</lang>

Visual Basic

In addition to the methods mentioned in BASIC above, it is also somewhat common to effectively comment out code by including the unwanted code inside an #If 0 ... #End If block. (This works because 0 evaluates to False in VB.) Note, however, that the IDE will complain about actual comments inside an #If 0 block unless it's also commented normally (i.e., using Rem or ').

<lang vb>'comment Rem comment

  1. If 0
 Technically not a comment; the compiler may or may not ignore this, but the
 IDE won't. Note the somewhat odd formatting seen here; the IDE will likely
 just mark the entire line(s) as errors.
  1. End If</lang>

Visual Basic .NET

Visual Basic .NET uses the "'" symbol or "REM" to mark it's comments. After placing a "'", or "REM", everything in that line will be ignored.

<lang vbnet>' This is a comment REM This is also a comment Dim comment as string ' You can also append comments to statements Dim comment2 as string REM You can append comments to statements</lang>

Vorpal

<lang vorpal># single line comment</lang>

XQuery

<lang xquery>(: This is a XQuery comment :)</lang>

XSLT

<lang xml></lang>