|
| 1 | +--- |
| 2 | +title: "Comments" |
| 3 | +--- |
| 4 | + |
| 5 | +import { Desc, DescList, DocLink, Revision, RevisionBlock } from '@components/index'; |
| 6 | + |
| 7 | +Comments serve as a sort of in-code documentation. When inserted into a program, they are effectively ignored by the compiler; they are solely intended to be used as notes by the humans that read source code. |
| 8 | + |
| 9 | +### Syntax |
| 10 | + |
| 11 | +* (1) `/*` _comment_ `*/` |
| 12 | +* <Revision since="C99">(2) `//` _comment_ </Revision> |
| 13 | + |
| 14 | +1) Often known as "C-style" or "multi-line" comments. |
| 15 | +2) Often known as "C++-style" or "single-line" comments. |
| 16 | + |
| 17 | +All comments are removed from the program at <DocLink src="/c/language/translation_phases">translation phase 3</DocLink> by replacing each comment with a single whitespace character. |
| 18 | + |
| 19 | +### C-style |
| 20 | + |
| 21 | +C-style comments are usually used to comment large blocks of text or small fragments of code; however, they can be used to comment single lines. To insert text as a C-style comment, simply surround the text with `/*` and `*/`. C-style comments tell the compiler to ignore all content between `/*` and `*/`. Although it is not part of the C standard, `/**` and `**/` are often used to indicate documentation blocks; this is legal because the second asterisk is simply treated as part of the comment. |
| 22 | + |
| 23 | +Except within a <DocLink src="/c/language/character_constant">character constant</DocLink>, a <DocLink src="/c/language/string_literal">string literal</DocLink>, or a comment, the characters `/*` introduce a comment. The contents of such a comment are examined only to identify multibyte characters and to find the characters `*/` that terminate the comment. C-style comments cannot be nested. |
| 24 | + |
| 25 | +<RevisionBlock since="C99"> |
| 26 | + |
| 27 | +### C++-style |
| 28 | + |
| 29 | +C++-style comments are usually used to comment single lines of text or code; however, they can be placed together to form multi-line comments. To insert text as a C++-style comment, simply precede the text with `//` and follow the text with the new line character. C++-style comments tell the compiler to ignore all content between `//` and a new line. |
| 30 | + |
| 31 | +Except within a <DocLink src="/c/language/character_constant">character constant</DocLink>, a <DocLink src="/c/language/string_literal">string literal</DocLink>, or a comment, the characters `//` introduce a comment that includes all multibyte characters up to, but not including, the next new-line character. The contents of such a comment are examined only to identify multibyte characters and to find the new-line character that terminates the comment. C++-style comments can be nested: |
| 32 | + |
| 33 | +```c |
| 34 | +// y = f(x); // invoke algorithm |
| 35 | +``` |
| 36 | + |
| 37 | +A C-style comment may appear within a C++-style comment: |
| 38 | + |
| 39 | +```c |
| 40 | +// y = f(x); /* invoke algorithm */ |
| 41 | +``` |
| 42 | + |
| 43 | +A C++-style comment may appear within a C-style comment; this is a mechanism for excluding a small block of source code: |
| 44 | + |
| 45 | +```c |
| 46 | +/* |
| 47 | + y = f(x); // invoke algorithms |
| 48 | + z = g(x); |
| 49 | +*/ |
| 50 | +``` |
| 51 | + |
| 52 | +</RevisionBlock> |
| 53 | + |
| 54 | +### Notes |
| 55 | + |
| 56 | +Because comments <DocLink src="/c/language/translation_phases">are removed</DocLink> before the preprocessor stage, a macro cannot be used to form a comment and an unterminated C-style comment doesn't spill over from an #include'd file. |
| 57 | + |
| 58 | +```c |
| 59 | +/* An attempt to use a macro to form a comment. */ |
| 60 | +/* But, a space replaces characters "//". */ |
| 61 | +#ifndef DEBUG |
| 62 | + #define PRINTF // |
| 63 | +#else |
| 64 | + #define PRINTF printf |
| 65 | +#endif |
| 66 | +... |
| 67 | +PRINTF("Error in file%s at line%i\n", __FILE__, __LINE__); |
| 68 | +``` |
| 69 | +
|
| 70 | +Besides commenting out, other mechanisms used for source code exclusion are: |
| 71 | +
|
| 72 | +```c |
| 73 | +#if 0 |
| 74 | + puts("this will not be compiled"); |
| 75 | + /* no conflict with C-style comments */ |
| 76 | + // no conflict with C++-style comments |
| 77 | +#endif |
| 78 | +``` |
| 79 | + |
| 80 | +and |
| 81 | + |
| 82 | +```c |
| 83 | +if(0) { |
| 84 | + puts("this will be compiled but not be executed"); |
| 85 | + /* no conflict with C-style comments */ |
| 86 | + // no conflict with C++-style comments |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +The introduction of // comments in C99 was a breaking change in some rare circumstances: |
| 91 | + |
| 92 | +```c |
| 93 | +a = b //*divisor:*/ c |
| 94 | ++ d; /* C89 compiles a = b / c + d; |
| 95 | + C99 compiles a = b + d; */ |
| 96 | +``` |
| 97 | + |
| 98 | +### Example |
| 99 | + |
| 100 | +```cpp |
| 101 | +#include <stdio.h> |
| 102 | +/* |
| 103 | +C-style comments can contain |
| 104 | +multiple lines. |
| 105 | +*/ |
| 106 | + |
| 107 | +/* Or, just one line. */ |
| 108 | + |
| 109 | +// C++-style comments can comment one line. |
| 110 | + |
| 111 | +// Or, they can |
| 112 | +// be strung together. |
| 113 | + |
| 114 | +int main(void) |
| 115 | +{ |
| 116 | + // The below code won't be run |
| 117 | + // puts("Hello"); |
| 118 | + |
| 119 | + // The below code will be run |
| 120 | + puts("World"); |
| 121 | + |
| 122 | + // A note regarding backslash + newline. |
| 123 | + // Despite belonging to translation phase 2 (vs phase 3 for comments), |
| 124 | + // '\' still determines which portion of the source code is considered |
| 125 | + // as 'comments': |
| 126 | + // This comment will be promoted to the next line \ |
| 127 | + puts("Won't be run"); // may issue a warning "multi-line comment" |
| 128 | + puts("Hello, again"); |
| 129 | +} |
| 130 | +``` |
| 131 | +
|
| 132 | +Output: |
| 133 | +
|
| 134 | +```text |
| 135 | +World |
| 136 | +Hello, again |
| 137 | +``` |
| 138 | + |
| 139 | +### References |
| 140 | + |
| 141 | +* C17 standard (ISO/IEC 9899:2018): |
| 142 | + * 6.4.9 Comments (p: 54) |
| 143 | +* C11 standard (ISO/IEC 9899:2011): |
| 144 | + * 6.4.9 Comments (p: 75) |
| 145 | +* C99 standard (ISO/IEC 9899:1999): |
| 146 | + * 6.4.9 Comments (p: 66) |
| 147 | +* C89/C90 standard (ISO/IEC 9899:1990): |
| 148 | + * 3.1.9 Comments |
| 149 | + |
| 150 | +### See also |
| 151 | + |
| 152 | +<DescList> |
| 153 | + <Desc> |
| 154 | + <DocLink slot="item" src="/cpp/comments">C++ documentation</DocLink> for <span>Comments</span> |
| 155 | + </Desc> |
| 156 | +</DescList> |
0 commit comments