atof 是ascII to float的缩写,它将ascII字符串转换为相应的单精度浮点数,比如传入"1.234",经过处理后就返回float类型的数1.234 。类似的还有atoi 、atol、itoa、ftoa等等。

示例程序,主函数使用两个值作为实参,并输出和。

#include <iostream>
using namespace std;
int main(int argc,char *argv[])
{
	if(argc!=3){
		cout<<"you should use three arguments"<<endl;
		return -1;
	}
	for(int i=0;i<argc;i++){
		cout<<argv[i]<<endl;   
	}

	cout<<"The sum is:"<<atof(argv[1])+atof(argv[2])<<endl;

	return 0;	

}


将程序编译成sum.exe, 在命令行下运行 sum 1.23 2.56, 返回3.79.


相关文章:

  • 2021-09-03
  • 2021-05-29
  • 2022-12-23
  • 2021-09-26
  • 2021-07-10
  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-18
  • 2021-09-17
  • 2022-12-23
  • 2021-08-05
  • 2021-06-11
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案