#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;
}
新聞熱點
疑難解答