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文件的代码区,所以从程序加载时,就已经分配好了内存用于存放这些代码:代码运行时所需要的内存, ...
随机推荐
- Spine学习一 -渲染组件
一共有四个播放的组件: SkeletonAnimation:有点儿类似于 unity的 Animation,挂上一个spine资源,就可以跑了 SkeletonRenderer:SkeletonAni ...
- java中equals与hashCode的重写问题
这几天有一个朋友问我在重写equals和hashCode上出现了问题,最后我帮她解决了问题,同时也整理出来分享给大家 现上Object的equals与HashCode的代码 public boolea ...
- 深入了解Netty【五】线程模型
引言 不同的线程模型对程序的性能有很大的影响,Netty是建立在Reactor模型的基础上,要搞清Netty的线程模型,需要了解一目前常见线程模型的一些概念. 具体是进程还是线程,是和平台或者编程语言 ...
- javascript面试题(二)
24. function foo() { } var oldName = foo.name; foo.name = "bar"; [oldName, foo.name] // [f ...
- 区间查询与等效minus查询
--表结构 create table hy_emp( id number(4,0) primary key, name nvarchar2(20) not null, edate date) --充值 ...
- MySQL互联网业务使用建议
一.基础规范 表存储引擎必须使用InnoDB 表字符集默认使用utf8,必要时候使用utf8mb4 解读: (1)通用,无乱码风险,汉字3字节,英文1字节 (2)utf8mb4是utf8的超集,有存储 ...
- jQuery的那些事儿
jQuery概述 j-JavaScript+Query就是查询js的库,把js中的DOM操作做了封装,实现快速查询使用其中的功能. 优化了DOM操作.事件处理.动画设计和Ajax交互 学习jQuery ...
- leetcode刷题-56合并区间
题目 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]] 思路 通过设置一个移 ...
- vps+v_2_ray+proxychains
电脑系统换到Linux快半年了,之前一直没有解决的问题是怎么上google,毕竟有些东西还是google上好找一点.最近不想复习,没想到自己成功搭了个梯子,着实把惊喜了我一把.下面记录一下过程. 首先 ...
- [03] C# Alloc Free编程
C# Alloc Free编程 首先Alloc Free这个词是我自创的, 来源于Lock Free. Lock Free是说通过原子操作来避免锁的使用, 从而来提高并行程序的性能; 与Lock Fr ...