Test whether the compiler allows annotation nesting. The Compiler allows annotation nesting.

Source: Internet
Author: User

Test whether the compiler allows annotation nesting. The Compiler allows annotation nesting.

Problem: Some C compilers allow nested comments. Write a test program with the following requirements:

For compilers that allow nested comments or compilers that do not allow nested comments

Can be compiled normally (no error message appears), but the results of program execution in both cases

But not the same.

 Tip: In a string enclosed by double quotation marks, the annotator/* is part of the string, while

The double quotation mark "" that appears in the comment is also part of the comment.

Coming out-exercise C traps and defects 1-1

Answer:

If you only test whether the compiler allows annotation nesting, it is easy to write the program as prompted:

Procedure 1:

# Include <iostream>

Int main ()
{

Std: cout </* "/**/" */"*/" <std: endl;

Return 0;

}

If the compiler allows annotation nesting, the program will be compiled successfully and the execution result will be :*/

The part before the string "*/"/* "/**/" */is directly commented out by the comment of its outermost layer.

If the compiler does not allow annotation nesting, an error is reported during program compilation because

/* "/**/" */"*/" Will change to strings "*/" and */". The compiler cannot recognize */", so an error is reported.

If you write the program as follows:

Procedure 2:

# Include <stdio. h>

Int main ()

{

Std: cout </* "/**/" */"<std: endl;

Return 0;

}

If the compiler allows annotation nesting, the program will report an error during compilation because

/* "/**/" */"Will become", which cannot be recognized by the compiler.

If the compiler does not allow annotation nesting, the program will be compiled successfully and the program running result will be :*/

The first/* will form a comment pair with the first */encountered, and comment out the content between the two,

Only the string "*/" is left "*/"


Both program 1 and Program 2 cannot meet the conditions of passing two compilers at the same time. The reason is that

/* And */or follow the nearest match. In this case, the compiler supports annotation nesting or priority matching.

*/Matches the most matched/*. In this case, the compiler does not support annotation nesting. And double quotation marks "and" follow

Proximity matching principle. This causes the program to fail to pass another compiler when it passes through one compiler,

Symbol redundancy is always generated when the program fails to pass the compiler.

Therefore, the most fundamental problem is to try to eliminate the symbol redundancy in the program. After thinking about several methods, none of them succeeded,

Finally, I thought of using the symmetrical thinking in mathematics. since such a formula generates a symbol redundancy, I would like to add another

And generates another symbol redundancy. When two redundant symbols are combined, a New Annotation pair is formed.

Or a string, so that the program can use both compilers at the same time.

The procedure is as follows:

Procedure 3:

# Include <stdio. h>

Int main ()

{

Std: cout </* "/*" */"*/"/* "/*" */"*/" <std: endl;

Return 0;

}

If the compiler does not support annotation nesting, the output result is:

*/*/

/*"/*"*/"*/"/*"/*"*/"*/" Is changed to "*/" and "*/". The bottom line is commented out.

If the compiler supports annotation nesting, the output result is:

/*"/*"*/"*/

/*"/*"*/"*/"/*"/* "*/" */"Is changed to:"/* "/*" */"*/". The bottom line is commented out.





In C, what are the differences between/*/**/"*/"/* "/**/in the execution results when nested comments are allowed and nested comments are not allowed?

The annotation symbol must appear in pairs. If nested comments are allowed, the result will not be affected. Otherwise, it will be treated as a piece of code in the program, or even cause the program to be unable to run, which is different from the result.

How can I answer this question?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.