1. Program execution begins in the main() function (or Winmain() if you're writing a windows program)
2. #include files are always optional, even in C++. The include files are lists of declarations for library functions. If you don't call the library functions, you don't need to declare them.
3. Execution of your program ends when your main() function returns.
Now, that said, there are exceptions. If you have a global object, that object's constructor gets called before main() so your first line of code might actually be there. But that's kind of a special case. Technically there are startup routines that initialize the C++ libraries and such, and they execute before your code. But for all intents and purposes your code starts at main().
If you have the following code snippet:
// declarations
int function1();
int function2();
int function3();
function1()
{
/* do stuff here */
return 0;
}
function2()
{
/* do stuff here */
return 0;
}
int main()
{
/*program execution starts here */
function3();
function2();
function1();
}
function3()
{
/* do stuff here */
return 0;
}
/* END FILE */
In this example, code starts at the main, then executes the function 3, function 2, and function 1, in reverse order. It doesn't make any difference here that function 3 is declared last in the file and function 1 is declared first. The execution starts at main() and calls them in the order you list there. When main is finished, the program ends. It doesn't continue on with function3, which happens to be after main in the code itself. If you don't call function3 from within your program it never gets executed at all.
Hope this helps.
2006-06-16 05:18:31
·
answer #1
·
answered by Mantis 6
·
4⤊
1⤋
First, an #include statement is a preprocessor command. It simply adds, changes and ignores certain things according to the commands and how they are used. This occurs independantly and before any code is converted to instructions or static data. The main() function is the cue to the compiler and later the linker as to what is the program entry point for your code. The actual code that is used to signal the OS and pass it needed information is called (usually) "startup" code and is automatically put into the symbol table by the compiler and/or linker. The end of the program is signified by the closing brace, '}', at the end of the main function. The compiler and linker always use the opening and closing braces of any function as the entry and exit of that function. Since the one in main() are the level which start your program, they are the pairing that end it as well. That is why anything that you do outside of the main() function that isn't a global data declaration, a preprocessor command, a function declaration or function definition will produce an error or be totally ignored by the compiler.
Since C++ will compile a C program that is an error. You could program anything that you wished in C or C++ without the header files as long as the following rules are obeyed:
1) Every function declared has a valid function definition.
2) The function declaration or definition(for functions without an explicit declaration) appears before the main() function.
3) Everthing appears in correct order as above in one big, ugly source file.
Agreed that this makes it lots more work to program things and you'd need to know a lot of low level stuff to make it work, but that doesn't mean it can't be done.
2006-06-16 05:04:15
·
answer #2
·
answered by Anonymous
·
0⤊
0⤋
The program actually starts execution in some start-up code that is linked in automatically. The first line of executable code in your program is in main(). The headers may have executable code, but it doesn't get called unless something refers to the function in the header.
You can write a C++ program with no headers, but it won't do much, since you won't be able to do any input or output. At a minimum, you need
BTW, the file is no longer iostream.h; all the new C++ headers have dropped the .h; using iostream.h will use an older version of the header.
The compiler understands the end of the program is where the source file ends. If you haven't closed all your code blocks (with } ), then the compiler will give you an error.
2006-06-16 04:47:32
·
answer #3
·
answered by Flyboy 6
·
0⤊
0⤋
Programs execute the main function.
Includes do exactly what they describe, they include source code into the main program file. Think of cut and paste. The include pastes that code into the file that calls it.
I'm not sure what you mean in regards to "compiler understands the end of program". A compiler turns object code into an executable program. The operating system is responsible for running it. Programs typicially run until they exit in the source code.
As for lafore's books, I assume he just used some things that were optional in his c examples. With c++ there's some standard libs that do basic things like IO that you need if you don't have some alternative.
2006-06-16 06:52:16
·
answer #4
·
answered by Gizmo L 4
·
0⤊
0⤋
So #include is actually a preprocessor directive; it is handled by the preprocessor before your code is every compiled. If you are on a Unix platform, you may find it instructive to compile your file with the flag that says to only run the preprocessor (for gcc, it is -E). This will result in a (messy) C (or C++) file that gets fed into the compiler, and by looking at it you may get a better feel for what's really going on when you compile your code. So, particular to your question, when your program runs, it isn't like it can "execute" a #include; the #include was gone before the code was compiled, and was replaced with whatever it found in that #include file.
So when you start running your program, the operating system loader loads it. Some code from the loader itself runs, and then calls your main() routine (passing in command line arguments). When that main routine exits, your program gets unloaded and ends. There are other weird circumstances that can end it; fatal errors (like a seg fault or uncaught exception in C++), calling the exit() routine, etc. But that is basically how it works. This is all true for C or C++.
As for someone saying that header files are necessary in C++ or C, that's a bit goofy. They are advisable in either case to keep your code clear, organized, and well-documented, but are not strictly necessary in either case. Perhaps what he means is that often in C++ you are implementing a standalone object to be used by other pieces of code, and if you haven't defined that object in a header file, the other pieces of code will have no way to reference your object. But it isn't like you can't write Hello World in C++ without a header file, for instance.
Hope this helps...
2006-06-16 04:52:51
·
answer #5
·
answered by dpawson 4
·
0⤊
0⤋
The program execution begins from the main() not from the include files, ie. . These are just like supporting files while executing code, linking header files with your code while executing.
And I think some of the header files are optional in C like stdio.h, Not all. But in C++ You need to specify. This statement is true as per my experience.It may depends on compiler too.
I don't know, how the compiler knows end of program, I think, the compiler, while translating the code into Object code, it may happen.
2006-06-16 08:15:37
·
answer #6
·
answered by yaramala 2
·
0⤊
0⤋
It begins from main. The include statements are preprocessor directives which means that they are checked during your compile and used to create your executable file.
2006-06-16 06:13:48
·
answer #7
·
answered by sinkablehail1978 5
·
0⤊
0⤋
I study this two times. the way you layer longer tale-appropriate prose is admirable, and some thing I actually have a lot worry with. you're making it look difficulty-free, organic, yet all those who write understand the different is real. I frequently degree the effectiveness of a piece by ability of the fashion of TD's........Jealousy rears it really is eco-friendly head. Kudos to you, Mr Carney.
2016-10-14 05:29:29
·
answer #8
·
answered by ? 4
·
0⤊
0⤋
begins from
#include<...>.. tell the compiler what stuff is needed to run the code
header files are like stdio.h, math.h,...
2006-06-16 04:44:54
·
answer #9
·
answered by pj2024 3
·
0⤊
0⤋
I think it begins with main() if memory serves me. Everything else is just getting ready.
2006-06-16 04:45:06
·
answer #10
·
answered by ksjazzguitar 4
·
0⤊
0⤋