当前位置: 移动技术网 > IT编程>开发语言>C/C++ > HelloWorld! C++纠错版

HelloWorld! C++纠错版

2019年07月12日  | 移动技术网IT编程  | 我要评论

iqpg,huijiadeyuwang,雪橇狗价格

例题:
1 #include<iostream> 2 3 int main() 4 { 5 cout << "helloworel!" ; 6 return 0; 7 }

1 #include <iostream>
2    using namespace std;
3    int main()  //c++ programs start by executing the function main
4    {
5          cout << "helloworld!\n"<< endl; //output"helloworld!"
6          return 0;
7  }

编译通过。

疑问:

书上的例题用vc++编译

我在dev-c++编译一样报错

using namespace std;干嘛用的?为毛跟书上的列题不一样?

 

查查资料后了解得知:
 #include<iostream.h> 非标准输入输出流,这个写法是以前c语言的写法,上个世纪八九十年代的书中一般采用这种写法,现在已经不适用了。
iostream.h时代没有名词空间,即所有库函数包括头文件iostream.h都声明在全局域。为了体现结构层次,避免名字定义冲突,

c++标准委员会特别引入了“名字空间的定义”,即namespace。引入了名词空间这一概念,并把所有库函数声明由全局域改到了名词空间std。
所以,新的标准是:

 

1 #include <iostream>  //标准输入输出流
2 using namespace std;

 

(因为iostream声明在std中,故而要加上这句,除非你不用库函数,否则错误);

很多编译器都同时支持这两种头文件形式,更好的当然是标准头文件。至于为什么不废除非标准头文件,大概是为了兼容以前的代码吧。

还有一点在标准c++中,所有库函数都没有.h后缀了,如果是c语言的库函数,则去掉后缀,并在开头加上一个c。

如下:

#include cstdio  //stdio.h
#include cstring //string.h  

 

使用<iostream>时,引入std::有以下方法:

1 using namespace std;
3 cout<<hello c++<<endl;
4 
5 using std::cout;
6 cout<<hello c++;
7 
8 std::cout<<hello c++;

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网