
There is an old saying:“A good memory is not as good as a bad pen.” This saying is equally true in the programming world.
The code we write may be forgotten by ourselves months later, and to avoid future frustration, we need to learn to use a very important tool——comments。
01
What are comments? The“invisible notes”
Comments (Comment), in simple terms, are text written in the code that will not be executed by the compiler.
You can think of it as a sticky note next to the code。Its purpose is to explain to yourself or others what a piece of code does, what needs to be noted, or to temporarily make a piece of code“inactive”.
When the program runs, it starts executing from the first line of code, but once it encounters a comment marker, it will skip directly over it when it is not present.
02
Single-line comments: The most commonly used“sticky note”
In C language, the most common way to comment is the single-line comment, which uses two slashes // to mark.
All content after // until the end of the line will be treated as a comment.
#include <stdio.h>int main() { // This is a single-line comment, the code below will print the number 123 printf("%d\n", 123);
// printf("This line of code is commented out, so it won't execute\n"); return 0; }
Key points:
-
Single-line comments are only valid for the current line.
-
It is very suitable for brief explanations of a line of code。
03
Block comments: Give a large section of code“a break”
So what if I want to comment out several consecutive lines of code? Adding// one by one is possible, but there is a more efficient way, which isblock comments (also called multi-line comments).
Block comments start with/* and end with */. All content wrapped in these symbols, whether one line or multiple lines, will be treated as comments.
#include <stdio.h>int main() { /* This is a block comment. The following two lines of code will not be executed. I can write many lines of explanatory text. printf("Hello, World! 1\n"); printf("Hello, World! 2\n"); */ printf("Only this line of code will execute.\n"); return 0;}
Key points:
-
Block comments can span multiple lines.
-
It is very suitable for writing large sections of explanations or temporarily disabling a large block of code during debugging.
04
Professional correction and addition:“Slash” standard terminology
In spoken language, we often refer to/ as“slash”. To be more professional, we need to clarify its standard name.
/ : This is called a forward slash (Forward Slash)。It is used for the comments we learned today and for division operations in mathematics.
\ : This is called a backslash (Backslash).It has special uses in C language, such as in escape characters, like the familiar newline character \n.
Understanding the difference between these two can help you understand and describe code more accurately.
05
Summary
Today we learned two ways to comment:
-
Single-line comments(//):Used for brief explanations at the end of a line.
-
Block comments(/* … */):Used for multi-line or large sections of explanations and code disabling.
