Hello.cpp
1. 创建c++程序源代码
vim hello.cpp
#include <iostream>
int main(void){
std::cout << "hello world" << std::endl;
return 0;
}
2. C++程序的编译
-
预处理
-
编译: 检查语法
-
汇编: 将源代码变为二进制指令
-
链接: 链接源代码中的资源文件
-
gcc 编译c++程序
gcc编译c++程序会报错:链接错误,没有链接库
collect2: error: ld returned 1 exit status
gcc增加-l
选项链接c++库后就可以成功
gcc -o xxx xxx.cpp -lstdc++
-
g++ 编译c++程序
g++ -o xxx xxx.cpp
3. C++扩展名
- .cpp
- .cc
- .c
- .cxx
4. C++头文件
#include <iostream> //c++中和I/O相关的内存都在此头文件
//C++中提供了一套和不带".h"的C头文件的替换版本
#include <stdio.h> ==> #include <cstdio>
#include <stdlib.h> ==> #include <cstdlib>
#include <string.h> ==> #include <cstring>
5. C++输入输出流
- 标准输出
C++中使用cout对象表示标准输出
#include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{
//1> 打印一个整形数
int num = 123;
//C
printf("%d\n",num);
//C++
cout << num << endl;
/*
注:
"<<"表示输出运算符
"endl"表示换行符(endline)
*/
//2> 同时打印int和double
int i = 123,duoble d= 4.56;
//C
printf("%d,%f\n",i,d);
//C++
cout << i << ',' << d << endl;
return 0;
}
- 标准输入
C++中使用cin对象表示标准输入
#include<stdio.h>
#include<iostream;
using namespace std;
int main(void)
{}
//1> 从键盘读一个int数据
int num = 0;
//C
scanf("%d",&num);
//C++
cin >> num;
/*
注:
">>"输入被称为输入操作
*/
//2> 从键盘中读取int和double
int i = 0,duoble d= 0;
//C
scanf("%d,%f",&i,&d);
//C++
cin >> i >> d;
return 0;
}