It is said that the monthly salary is 2 W.

Source: Internet
Author: User

In other words, there is a pen question that is said to be a monthly salary of 2 W:

# Include "stdio. H"
Void print ()
{
*
}
Void main ()
{
}

It must be written in part *.CodeMake the entireProgramAfter running the command, output "Hello world". Some people say that this is not simple, so they wrote the code like this:

View Source

Print? 01. # Include "stdio. H" 02. Void Print () 03. { 04. Printf("Hello World"); 05. } 06. Void Main () 07. { 08. Print (); 09. }

This is indeed okay, But it violates the requirements of the question, because the question cannot be modified to the main function, it seems that we have no choice, when we learned the C/C ++ language, we clearly stated that the main function is the main function of the program and the entry function of the program. However, the main function is empty now, how can we execute subfunctions?

In fact, this pen test is not about our understanding of the basic knowledge, but about our ability to discover and solve problems. Sometimes, when a road fails, we have to change the road, by searching for information, we found that C ++ can actually modify the entry function.

Here we need a c ++ precompiled identifier # pragma comment () to help us solve this problem. The usage type is Pragma comment (comment-type, ["commentstring"]).

Comment-type is a predefined identifier that specifies the annotation type. It should be one of compiler, exestr, Lib, and linker.

Commentstring is a string that provides additional information for comment-type.

Here we need to use linker to modify link settings:

# Pragma comment (linker, "/entry: Print ")

Have you seen it? Use the/entry parameter to set the main function to print when the program is linked. In this way, the print function is the main function of the entire program. Instead, it replaces the main function, the main function is a subfunction.

Complete code and running results:

As for # pragma comment (Lib, "msvcrt. lib"), it is because the puts function is used. If printf is used, do not use this sentence.

 

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.