Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ Questions about the main() function in C++

Malcolm

Administrator
Administrator
Staff Team
Code Plus
Company Plus
Hello Coders!

Learning some C++ today and I'm getting a little confused. What I think I'm getting is that when a program is executed the OS looks for the main() to know what to do with the program. So I know that every program requires that at least one .cpp file has a main() function.

But what I'm getting confused about is that "return 0 indicates a successful program execution" does this mean that it got through the main() function without any errors? And, my second question what are significant of the two valid versions of the main()? E.g. below

C++:
int main(){
    // code
    return 0;
}
C++:
int main(int argc, char *argv[]){
    // code
    return 0;
}
 
The return value can usually be used by the caller that started the program to determine if the program was successful or not. This can be entirely up to you to determine the return values, but I think the unwritten rule is that a 0 return value indicates that a program ran successfully. If you're calling your application from a batch file or script, you can use the return value as part of a conditional to determine what the batch file/script does next. The return value is not constrained to scripting: it is also possible to use this value if another application is calling your C/C++ application: for example, in Java you would create a Process that executes your C++ application, and when your Java progam detects that your C++ application has terminated it can query the return code from the Java Process object.

A possible starting point would be to google "console application return code" to get an idea of how these are typically used.

The second version of the main() function is usually used when writing a program that is running in an OS that supports passing parameters to the program. argc is a count of the number of arguments you've passed to the program, and argv are the string arguments themselves. I think by convention the first value in argv is the name of the application (usually full path). You usually have to do a bunch of string parsing to determine how to interpret those arguments.

The first version of the main() function declaration is more useful in an embedded context, where there isn't an OS that allows a user to pass arguments to the program before it is run.
 
From what I am understanding the caller is referred to the main() function, the caller then looks to the return on what to do next. Am I right to assume that?

A possible starting point would be to google "console application return code" to get an idea of how these are typically used.
I'll have a look at this!

The second version of the main() function is usually used when writing a program that is running in an OS that supports passing parameters to the program. argc is a count of the number of arguments you've passed to the program, and argv are the string arguments themselves. I think by convention the first value in argv is the name of the application (usually full path). You usually have to do a bunch of string parsing to determine how to interpret those arguments.
I'm a little confused by this part, does this mean like my function is needing to use certain OS features?
 
You have to take into account that when a C++ program is made executable it is converted to machine code, but moreso that it is converted to a structure that the underlying operating system can handle. This is where references to the ELF format for linux (https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) and PE format (https://en.wikipedia.org/wiki/Portable_Executable) are relevant.

main() is usually defined as the default entry point into the application. When the compiler parses and converts your code to an application, it's written in such a way that the "first instructions" executed for your program are located in your main function. It is possible to change the name of the function you want to declare as your starting function: eg. for gcc refer to https://stackoverflow.com/questions/7494244/how-to-change-entry-point-of-c-program-with-gcc .

So your first question is partially right: the caller can look to see what the return value from your application is, but only after it has terminated. Depending on how long your application will take to run, the caller will either have to wait for it to complete or be set up to receive notification that it has completed, either through callbacks or an OS event.

I'll see if I can answer your second question: typically the OS is always setting up an environment for your application to run in. From memory allocation, to I/O streams (for printing to a terminal and reading user input), the OS manages the computer resources for all running applications. This also includes managing return codes to be made available to calling scripts and programs, and (I think) any exception handling. The function signature of allowing parameters to a program is both a convenience and convention - I think gcc can be stricter about what its requirements are for an entry point, but then again this is dependant on how gcc is compiled/configured. My experience with main() has typically been in an embedded environment where there is no underlying operating system to manage "computer" resources like memory or interprocess communication (if for some reason you were able to have 2 "processes" at that level) so parameters to the entry point function as well as any return code doesn't make sense.
 
You have to take into account that when a C++ program is made executable it is converted to machine code, but moreso that it is converted to a structure that the underlying operating system can handle. This is where references to the ELF format for linux (https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) and PE format (https://en.wikipedia.org/wiki/Portable_Executable) are relevant.
So basically once it complies it is turned into machine code and that's where the platform (OS) understands what to do with it?

main() is usually defined as the default entry point into the application. When the compiler parses and converts your code to an application, it's written in such a way that the "first instructions" executed for your program are located in your main function. It is possible to change the name of the function you want to declare as your starting function: eg. for gcc refer to https://stackoverflow.com/questions/7494244/how-to-change-entry-point-of-c-program-with-gcc .
Okay, that's what I needed. Is it like starting up a game and goes to the game menu, could that be considered as the "main()"?
So your first question is partially right: the caller can look to see what the return value from your application is, but only after it has terminated. Depending on how long your application will take to run, the caller will either have to wait for it to complete or be set up to receive notification that it has completed, either through callbacks or an OS event.
I'll see if I can answer your second question: typically the OS is always setting up an environment for your application to run in. From memory allocation, to I/O streams (for printing to a terminal and reading user input), the OS manages the computer resources for all running applications. This also includes managing return codes to be made available to calling scripts and programs, and (I think) any exception handling. The function signature of allowing parameters to a program is both a convenience and convention - I think gcc can be stricter about what its requirements are for an entry point, but then again this is dependant on how gcc is compiled/configured. My experience with main() has typically been in an embedded environment where there is no underlying operating system to manage "computer" resources like memory or interprocess communication (if for some reason you were able to have 2 "processes" at that level) so parameters to the entry point function as well as any return code doesn't make sense.
Okay, not sure if I understand these two parts fully, but that's not you that's me. I will eventually, thank you so much for this! :thinking:

So in regards to the return value: the caller won't get to know that the application is successful or not until the end.

In regards to the OS is always setting up the environment for the application. Does this mean when I use main() with parameters it's basically telling the OS the minium allocation I need to run this application?


Okay, it's okay! I figured it out! :D
 
Last edited:
Back
Top Bottom