linux c 的main 函数中的return 和 查看返回参数 argv 与 argc 作用

hello.c

#include <stdio.h>

int main(int argv, char* argc[])
{
    printf("hello word!\n");
    return 0;
}

编译后  直接运行   && 作用为连接命令

gcc hello.c -o main.out && ./main.out

再运行  就查看返回参数

echo $?  

argv作用:

main.c文件代码

#include <stdio.h>

int main(int argv, char* argc[])
{
    printf("argv is %d\n", argv);
    return 0;
}

先把文件编译  然后运行  

扫描二维码关注公众号,回复: 7228533 查看本文章
[root@lyy les3]# gcc main.c -o m2.out
[root@lyy les3]# ls
m2.out  main.c  main.out
[root@lyy les3]# ./m2.out -l -a
argv is 3
[root@lyy les3]# ./m2.out -l
argv is 2

argc

main.c代码:

#include <stdio.h>

int main(int argv, char* argc[])
{
    printf("argv is %d\n", argv);
    int i;
    for(i=0;i<argv;i++)
    {
        printf("argc[%d] is %s\n",i,argc[i]);
    }
    return 0;
}

编译后运行  并加上参数

[root@lyy les3]# gcc main.c -o m3.out
[root@lyy les3]# ls
m2.out  m3.out  main.c  main.out
[root@lyy les3]# ./m3.out -l -a asdfasf fdsaf
argv is 5
argc[0] is ./m3.out
argc[1] is -l
argc[2] is -a
argc[3] is asdfasf
argc[4] is fdsaf

猜你喜欢

转载自www.cnblogs.com/jasonLiu2018/p/11484242.html