访问命令行参数使用C [英] Accessing command line arguments in C

查看:106
本文介绍了访问命令行参数使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我,如果这是一个noob问题,但我位于C初学者,只一会儿学习。我试着写总结两个数字的程序(如PARAMS给应用程序提供)。在code是这样的:

please forgive me if this is a noob question, but i'm a beginner at C, learning only for a while. I tried to write a program that sums up two numbers (provided as params to the application). The code is like this:

#include <stdlib.h>
#include <stdio.h>

int main( int argc, char** argv)
{
   int a = atoi(argv[0]);
   int b = atoi(argv[1]);
   int sum = a+b;
   printf("%d", sum);
   return 0;
}

但我得到不正确的结果 - 巨大的数字,甚至像5 10.什么是错在这里投入较小

But I get incorrect results - huge numbers even for small inputs like 5 and 10. What is wrong here?

推荐答案

的第一个参数的节目是节目本身的名称。尝试使用下面的替代。

The first argument to the program is the name of the program itself. Try using the following instead.

int a = atoi(argv[1]); 
int b = atoi(argv[2]); 

这篇关于访问命令行参数使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆