• main返回值

main的返回值被用来指示状态。返回值0表明成功,非0的返回值的含义由系统定义,通常用来指出错误类型.

  • 两个编译器的使用:g++和vs的cl

写一个简单的测试程序:

#include <iostream>

int main()
{
    std::cout << "hello world" << std::endl;
    return 0;
}

g++:

g++ -o progl progl.cc

-o progl是编译器参数,指定了可执行文件的文件名。

clVS2013 开发人员命令提示启动:

cl /EHsc prog1.cc

/EHsc是编译器选项,用来打开标准异常处理。

都会生成exe,运行它:

D:\cpp\cpp_primer\ch1>prog1
hello world
  • 标准库定义了4个IO对象:

    1. istream:cin
    2. ostream:cout, cerr, clog
  • /**/不能嵌套。

  • 读取数量不定的输入数据:

#include <iostream>
int main()
{
    int sum = 0, n;
    while(std::cin >> n) {
        sum += n;
    }
    std::cout << sum << std::endl;
    return 0;
}

编译后运行:

D:\cpp\cpp_primer\ch1>readnum.exe
1 2 3^Z
6

注意用^Z(ctrl+z)结束输入。

相关文章:

  • 2021-05-27
  • 2021-12-18
  • 2022-02-22
  • 2021-10-08
  • 2021-04-27
  • 2022-01-16
  • 2021-11-15
  • 2022-01-17
猜你喜欢
  • 2022-12-23
  • 2021-10-11
  • 2021-10-10
  • 2021-08-08
  • 2022-12-23
  • 2021-05-27
  • 2022-01-09
相关资源
相似解决方案