-1

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

In this code argv[] is a string not array so how can i use as a array to perform sum operation?

Akash
  • 25
  • 1
  • 5

1 Answers1

2

You can do it like this

#include <stdio.h>
#include <stdlib.h>

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

And the result:

$ gcc test.c && ./a.out 1 2 3 4
10
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
Shy Fu
  • 46
  • 1