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文件的代码区,所以从程序加载时,就已经分配好了内存用于存放这些代码:代码运行时所需要的内存, ...
随机推荐
- nginx如何限制并发连接请求数?
简介 限制并发连接数的模块为:http_limit_conn_module,地址:http://nginx.org/en/docs/http/ngx_http_limit_conn_module.ht ...
- Django:给requests发送请求功能 套一层衣服。
个人的疑问
- 用 Spring Boot 和 MybatisPlus 快速构建项目
自动生成 public class MPGenerator { public static void main(String[] args) { AutoGenerator autoGenerator ...
- HashMap位运算你可知一二
前置位运算知识 我们平时在写代码过程中用的位运算操作比较少,因为我们更关注于可读性而不是性能,如果为了性能而使用较多的位运算,我想我们的同事会疯掉.但在框架里位运算却非常常见,因为框架的性能是我们关注 ...
- Beauty Contest(POJ 2187)
原题如下: Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 42961 Accepted: ...
- ulimit 的认识
原文出自 通过 ulimit 改善系统性能 概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时 ...
- DNS递归解析和迭代解析
DNS解析流程分为递归查询和迭代查询,递归查询是以本地名称服务器为中心查询, 递归查询是默认方式,迭代查询是以DNS客户端,也就是客户机器为中心查询.其实DNS客户端和本地名称服务器是递归,而本地名称 ...
- Docker跨主机通信(九)
容器网络 在前面的博客中已经详细讲解了几种网络方案: none, host, bridge,user-defined.但是他们只是解决了单个主机间的容器的通信问题,并不能实现多个主机容器之间的通信.本 ...
- Docker多主机管理(八)
docker多主机管理 前面我们的实验环境中只有一个 docker host,所有的容器都是运行在这一个 host 上的.但在真正的环境中会有多个 host,容器在这些 host 中启动.运行.停止和 ...
- elasticsearch 索引清理脚本及常用命令
elastic索引日志清理不及时,很容易产生磁盘紧张,官网给出curl -k -XDELETE可以清理不需要的索引日志. 清理脚本 #!/bin/bash #Author: 648403020@qq. ...