(相关资料图)
#include短短的8行代码包含的信息量不少using namespace std; int main(int argc, char** argv) { int input = 10; cout << "Hello, world!\n" << endl; cin >> input; return 0; }
#includemain函数传入三个参数案例using namespace std; int main(int argc, char** argv) { cout << "argc = " << argc << endl; cout << "argv[0] = " << argv[0] << endl; return 0; }
#includeusing namespace std; int main(int argc, char** argv) { cout << "argc = " << argc << endl; cout << "argv[0] = " << argv[0] << endl; if(argc == 3) { int a = stoi(argv[1]); int b = stoi(argv[2]); cout << "a+b = " << a+b << endl; } else { cout << "传入的参数个数不对"< View Code当项目有需求,如让你写个小工具自动化批处理文件时,使用命令行给main函数传参就会显得很实用。
- main函数返回值
- 编写main函数梭哈return 0可不是个好习惯,应该根据不同情况返回不同值,方便程序执行过程中就可以得知程序是那个环节出现问题。
- main函数中的return 0是返回给操作系统的,如不需要返回给操作系统可定义void main(),常见于单片机程序。
- return 0 exit(0) _exit(0)区别,
臭名昭著的“Hello,World!”果然不简单
- return是关键字,exit(0)和_exit(0)是函数
- return是返回给上一级,exit(0)和_exit(0)是退出进程,在main函数里,return 0 和 exit(0) 效果一样
- exit(0)退出进程前刷新IO缓冲区,_exit(0)属于内核中的系统调用,不会刷新IO缓冲区
- 详细区别见https://www.zhihu.com/question/545048629
标签:
Copyright © 2015-2022 东方科普网版权所有 备案号:沪ICP备2020036824号-8 联系邮箱:562 66 29@qq.com