In c or c++ from where program begins execution?i.e from #inlcude%26lt;iostream.h%26gt; or main() ?
How compiler understands the end of program?
Also i have read from book of robert lafore that header files in c are optional but they are necessary in c++,what does that mean?If true why it is optional in c?
In c or c++ from where program begins execution?i.e from #inlcude%26lt;iostream.h%26gt; or main() ?
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.
Reply:The program execution begins from the main() not from the include files, ie. %26lt;iostream.h%26gt;. 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.
Reply: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.
Reply: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...
Reply: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.
Reply:begins from
#include%26lt;...%26gt;.. tell the compiler what stuff is needed to run the code
header files are like stdio.h, math.h,...
Reply:I think it begins with main() if memory serves me. Everything else is just getting ready.
Reply: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 %26lt;iostream%26gt;
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.
Reply: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.
love song
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment