编写满足以下要求的程序的方法是什么:
- 打印“Hello world!” 到标准输出;
有空的主要(只返回0)即
int main(int argc, char** argv) { return 0; }
main
除上述代码外,不得包含其他代码。
You can do this in different ways. Consider you have #include <iostream>
then following methods should be placed before main.
You may use macros, but the result is undefined as noticed in comments. So even if this is an easy way, it should never be used. I will still leave it here for completeness.
#define return std::cout << "Hello world!"; return
You may use static variable:
int helloWorld()
{
std::cout << "Hello World";
return 0;
}
static int print = helloWorld();
... or even simpler:
bool printed = std::cout << "Hello World";
You can do same with object:
struct hello
{
public:
hello()
{
std::cout << "Hello, world!";
}
} world;
struct Bob
{
Bob()
{
printf("Hello world!");
}
} bob;
int main()
{
}
Object Instantiation:
struct S
{
S() { std::cout << "Hello World!"; }
} s;
int main() { }
Or in an expression:
int i = ((std::cout << "Hello World\n"), 5);
int main() { }