Comment Syntax in Every Major Programming Language

Most languages use //, #, or /* */, but the differences are interesting. Here's a single reference for every language you're likely to touch.

Tech Talk News Editorial5 min read
ShareXLinkedInRedditEmail
Comment Syntax in Every Major Programming Language

Almost every programming language supports comments, and most of them use one of three formats: // for single-line, /* */ for multi-line, or #for both. The differences map almost perfectly to which language family the language descended from. C-family uses slashes. Shell-family uses hashes. SQL is its own thing. Here's a complete reference.

Plain English

A comment is text in source code that the language ignores. Comments are for humans reading the code, not for the compiler. Different languages mark them differently because each language was designed at a different time by people who liked different conventions.

C-Family: // and /* */

The C-family inherited C's comment syntax: single-line with // and multi-line with /* */. This includes:

  • C, C++, Objective-C
  • Java, Kotlin, Scala, Groovy
  • JavaScript, TypeScript
  • C#, Dart
  • Go, Rust, Swift
  • PHP (also supports #)
JavaScript / TypeScript / Java / C++ / etcJavaScript
// This is a single-line comment

/*
   This is a multi-line
   comment
*/

/**
 * This is a doc comment (JSDoc, JavaDoc, etc.)
 * @param x The first argument
 */

The /** */ with extra asterisks is a doc-comment convention used by JSDoc, JavaDoc, Doxygen, and similar tools. The syntax is just a regular comment to the compiler, but documentation generators recognize the extra asterisk as a marker for structured doc strings.

Shell and Hash-Family: #

Shell scripts and many high-level languages use # for comments. The convention came from Bourne shell in the 1970s and stuck.

  • Python
  • Ruby
  • Perl
  • Bash, sh, zsh, fish
  • R
  • YAML
  • Shell (any flavor)
  • Makefiles
PythonPython
# This is a single-line comment

# Python doesn't have multi-line comment syntax,
# you just use multiple single-line comments.

"""
This is technically a string literal,
but it's commonly used as a multi-line
comment inside functions.
"""

Python notably doesn't have true multi-line comments. Triple-quoted strings ("""") are sometimes used as multi-line comments, but they're actually string expressions that the parser evaluates and discards. They're also how docstrings work, which are real and accessible at runtime via __doc__.

SQL: Two Conventions

SQLSQL
-- Single-line comment (standard SQL)

# Single-line comment (MySQL, MariaDB only)

/*
   Multi-line comment
*/

SQL uses -- for single-line, which sometimes confuses developers from C-family backgrounds. MySQL adds # as an alternative single-line. Multi-line is /* */ across most SQL dialects.

HTML and XML

HTML / XMLHTML
<!-- This is a single-line comment -->

<!--
    This is a
    multi-line comment
-->

Same syntax for both single and multi-line. The opening <!-- and closing --> work for any number of lines. Note that you can't nest HTML comments, and the string -- inside a comment is technically illegal per the spec (browsers tolerate it).

CSS

CSSCSS
/* This is a comment */

/* CSS only has multi-line style.
   No single-line // syntax. */

CSS uses /* */ only. The // single-line comment is not valid CSS and will silently break the rule it appears in. CSS preprocessors (Sass, Less) do support //, but plain CSS doesn't.

Lisp Family: ;

Lisp / Scheme / Clojureplaintext
; This is a single-line comment

;; Two semicolons for top-level comments by convention

;;; Three semicolons for file-level comments

#| This is
   a multi-line comment |#

Lisp-family languages use ; for single-line and #| ... |# for multi-line. The number of semicolons is a stylistic convention indicating comment level.

Haskell and ML-Family

Haskellplaintext
-- This is a single-line comment

{-
   This is a
   multi-line comment
-}

Haskell uses -- for single-line (same as SQL) and {- -} for multi-line. OCaml uses (* *), which is Pascal's convention.

Assembly

Different assemblers use different conventions:

  • ; for x86 NASM and most Intel-syntax assemblers
  • # or @ for ARM
  • ! for some MIPS variants
  • ; or -- for various others

Assembly is the wild west of comment syntax. Read the specific assembler's docs.

The Languages That Don't Fit

A few worth noting:

  • Fortran: ! for end-of-line comments (modern Fortran). Older Fortran used a C in the first column.
  • Erlang, Elixir: % for Erlang, # for Elixir.
  • VB / VBA: ' (single quote) or REM.
  • Pascal / Delphi: { } or (* *).
  • BASIC: REM or '.

Doc Comments

Most languages have a convention for documentation comments that get parsed by tools:

  • Java: /** */ (JavaDoc)
  • JavaScript/TypeScript: /** */ (JSDoc, TSDoc)
  • C++: /** */ or /// (Doxygen)
  • Python: triple-quoted string immediately after a function or class (docstring)
  • Rust: /// (rustdoc)
  • Go: regular // comments immediately above a declaration (godoc)
  • Ruby: comments above the method, processed by RDoc/YARD

These conventions matter because many tools generate API documentation from them, and good doc comments end up rendered into HTML pages, IDE tooltips, or man pages without you doing anything extra.

Takeaway

Most languages use //, #, or some variant for comments. The pattern is largely inherited from the language family the language descended from: C-style for compiled languages, # for shell-derived, -- for SQL and Haskell. When in doubt, check the language docs. Doc-comment conventions are usually more important than the basic syntax for any code that'll be read by other people.

The Take

Comment syntax is one of those things you absorb in the first week of a new language and never think about again. The interesting part is the doc-comment convention, because that's what determines whether your code generates useful documentation or not. For any function you expect someone else to call, write a doc comment in the language's standard format. The IDE tooltips alone justify the time, and the generated docs become real artifacts that outlast the team.

Written by

Tech Talk News Editorial

Tech Talk News covers engineering, AI, and tech investing for people who build and invest in technology.

ShareXLinkedInRedditEmail