You need to get the data from array of arguments that the main()
takes. Takes each of the elements without taking 0 which is the name of the executable.
How the argument comes as string, need to make a conversion with strtol()
.
It has a series of checks that would be good to do, how to know if came the amount of arguments needed, if the number conversion worked and others that might be useful to the problem.
You don’t need variables to display the sum. But if you’re going to use variables for something else, don’t make them global, you almost never need.
You don’t need this static variable, I just did so by the limitation of ideone.
#include <stdio.h>
#include <stdlib.h>
static char *argv[] = { "app", "123", "456" }; //gambiarra pra simular a passagem de argumento pela linha de comando
int main(/*int argc, char *argv[] */) {
printf("Soma: %ld", strtol(argv[1], NULL, 10) + strtol(argv[2], NULL, 10));
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I don’t understand what you want, want to ask the user to enter the data?
– Maniero
i enter the command to output the program but need to pass the values 1 and 2 for the variables a and b within the program. Is it not possible to do this friction directly?. /example < a=1 b=2
– user48571