Hello World背后的事情
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 <iostream> 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背后的事情的更多相关文章
- HTML5开发,背后的事情你知道吗?
		
现在的H5越来越受到企业或者是开发者的一个大力的追捧,已经成为网络推广必不可少的一个使用的工具,相信还有很多朋友现在都不知道H5是个什么东西,本文将为大家讲的是关于H5一些分类的问题,让你进一步的去学 ...
 - C++异常处理:try,catch,throw,finally的用法
		
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
 - 辛巴学院-Unity-剑英的c#提高篇(一)主循环
		
这是测试版 辛巴学院:正大光明的不务正业. 最近刚刚离开了我服务了三年多的公司,因为一个无数次碰到的老问题,没钱了. 之前不知道做什么好的时候,机缘巧合之下和哒嗒网络的吴总聊了一下,发现了vr gam ...
 - C++异常处理: try,catch,throw,finally的用法
		
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
 - 《征服 C 指针》摘录1:什么是空指针?区分 NULL、0  和 '\0'
		
一.什么是空指针? 空指针 是一个特殊的指针值. 空指针 是指可以确保没有向任何一个对象的指针.通常使用宏定义 NULL 来表示空指针常量值. 空指针 确保它和任何非空指针进行比较都不会相等,因此经常 ...
 - 转:如何实现一个malloc
		
如何实现一个malloc 转载后排版效果很差,看原文! 任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉. ...
 - Selenium Web 自动化 - 项目实战(三)
		
Selenium Web 自动化 - 项目实战(三) 2016-08-10 目录 1 关键字驱动概述2 框架更改总览3 框架更改详解 3.1 解析新增页面目录 3.2 解析新增测试用例目录 3. ...
 - Google Kubernetes设计文档之服务篇-转
		
摘要:Kubernetes是Google开源的容器集群管理系统,构建于Docker之上,为容器化的应用提供资源调度.部署运行.服务发现.扩容缩容等功能. Pod是创建.调度和管理的最小部署单位,本文详 ...
 - C++函数内存占用
		
一个类的对象中是没有关于普通成员函数的指针的slot,只有成员变量还有虚表指针,类的成员函数的代码定义在PE文件的代码区,所以从程序加载时,就已经分配好了内存用于存放这些代码:代码运行时所需要的内存, ...
 
随机推荐
- 从通达信导出文件获取A股所有股票代号名称(至2020年2月24日)
			
下文是讲述如何从通达信的输出文件中获得股票信息,如果想用Java爬虫从网页爬取信息请参考:https://www.cnblogs.com/xiandedanteng/p/12808381.html 要 ...
 - java面试题0004-在一个类上是否可以用abstract和final同时加以修饰?
			
我们先用提干两个修饰词中的任意一个创建一个类 package components.javaTest.day4_20200910; /** * Question004: * java面试题0004-在 ...
 - java中双亲委派机制(+总结)
			
类加载器 加载类的开放性 类加载器(ClassLoader)是Java语言的一项创新,也是Java流行的一个重要原因.在类加载的第一阶段"加载"过程中,需要通过一个类的全限定名来获 ...
 - Dubbo系列之 (七)链路层那些事(1)
			
辅助链接 Dubbo系列之 (一)SPI扩展 Dubbo系列之 (二)Registry注册中心-注册(1) Dubbo系列之 (三)Registry注册中心-注册(2) Dubbo系列之 (四)服务订 ...
 - 利用 QEMU USER 模式运行 mips 程序
			
摘要 关键字: qemu mips 前述 QEMU是一个处理器模拟软件,可以用来在PC中模拟ARM.MIPS等多种架构的软硬件运行环境.QEMU主要有两种模拟模式: User Mode System模 ...
 - Ansible常用模块介绍及使用(2)
			
Ansible模块 在上一篇博客<Ansible基础认识及安装使用详解(一)–技术流ken>中以及简单的介绍了一下ansible的模块.ansible是基于模块工作的,所以我们必须掌握几个 ...
 - C#开发PACS医学影像处理系统(十九):Dicom影像放大镜
			
在XAML代码设计器中,添加canvas画布与圆形几何对象,利用VisualBrush笔刷来复制画面内容到指定容器: <Canvas x:Name="CvsGlass" Wi ...
 - 天猫精灵对接1:outh对接
			
公司的智能家居产品需要接入语音控制,目前在对接阿里语音的天猫精灵 对接天猫精灵的第一步是完成outh鉴权 https://doc-bot.tmall.com/docs/doc.htm?spm=0.76 ...
 - java8的::
			
public static void main(String[] args) throws Exception { // 第一种方法引用的类型是构造器引用,语法是Class::new,或者更一般的形式 ...
 - Ubuntu16环境安装和使用NFS
			
通过NFS服务我们可以方便的读写服务器上的文件,一起来实战Ubuntu16环境安装和使用NFS: 文章概要 本次实战由以下步骤组成: 列举环境信息: 在192.168.119.128安装NFS服务,将 ...