How do the main function inputs work in C?

Asked

Viewed 93 times

1

I’ve always used the main function as follows, creating other functions:

int main() {
    .
    .
    .
    return 0;
}

How the inputs to the main function work?

int main(int argc, char **argv){

We have a variable and a char, right? So, I have to provide these values before running the program, correct?

Could shell run the following line, for example:

./bin/programa -e ./data/entrada.txt  -s ./data/saida.net

Correct?

1 answer

3


You have an array of strings (char **argv) of integer size (int argc). It is important to note that the parameter of position 0 is the name of the program itself, and the next ones are the parameters passed in the command line so your example would give us the following:

argv[0] = "./bin/programa"
argv[1] = "-e"
argv[2] = "./data/entrada.txt"
argv[3] = "-s"
argv[4] = "./data/saida.net"
  • 1

    @Guilhermebrügger That’s right, I put that it was whole but I didn’t explain what it was! Thanks!

  • Got it! And the values (int for argc and char for argv) are saved in these "variables"? Can I use them in the function, correct?

  • @Klel can use yes. If you think the answer helped you can accept it as a solution. It helps people who respond :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.