Comments serve multiple purposes in code, enhancing its readability, providing explanations, and facilitating testing by preventing execution of alternative code.
They come in two forms: single-line or multi-line.
A single-line comment begins with two forward slashes ( // ).
Anything following these slashes // on the same line is disregarded by the compiler and won’t be executed.
In this example, a single-line comment precedes a line of code.
// This is a comment printf(“Hello World!”); |
In this example, a single-line comment is placed at the conclusion of a line of code.
printf(“Hello World!”); // This is a comment |
Multi-line comments are initiated with /* and concluded with */.
Any text enclosed between these symbols will be disregarded by the compiler.
/* The code below will print the words Hello World! to the screen, and it is amazing */ printf(“Hello World!”); |
Single or multi-line comments?The choice between // and /* */ is yours to make. Typically, // is employed for brief comments, while /* */ is preferred for longer ones. Fun fact: Prior to the release of C99 in 1999, multi-line comments were the only option available in C. |