C++从入门到放弃--2.第一个程序

目录

1.程序之前的准备

2.程序


书接上文~

既然讲了C++是什么,那就要细说怎么从入门到放弃了。

1.程序之前的准备

下载一个VisualStudio2022小龙Dev-C++

官网

也可以下VS。(好吧我承认不可能)

官网

2.程序

先贴代码~

#include <iostream>
using namespace std;

int main() {
    cout << "Hello" << endl;
    return 0;
}

这个程序的意图很简单,输出Hello并换行。

学习过C的同学可能会小吃一惊

“按理应该是这样”

#include <stdio.h>

int main(void) {
    printf("Hello\n");
    return 0;
}
  • "using namespace std;"是什么
  • <iostream>是什么
  • 咋没“void”啊
  • cout和<<是什么鬼
  • 哪来的endl
  • ......

别急,一行一行解释

#include <iostream>

#include是一个预处理器标志,这一行的意思是,告诉预处理器找到iostream并替换为iostream里面的代码。

看完iostream的代码你就知道这很省事。

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

// Copyright (C) 1997-2018 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 */

来自XiaoLoong Dev-C++ 5.16

你可能说那些//...和/*...*/的东西是什么,他们叫注释,人能看懂,编译器emm。。。直接略过

如果(不可能)你觉得不够多,那你可能没有看过istream和ostream的代码。

不展示了,来回贴代码没意思。

哎呀跑题了,来第二行。

using namespace std;

这句话意思是使用std名称空间,名称空间是一个C++概念之一(C没有),它可以防止函数,变量,常量,类,结构,共用体,数组,情书的名字重叠。

打个比方,小明开发了一个头文件。

#ifndef XIAOMING_H
#define XIAOMING_H

//(...)

int w12(int a,int b) {
    //(...)
}
//(...)
#endif

小亮也开发了一个头文件。

#ifndef XIAOLIANG_H
#define XIAOLIANG_H

//(...)

int w12(int n,int m) {
    //(...)
}
//(...)
#endif

我想用小亮的w12()函数,但是编译器会报错。

w12(1, 2);

这时候加一个名称空间就可以解决所有问题。

小明听到后,改了头文件。

(小亮抄袭我!)

#ifndef XIAOMING_H
#define XIAOMING_H

//(...)

namespace ming {
int w12(int a,int b) {
    //(...)
}
//(...)
}
#endif

 小亮:我没有

#ifndef XIAOLIANG_H
#define XIAOLIANG_H

//(...)

namespace liang {
int w12(int n,int m) {
    //(...)
}
//(...)
}
#endif

这时候我再用w12()就不会报错了

(加了一些代码)
 

liang::w12(1, 2);

编译器就不会报错了。

这个::叫作用域运算符,以后讲类时会再次提到他。

扯老远了,第三行

int main() { /*...*/ }

这个意思是说新建一个main()函数,他返回int类型

(什么叫int)

以后我们会讲到,他是一种整数数据存储类型

()是说提示它一个函数没有这个编译器会以为:

啊,他新建了一个外部变量,后面的大括号是什么?一个代码块而已。

(咋没;啊)

大括号是main()函数的主体,问题:谁调用了main()函数?

你可以理解为操作系统。

第四行

cout << "Hello" << endl;

cout(Console out,控制台输出)是什么?它是一个ostream(out-stream,输出流)类的(东西),在iostream(in-out stream,输入输出流)中定义过了。

<<是为cout准备的符号,用来控制流,官方名称叫插入流运算符,带“”的Hello叫字符串,cout输出“”里面的字符,不输出“”本身。

endl是换行的意思,这一行还有另一种写法:

cout << "Hello\n";

字符串里包含的\n就替代了换行符,它叫转义序列,暂可理解为一种字符串里的特殊符号。

第五行

return 0;

刚才说了main()变量,他返回int类型,这就是他返回的数值(0),return这个名字似乎合情合理。

拜~未完待续

允许转载,请注明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值