-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?

7
  • You will have to parse the command line arguments yourself to create an array in your C program. Commented May 18, 2018 at 3:30
  • @DavidBowling can you give me a example to add two numbers through command line? Commented May 18, 2018 at 3:35
  • atoi(argv[1]) + atoi(argv[2]) Commented May 18, 2018 at 3:38
  • what is atoi... error atoi is not defined @sergeyrar Commented May 18, 2018 at 3:41
  • linux.die.net/man/3/atoi Commented May 18, 2018 at 3:42

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.