c语言hello world代码编写教学
面试题:C语言用十种方法实现hello world程序,怎么做? 1 最经典的Hello world!,直接用 printf 输出 Hello world!
#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;}
2用宏写的Hello world!
#可以提取参数的名 字,把它变成字符串
#include <stdio.h>
#define Say(sth) puts (#sth)
int main(){
return Say(Hello world!);
}
3. 断章取义的Hello world!
#include <stdio.h>
int main(){
return puts ("Do not say: Hello world! ");
}
4. 退出时运行的Hello world!
atexit()注册回调函数这个函数可以调用多次,最后注册的函数最先执行
#include <stdio.h>
#include <stdlib.h>
void say(){printf("world! ");}
void sth(){printf("Hello ");}
int main(){
return atexit(say), atexit (sth);
}
5. 读取自己的Hello world!
// 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;
}
6. 话分两头的Hello world!
声明一个全局的类的实例,在 main 函数执行之前会调用这个类的构造函数,结束之后则会调用析构函数
#include <iostream>
class say{
public:say(){std::cout << "Hell";}
~say(){std::cout << "world!";}
}hello;
int main(){
std::cout << "o ";
return 0;
}
7. 传入模板的Hello world!
#include <iostream>
template <char * words>
class say{
public:
void operator () (){std::cout << words;}
};
char hello[] = "Hello world!";
int main(){
return say<hello>()(), 0;
}
8. 调用私有函数的Hello world!
#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;
}
9. 直接修改函数的返回地址
#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;
}
10. 外星人说的Hello world!
#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;
} C语言编程“Hello,world”应该怎们弄现在网上有很多C语言集成开发环境,不错,我现在就在用. HELLO WORLD程序如下: #include <stdio.h> main() {printf("Hello World!n"); } 你若用TURBO C2.0的话,可能是路径设置不对,要改路径,建议下载网上集成开发环境,那样就不用考虑那些就可以用了 (随机推荐阅读本站500篇优秀文章点击前往:500篇优秀随机文章)
#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;}
2用宏写的Hello world!
#可以提取参数的名 字,把它变成字符串
#include <stdio.h>
#define Say(sth) puts (#sth)
int main(){
return Say(Hello world!);
}
3. 断章取义的Hello world!
#include <stdio.h>
int main(){
return puts ("Do not say: Hello world! ");
}
4. 退出时运行的Hello world!
atexit()注册回调函数这个函数可以调用多次,最后注册的函数最先执行
#include <stdio.h>
#include <stdlib.h>
void say(){printf("world! ");}
void sth(){printf("Hello ");}
int main(){
return atexit(say), atexit (sth);
}
5. 读取自己的Hello world!
// 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;
}
6. 话分两头的Hello world!
声明一个全局的类的实例,在 main 函数执行之前会调用这个类的构造函数,结束之后则会调用析构函数
#include <iostream>
class say{
public:say(){std::cout << "Hell";}
~say(){std::cout << "world!";}
}hello;
int main(){
std::cout << "o ";
return 0;
}
7. 传入模板的Hello world!
#include <iostream>
template <char * words>
class say{
public:
void operator () (){std::cout << words;}
};
char hello[] = "Hello world!";
int main(){
return say<hello>()(), 0;
}
8. 调用私有函数的Hello world!
#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;
}
9. 直接修改函数的返回地址
#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;
}
10. 外星人说的Hello world!
#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;
} C语言编程“Hello,world”应该怎们弄现在网上有很多C语言集成开发环境,不错,我现在就在用. HELLO WORLD程序如下: #include <stdio.h> main() {printf("Hello World!n"); } 你若用TURBO C2.0的话,可能是路径设置不对,要改路径,建议下载网上集成开发环境,那样就不用考虑那些就可以用了 (随机推荐阅读本站500篇优秀文章点击前往:500篇优秀随机文章)
来源:本文由易搜IT博客原创撰写,欢迎分享本文,转载请保留出处和链接!