Good practice in C on the "main()" call?

Asked

Viewed 109 times

0

I’m developing a simple app for college work, and I have doubts about using the function main() in the code for instead of going out back to the beginning of the program, this would be good practice or not?

Below I have an example in my role cadastro(), note that at the end of the program I put the main(), if the user type 4, 5 or any other key, so that it does not exit the program.

void cadastro(){
    int op;
    static int linha;

    cabecalhoCadastrar();

    do{
        //printf(":::::Cadastrar cliente:::::\n\n");

        if(linha >= 5){
            printf("\nAgenda lotada!\n");
            break;
        }
        else{
            printf("\nNome: ");
            fflush(stdin);
            fgets(cliente[linha].nome, 50, stdin);

            printf("Telefone: ");
            fflush(stdin);
            scanf("%d", &cliente[linha].telefone);

        /*  printf("Endereço: (rua, número, complemento): ");
            fflush(stdin);
            fgets(cliente[linha].endereco, 150, stdin);*/

            linha++;

            printf("\nCliente cadastrado com sucesso!\n\n\n");
            system("pause");
            system("cls");

            cabecalhoCadastrar();
            printf("\n1- Cadastrar cliente\n2- Sair\n");

            fflush(stdin);
            scanf("%d", &op);
            system("cls");
        }
    }while(op == 1);
    main();
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

As I say: forget good practices. You can only follow them without causing harm to those who do not need them, which is a paradox.

It is a question of whether it is right or not, and whether it is appropriate for the context.

In this specific case it is wrong. It may even work in exercise, but if you enter the function several times main() as you are doing will accumulate in the stack and this will give a burst of memory at some point.

Depending on other factors can even cause other problems, this is the worst thing you can do, an example is when using the static which is almost never suitable, especially in this case where the recursive calls unintentionally.

When executing a function you should only returnair to the calling function. You should never call the calling function or another one that can call the calling function to avoid an unwanted loop and stack burst.

There are several other errors in this application.

  • Thank you Maniero for your comment. I would like to point out the other errors as you checked.

  • It’s a lot and already it’s another question, the fact is that you need to rethink the way you’re learning, look for another source.

Browser other questions tagged

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