如何检查是否给出了参数? [英] How to check if an argument was given or not?

查看:32
本文介绍了如何检查是否给出了参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//Checks if an argument was specified
if (argv[1] != "")
    strcpy(Buff1, argv[1]);
else
    strcpy(Buff1, "default");

如果我运行:./程序测试

Buff1 = 测试

如果我运行:./program

Buff1 = PACKAGES/=packages

Buff1 = PACKAGES/=packages

如果未指定任何内容,我该如何设置 Buff1 将是默认";默认?

How do I make it if nothing was specified, that Buff1 would be "default" by default?

推荐答案

使用 argc 来确定参数计数.如果没有给出参数,则等于 1,如果给出一个参数,则等于 2.

Use argc to determine arguments count. It will be equal to 1 if no arguments were given, and 2 if one argument was given.

请注意,您不能使用 == 运算符比较 C 字符串.这是指针比较.

Note that you can't compare C strings using == operator. It's pointers comparasion.

这篇关于如何检查是否给出了参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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