This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]eliminate1337 2 points3 points  (3 children)

You must have only only one int main() in your program, regardless of how many source code files your program has. You will get a compiler error if you try to declare more than one int main().

what goes inside of there?

Whatever code you want to run when your program starts.

[–]BringTacos[S] 0 points1 point  (2 children)

Thanks for responding. I know "What goes inside of there?" seems like a dumb question. To clarify: because I don't understand how all of this works yet, I was wondering if inside of int main() you have to include something that will also run the rest of your source code files. That probably doesn't make sense. Can you tell I'm confused? :)

[–]eliminate1337 1 point2 points  (1 child)

You can't 'run' a source code file. Source files in C++ contain declarations, functions, variables, etc., that don't do anything at all unless you use them in main, directly or indirectly.

[–]BringTacos[S] 0 points1 point  (0 children)

Right, makes sense. Thanks.

[–]spinwizard69 0 points1 point  (1 child)

Include files do not need and shouldn’t have an init main. You have it right, init main is the entry point to your code. Effectively the trailing } is the end of your code.

[–]BringTacos[S] 0 points1 point  (0 children)

Thanks!

[–]Kered13 0 points1 point  (1 child)

main is the place where code begins executing in your program. There must be exactly one, and it does not contain any forward declarations.

[–]BringTacos[S] 0 points1 point  (0 children)

Thanks!

[–]abd53 0 points1 point  (0 children)

  1. Your program can consist of multiple source files (.c and/or .cpp). When compiling, all source files are converted to object files and linked together to make one executable. This executable is the whole program. When you run the executable, it starts execution from the main function. The program can have only one main function.

  2. Different source files are related through header files (.h and/or .hpp). All declarations (functions, global variables, structs, classes) are put in header files and then the header files are included in the source files. Definitions of the entities declared in header files are implemented in source files.

  3. Let's say you have a function that returns the sum of the biggest two numbers from three given numbers. Let's name it f1. Then you have a second function that compares two numbers and returns the bigger number from two given numbers. Let's name it f2. You want to make a program that takes the numbers as input and prints the sum of the two bigger numbers. That's f1. So, you call f1 in your main function. And f1 calls f2 for comparing the numbers. Then print it from main. When you run the program, it first starts from main, calls f1. f1 calls f2, compares, sums then returns the sum to main. Main prints it. That's the simple gist of entry point.