main参数传递-1

main参数传递-1

求和函数

//sum.c
#include<stdio.h>
int sum(int N)
{
    int sum=0;
    sum = sum+N;
    return sum;
}
//main.c
#include<stdio.h>
#include<stdlib.h>

//声明
int sum(int N);

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

编译运行

gcc sum.c main.c -o main
./main 5 6 8 7

运行结果
运行结果

posted @ 2025-01-02 11:29  Arisf  阅读(1)  评论(0编辑  收藏  举报