如何使用命令行参数在C(操作系统:Windows)? [英] How to use command line arguments in c (OS: Windows)?

查看:132
本文介绍了如何使用命令行参数在C(操作系统:Windows)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法做一个程序,要求用户输入的命令行上三个参数:
1)一个运算符(+, - ,*,/);
2)的整数(n)的;
3)另一整数(米)。
因此,本方案应作为一个基本的计算器生产这种格式输出:

 例如。
运算符='+'
N = 5
M = 6
输出:5 + 6
        = 11


解决方案

如果您想在用户执行可以使用的argv向量取值的程序从一个命令行参数

  INT主(INT ARGC,字符** argv的){}

所以,如果你执行你的程序如下,

  ./ PROG + 1 2

ARGV将包含follwing,

 的argv [0] ='PROG',
的argv [1] ='+',
的argv [2] ='1',
的argv [3] ='2',

所以,你可以获取从的argv 的每个值和实现自己的逻辑。

更好地理解本教程。

I'm unable to make a program that asks the user to input three arguments on the command line: 1) an operator (+, -, *, /); 2) an integer (n); 3) another integer (m). The program should thus act as a basic calculator producing the output in this format: .

e.g.
operator='+'
n=5
m=6
output: 5+6
        = 11

解决方案

If you want to take the argument from the command line while the user executes the program you can use the argv vector to fetch the values

int main(int argc, char** argv){

}

So that if you execute your program as follows,

./prog + 1 2

argv will contain the follwing,

argv[0] = 'prog',
argv[1] = '+',
argv[2] = '1',
argv[3] = '2',

So that you can fetch each value from argv and implement your logic.

Read this tutorial for better understanding.

这篇关于如何使用命令行参数在C(操作系统:Windows)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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