Location>code7788 >text

Extended analysis of single and double quotes, backslashes and comments

Popularity:887 ℃/2024-09-19 11:18:07

catalogs
  • 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
  • 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.
1正确的注释.png
Why is 4 wrong? Look at the pre-compiled result.
Execute the pre-compile command.gcc -E -o

1gcc编译.png

(found that the preprocessing stage gcc did not report errors, indicating that the problem is in the post-compilation)

1预编译后的注释
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.

C风格注释不能嵌套.png

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

bd9dd9d0-ed43-476d-ba9e-4237c15775e7

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

  1. Annotations should be accurate, easy to understand and prevent duality. Incorrect exegesis is not only unhelpful but also harmful.

  2. 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.

  3. Comments are "hints" to the code, not documentation. Comments in the program should be simple and clear, too many comments will make people confused.

  4. For global data (global variables, constant definitions, etc.) comments must be added.

  5. 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.

  6. 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.

  7. 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.

  8. The units of the values must be commented. For example, units of hours, minutes, seconds, etc.

  9. Give comments on the scope of variables, especially parameters.

  10. Give comments on a series of numerical numbers, especially when writing the underlying driver (e.g., pin numbering).

  11. 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:

7b2ff3f7-5848-42cc-8a45-24c97b3585f9

However, the line continuation character cannot be followed by a space

f8cf3aed-e3ac-4025-9c8d-97ac16dd43dc


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,\tSuch 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

f0339503-6593-4f95-b8ef-152f6b55d48b


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

a92d0e67-ae21-41a0-bcee-868c14f79dcb


Single quotes are usually one character, but there can be more than one character.

As you can see below, there is no warning.

819f3b5e-35aa-413b-886b-029ba7f03d29

Print Results.

7d90218a-50d7-46fe-b206-77eba1eb41b9

But the maximum number of characters is 4, more will cause an alarm.

d5e2f2e1-cc45-43b0-b75e-589da65f1711

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).