-
marginal notes
- Strange annotations
- C-style comments cannot be nested
- Some special notes
- Annotated rule proposals
-
Backslash '\'
-
The backslash has the effect of continuing a line, but be careful not to add a space after the continuation line
- Carriage returns also act as line breaks, so what is the point of the line continuation character?
- Backslash escape function
-
The backslash has the effect of continuing a line, but be careful not to add a space after the continuation line
-
Single and double quotes
- Nominal value, string, character, character variable size
- Why is sizeof('1') of size 4 ?
- The char type is always truncated.
- Empty character constants are not allowed to be defined
- Single quotes are usually one character, but there can be more than one character.
marginal notes
C-style comments./* This is a comment */
C++ style comments.//This is a comment
Strange annotations
int main()
{
int /* */ i; //1. correct
char *s = "abcdefgh //hijklmn"; //2. Correct: //Is it a part of string
//Is it a\
valid comment? //3. Correct A comment can be continued by a line continuation character
in/* */t j; //4.
system("pause");
system("pause"); return 0; }
}
Syntax checking for gcc 4.8.5.
Why is 4 wrong? Look at the pre-compiled result.
Execute the pre-compile command.gcc -E -o
(found that the preprocessing stage gcc did not report errors, indicating that the problem is in the post-compilation)
You can see that there is an extra space in line 9 between int, indicating that the comment may be converted to a space in the pre-compilation.
In line 5, there is only one space in the middle of int i, indicating that the comment was either deleted directly or merged into a single space.
Conclusion: comments will be replaced by spaces after precompilation, the exact replacement is determined by the compiler.
C-style comments cannot be nested
Pre-compilation.
You can see that after precompilation, the top two \* are gone, and there is still a *\ below.
Reason: C treats all data after the first /* encountered as comments until the first /* is encountered, so nested /*s are not checked. The comments are removed during preprocessing. So nested comments leave a *.
C-style comments use the proximity principle. If you want to be able to nest, you have to use the proximity principle in order to accommodate multiple nests.
As for why we don't choose a matching rule like code block, it might be a historical problem, if you know it, you can share it in the comment section.
Some special notes
- Comments in the middle of macros
- Use of division sign and * at the same time
Solution.
1. Add a space after / and *.int b = 20/ *p
2. Enclose (*) in parenthesesint b = 20/(*p)
This is the recommended way. It's more intuitive.
Annotated rule proposals
-
Annotations should be accurate, easy to understand and prevent duality. Incorrect exegesis is not only unhelpful but also harmful.
-
Comment while writing code, modify the code at the same time modify the corresponding comments to ensure the consistency of comments and code. Comments that are no longer useful should be deleted.
-
Comments are "hints" to the code, not documentation. Comments in the program should be simple and clear, too many comments will make people confused.
-
For global data (global variables, constant definitions, etc.) comments must be added.
-
Comments should be positioned adjacent to the code being described, either on the same line as the statement or on an upper line, but not below. Comments in different domains of the same structure should be aligned.
-
When the code is long, especially when there is multiple nesting, comments should be added at the end of some paragraphs to make it easier to read.
-
Annotate snippets with a focus on "why" rather than "how".
Comments that explain how to do something are generally at the level of the programming language and are not intended to be explanatory. Comments that try to spell out the "how" generally do not tell us the intent of the operation, and comments that specify the "how" are usually redundant. -
The units of the values must be commented. For example, units of hours, minutes, seconds, etc.
-
Give comments on the scope of variables, especially parameters.
-
Give comments on a series of numerical numbers, especially when writing the underlying driver (e.g., pin numbering).
-
In complex functions, after the end of the branch statement, loop statement needs to be appropriate comments, easy to distinguish between the branches or loop body. (Often times when the code is long, you can't see the following code in a screen, and you don't know which one is the matching parenthesis of the corresponding code block.)
Backslash '\'
The backslash has the effect of continuing a line, but be careful not to add a space after the continuation line
As shown, adding multiple spaces before the line continuation character has no effect:
However, the line continuation character cannot be followed by a space
Carriage returns also act as line breaks, so what is the point of the line continuation character?
The continuation character is inherently self-descriptive. This is the main point of the line continuation character, which is to make it clear that there is a line break, and that there is no dichotomy. If you use a carriage return, someone reading the line may have to wonder if the line break was intentional or if it was accidentally struck by mistake.
Backslash escape function
escaping, i.e., backslashing certain characters, can give them more than their own functionality.
There are two types of escapes, one such as\n
,\t
Such an escape character, after the escape has a special function other than its own literal value; another type of character such as\"
,\\
In this case, the literal value itself has no meaning, but other functions, which have the meaning of the literal value after escaping.
The function of escaping is simply described as (a. literal to special b. special to literal)
Single and double quotes
Basic concepts: single quotes are characters, double quotes are strings
Nominal value, string, character, character variable size
The sizeof is calculated separately: the
Why is sizeof('1') of size 4 ?
According to the C99 standard, the character caused by the single quote 'a' is called an integer character constant and will be treated as an integer by the compiler. Therefore the size is 4 bytes.
(Note: In C++, the size of character constants is 1, which is interpreted differently by different platforms/languages.)
The char type is always truncated.
The char type basically receives integer character constants, and the char type occupies only one byte in size, so only one byte (the first byte) of the integer type can be retained, i.e. truncated.
Empty character constants are not allowed to be defined
There must be at least one literal value within single quotes
Single quotes are usually one character, but there can be more than one character.
As you can see below, there is no warning.
Print Results.
But the maximum number of characters is 4, more will cause an alarm.
Why does it work this way? As mentioned above, character constants are treated as integers by C and are allocated 4 bytes.
A character literal value takes up one byte, 4 characters take up exactly 4 bytes, the space allocated for integers can be satisfied.
The char type truncates the lower byte bit of the integer type, so it only outputs the last character literal value (the little end).