Skip to content
On this page

第一个程序 c++风格

c++风格 helloWorld

cpp
#include <iostream>
using namespace std;
int main(){
    cout << "Hello World!" << endl;
    return 0;
}
1
2
3
4
5
6
  • iostream是头文件,是input output stream的缩写。
  • 包含iostream头文件后,就可以使用coutcin这两个对象了。
  • main主要的意思,程序从main的第一句代码开始执行。
  • ;号表示一句代码的结束,;前面可以什么也不写,表示空语句

如何编译并运行程序

方法1

  1. codeblocks写入上面的代码,保存运行

方法2

  1. 使用vim写入上面的代码,保存退出(<esc>:wq)
  2. 编译
bash
g++ -g -o 1 helloworld.cpp
1
  1. 运行
bash
./1
1

c风格 helloWorld

#include <cstdio>
int main(){
    printf("Hello World!");
    return 0;
}

1
2
3
4
5
6
  • cstdio是头文件,是c stand input output的缩写。
  • 包含cstdio头文件后,就可以使用printfscanf这两个函数了。
  • main主要的意思,程序从main的第一句代码开始执行。
  • ;号表示一句代码的结束,;前面可以什么也不写,表示空语句

如果编译并运行程序

  1. vim写入上面的代码,保存退出(<esc>:wq)
  2. 编译
bash
g++ -g -o 1 helloworld.cpp
1
  1. 运行
bash
./1
1