Comments can be nested in c programming

C ++ programming / introduction to C ++ / comments

In all programming languages ‚Äč‚Äčthere is the possibility to make notes in the source text. For others or for yourself, because after a few weeks you may no longer understand your own source code without further ado. Comments help you and others to understand better and, above all, faster what the source text does. There are two ways to write comments in C ++:

// A comment that is introduced with two slashes extends to the end of the line / * A comment of this type can extend over several lines or ... * / a = b / * ... end before the end of the line. * / + c;

The first type, which applies to the end of the line, is the modern type of comment. As a rule, it is preferable because it has some advantages over the old variant, which originated from C. The second type (sometimes also referred to as C comment) in its multiline form should only be used in places with longer text passages and, if possible, not between code. Such comments are often used at the beginning of the file to briefly summarize the content or to regulate licensing issues.

The main disadvantage of multiline comments is that they cannot be "nested". For example, parts of the source code are often briefly commented out for test purposes. The following example should demonstrate this:

#include / * input and output library * / intmain () {/ * main function * // * std :: cout << "Hello, you beautiful world!" << std :: endl; /* Output */*/}

That wouldn't work. If, on the other hand, the other comments are used, there are no such problems:

#include // input and output library intmain () {// main function / * std :: cout << "Hello, you beautiful world!" << std :: endl; // Output */}

In the first example, the introduction to is simply ignored. The final part ends the comment that is supposed to comment out the line of code, and the final part leads to a compile error. Admittedly, that is not a problem, because such a mistake is found quickly, but avoiding it from the outset is even more time-saving. Incidentally, one-line comments often have to be written less. The exception is comments that are simply too long to write on one line. Nevertheless, they should also be implemented using comments.

// I'm an example of a long comment spanning multiple lines by // double slashes. If I were at the beginning of the file, // I would probably have been given different comment characters, // but since I am clearly surrounded by a huge amount of source text, // despite the increased amount of paperwork, the decision was made for //

Many text editors contain a keyboard shortcut that can be used to comment text in and out. This is particularly useful for longer code passages.