首页 > 其他 > 详细

Hello World背后的事情

时间:2020-10-11 22:29:04      阅读:83      评论:0      收藏:0      [点我收藏+]

Hello World是不少人学习C++的第一项内容,代码看似简单,很多东西却涉及根本

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello World" <<endl;
    return 0;        
}

第一行 预处理文件

C++编译器自带了很多头文件,每个头文件支持一组特定的工具,头文件中包含了函数声明和宏定义。头文件分两种,一种是程序员自己写的,一种是编译器自带的,要是愿意的话,可以包含任何一个你写过的代码文件。带有.h后缀的一般为C的头文件,但是C++编译的时候也可以包含,虽然在C++中已经有相应版本的文件了。

即C++编译器是向前兼容的比如说C版本的math.h在C++中为cmath,但是在C++中下面这行代码仍然可以使用:

#include <math.h>

iostream文件内容:

// Standard iostream objects -*- C++ -*-

// Copyright (C) 1997-2014 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file include/iostream
 *  This is a Standard C++ Library header.
 */

//
// ISO C++ 14882: 27.3  Standard iostream objects
//

#ifndef _GLIBCXX_IOSTREAM
#define _GLIBCXX_IOSTREAM 1

#pragma GCC system_header

#include <bits/c++config.h>
#include <ostream>
#include <istream>

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   *  @name Standard Stream Objects
   *
   *  The &lt;iostream&gt; header declares the eight <em>standard stream
   *  objects</em>.  For other declarations, see
   *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/io.html
   *  and the @link iosfwd I/O forward declarations @endlink
   *
   *  They are required by default to cooperate with the global C
   *  library‘s @c FILE streams, and to be available during program
   *  startup and termination. For more information, see the section of the
   *  manual linked to above.
  */
  //@{
  extern istream cin;        /// Linked to standard input
  extern ostream cout;        /// Linked to standard output
  extern ostream cerr;        /// Linked to standard error (unbuffered)
  extern ostream clog;        /// Linked to standard error (buffered)

#ifdef _GLIBCXX_USE_WCHAR_T
  extern wistream wcin;        /// Linked to standard input
  extern wostream wcout;    /// Linked to standard output
  extern wostream wcerr;    /// Linked to standard error (unbuffered)
  extern wostream wclog;    /// Linked to standard error (buffered)
#endif
  //@}

  // For construction of filebuffers for cout, cin, cerr, clog et. al.
  static ios_base::Init __ioinit;

_GLIBCXX_END_NAMESPACE_VERSION
} // namespace

#endif /* _GLIBCXX_IOSTREAM */

 

 

第二行 命名空间

using namespace std;

这行代码说明std命名空间中的所有名称都可以使用,命名空间有助于解决程序中常见的命名冲突问题,大概作用是一个包含很多函数调用方法的目录

比较通俗解释是:

The role of the namespace in C + + is similar to the relationship between directory and file in the operating system. Because there are many files, it is inconvenient to manage and easy to duplicate names. Therefore, we set up several subdirectories and put the files in different subdirectories. The files in different subdirectories can have the same name, and the file path should be pointed out when calling files.

C ++中名称空间的作用类似于操作系统中目录和文件之间的关系。 由于文件很多,因此管理不便且名称容易重复。 因此,我们设置了几个子目录并将文件放在不同的子目录中。 不同子目录中的文件可以具有相同的名称,并且在调用文件时应指出文件路径。

例如如果不写using语句,则使用std::cout,std::endl也是一样的效果(不写using语句是CPP推荐的做法,虽然实际上为了方便都在写using语句,qaq)

 

第三行 主函数

主函数也就是main()是一个程序的入口点,代表程序从这里开始,主要功能是程序的入口和程序的出口。通常我们还可以指定一个返回代码,然后退出(通常返回0,也就是return 0;)来显示程序的最终结果。main函数的功能大致类似于python中的

if __name__ == "__main__"

 

第五行 cout

cin和cout输出是以“流”的方式实现的。流运算符和其他信息的定义存储在C++输入和输出流库(也就是<iostream>)中,库定义的名称都位于名称空间std中,因此如果不适用iostream的话,cout又可以写作std::cout。

第六行 return

return常见的返回类型有一下三种

return 0;  //函数执行成功
return -1; //函数执行失败
return 1; //函数异常退出

 

其他的事情

Q:一定要换行吗?结尾的“;”有什么特殊含义吗?

A:不是的,C++执行是statement by statement,以“;”结尾,如果愿意的话,把所有代码放在一行也行。乱序也成,比如下面这样:

技术分享图片

 

附录1-C版本头文件:

  • #include<assert.h>    //设定插入点
  • #include <ctype.h> //字符处理    
  • #include <errno.h>     //定义错误码
  • #include <float.h>     //浮点数处理
  • #include <fstream.h>   //文件输入/输出
  • #include <iomanip.h>   //参数化输入/输出
  • #include<iostream.h>   //数据流输入/输出
  • #include<limits.h>    //定义各种数据类型最值常量
  • #include<locale.h>    //定义本地化函数
  • #include <math.h>     //定义数学函数
  • #include <stdio.h>    //定义输入/输出函数
  • #include<stdlib.h>    //定义杂项函数及内存分配函数
  • #include <string.h>    //字符串处理
  • #include<strstrea.h>   //基于数组的输入/输出
  • #include<time.h>     //定义关于时间的函数
  • #include <wchar.h>    //宽字符处理及输入/输出
  • #include <wctype.h>    //宽字符分类

附录2-C++版本头文件:

  • #include <algorithm>    //STL通用算法
  • #include <bitset>     //STL位集容器
  • #include <cctype>                //字符处理
  • #include <cerrno>     //定义错误码
  • #include <clocale>    //定义本地化函数
  • #include <cmath>     //定义数学函数
  • #include <complex>     //复数类
  • #include <cstdio>    //定义输入/输出函数
  • #include <cstdlib>    //定义杂项函数及内存分配函数
  • #include <cstring>    //字符串处理
  • #include <ctime>     //定义关于时间的函数
  • #include <deque>      //STL双端队列容器
  • #include <exception>    //异常处理类
  • #include <fstream>   //文件输入/输出
  • #include <functional>   //STL定义运算函数(代替运算符)
  • #include <limits>    //定义各种数据类型最值常量
  • #include <list>      //STL线性列表容器
  • #include <map>       //STL 映射容器
  • #include <iomanip>   //参数化输入/输出
  • #include <ios>      //基本输入/输出支持
  • #include<iosfwd>     //输入/输出系统使用的前置声明
  • #include <iostream>   //数据流输入/输出
  • #include <istream>     //基本输入流
  • #include <ostream>     //基本输出流
  • #include <queue>      //STL队列容器
  • #include <set>       //STL 集合容器
  • #include <sstream>    //基于字符串的流
  • #include <stack>      //STL堆栈容器    
  • #include <stdexcept>    //标准异常类
  • #include <streambuf>   //底层输入/输出支持
  • #include <string>     //字符串类
  • #include <utility>     //STL通用模板类
  • #include <vector>     //STL动态数组容器
  • #include <cwchar>    //宽字符处理及输入/输出
  • #include <cwctype>    //宽字符分类

附录3-参考:

头文件汇总:https://blog.csdn.net/sinolzeng/article/details/44920285;

C++返回码:https://blog.csdn.net/robotkang/article/details/80659244;

Hello World背后的事情

原文:https://www.cnblogs.com/XUYICHENMO/p/13777382.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!