设为首页 加入收藏

TOP

HelloWorld! C++纠错版
2019-07-12 14:10:03 】 浏览:86
Tags:HelloWorld 纠错
例题:
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++;
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++20 的 Modules 下一篇P1349 广义斐波那契数列(矩阵乘法)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目