#include <stdio.h>
#include <iostream>
int main()
{
printf("Hello world!"); // 教科书的写法
puts("Hello world!"); // 我最喜欢的
puts("Hello" " " "world!"); // 拼接字符串
std::cout << "Hello world!" << std::endl; // C++风格的教科书写法
return 0;
}
非凡需要注重的是,在C/C++里,假如两个字符串之间除空白符以外没有任何东西,编译器会自动认为这两个字符串是连在一起的字符串。这样,假如一个字符串过长,可以用这种方法换行来写,既不浪费性能,又美观。 #include <stdio.h>
#define Say(sth) puts(#sth)
int main()
{
return Say(Hello world!);
}
请注重,这个Hello world可是完全没有出现引号哦! #include <stdio.h>
int main()
{
return puts(&"Do not say: Hello world!"[12]);
}
#include <stdio.h>
#include <stdlib.h>
void say()
{
printf("world!");
}
void sth()
{
printf("Hello ");
}
int main()
{
return atexit(say), atexit(sth);
}
// Hello world!
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream ifs(__FILE__);
std::string say, some, Word;
ifs >> say >> some >> word;
std::cout << some << " " << word;
return 0;
}
#include <iostream>
class say
{
public:
say()
{
std::cout << "Hell";
}
~say()
{
std::cout << "world!";
}
}hello;
int main()
{
std::cout << "o ";
return 0;
}
#include <iostream>
template <char * words>
class say
{
public:
void Operator () ()
{
std::cout << words;
}
};
extern char hello[] = "Hello world!";
int main()
{
return say<hello>()(), 0;
}
请注重,这个 extern 是十分必要的,只有加上了 extern,这个指针才是一个编译器间可以确定的值,也才可以参与模板运算。还有,hello 必须为数组类型,而不能为 char*,这个道理和加 extern 是一样的。 #include <iostream>
#include <cstddef>
class secret
{
private:
virtual void say()
{
std::cout << "Hello world!";
}
};
int main()
{
secret word;
(reinterpret_cast<void (*)()>(**(intptr_t**)(&word)))();
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
void say()
{
puts("Hello world!");
exit(0);
}
int main()
{
volatile intptr_t a = 0;
volatile intptr_t * p = &a;
*(p + 2) = (intptr_t)say;
*(p + 3) = (intptr_t)say;
return 0;
}
#include <stdio.h>
void alien_say(char * p)
{
while (putchar(*(p += *(p + 1) - *p)));
}
int main()
{
return alien_say("BETHO! Altec oh liryom(a loadjudas!) dowd."), 0;
}
新闻热点
疑难解答