Where are C/C++ main function's parameters?

前端 未结 10 1830
清酒与你
清酒与你 2020-11-29 02:31

In C/C++, the main function receives parameters which are of type char*.

int main(int argc, char* argv[]){
  return 0;
}

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 02:38

    The answer to this question is compiler-dependent. This means it is not treated in the C standard, so anyone can implement that as he or she would like to. This is normal since also operating systems don't have a common accepted, standard way to start processes and finish them.

    Let's imagine a simple, why-not scenario.

    The process receives by some mechanism the arguments written in the command line. argc is then just an int which is pushed to the stack by the bootstrap function the compiler put as the entry point for the process of the program (part of the runtime). The actual values are get from the operating system, and can be, say, written in a memory block of the Heap. Then the argv vector is built and the address to its first position also pushed into the stack.

    Then the function main(), which must be provided by the programmer, is called, and its return value is saved for later (nearly inmediate) use. The structures in the Heap are freed, and the exit code obtained for main is exported to the operating system. The process finishes.

提交回复
热议问题