在c/c++中的main()之前和之后打印一些内容

printing something before and after main() in c/c++

本文关键字:打印 之后 c++ 中的 main      更新时间:2023-10-16

考虑代码片段

main()
{
  cout << "Hello";
} 

在不接触上述代码片段的情况下打印


你好
再见

#include <iostream>
using namespace std;
struct A
{
    A() { cout << "Hi" << endl; }
    ~A() { cout << endl << "Bye"; }
};
A a;
main()
{
  cout << "Hello";
} 

实时演示链接。

#include <iostream>
using namespace std;
struct A
{
    A() { cout << "Hi" << endl; }
    ~A() { cout << "nBye" << endl; }
};
A a;

int
// untouched part :)
main()
{
    cout << "Hello";
}

输出为

Hi
Hello
Bye