linux 带参数main函数调用,C语言中给main函数传递参数

相信大家用C语言定义main函数时,大多数人的写法都是int main(void)。其实main函数是可以向其传递参数的,给个实例:

//给main函数传参实例:

#include

#include

//int main(int argc, char *argv[])

int main(int argc, char **argv)

{

char *array[3]  = {"./main", "hello", "world"};

if (argc < 2)

{

printf("error!\n");

printf("./main str\n");

return -1;

}

printf("The first string of your command is:%s\n", argv[0]);

printf("The second string of your command is:%s\n", argv[1]);

printf("The third string of your command is:%s\n", argv[2]);

if (!strcmp(argv[0], array[0]))

{

printf("The first command execute successfully!\n");

}

if (!strcmp(argv[1], array[1]))

{

printf("The second command execute successfully!\n");

}

if (!strcmp(argv[2], array[2]))

{

printf("The third command execute successfully!\n");

}

return 0;

}

输出结果:

(1)若输入命令为:./main

则输出结果为:

error!

The command is:./main

(2)若输入命令为:./main hello world

则输出结果为:

The first string of your command is:./main

The second string of your command is:hello

The third string of your command is:world

The first command execute successfully!

The second command execute successfully!

The third command execute successfully!

大家可以利用这个程序模型干很多事呢!

将C语言梳理一下,分布在以下10个章节中:

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值