Comments // /* */

This page was translated by a robot.

In the C and C++ languages, source code can contain single-line and bracketed comments. Single-line comments begin with a double slash //, causing the compiler to ignore all characters to the end of the lineieren.

// This is a comment
int x = 5;  // This variable is initialized with five.

Comments in parentheses are within the character string /*and */all characters, including line breaks, that are in between are ignored by the compiler.

/*  This comment
    has multiple
    lines.*/
int x = /*5*/ 6;

Details

Writing comments is important, firstly, to make the code easier to understand for a stranger who may need to understand it. Last but not least, comments are also a help for the programmer himself and a design tool. For example, important code blocks can be highlighted using visually supporting comments. In addition, modern editors often color comments in a special color, which provides additional orientation.

printf("Some boring code.");

////////// THE MAIN KERNEL //////////

result = 42;

///////// END OF THE KERNEL /////////

printf("Again, boring code.");

Basically, the one-line comments are intended for writing short comments or commenting out entire lines. The bracketed comments are there to enclose longer explanatory texts or to hide part of the code in the middle of a line.

x++;y++;   // go to the next coordinates
//x = y + 1;
/* Now the coordinates of the point
   are set and we can calculate the
   distance to the 0-vector. */
dist = sqrt(/*x + y*/ x*x + y*y);

Parenthesized comments cannot be nested. Since the compiler ignores all characters (including the character string /*), the first concluding character string */is regarded as the end of the comment.

/* This is /* not */ a correct comment. */

Caution is also advised with single-line comments. If a single-line comment has a backslash at the end of the line , the end of the line is removed \by the endline escape character and the compiler also ignores the following line. The following example will therefore never produce any output:

#include <stdio.h>

int main(){
  //\
  printf("asdf");
  return 0;
}
Next Chapter: Compiler, Linker