473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how does a C program gets executed

I want to know how a C program gets executed. I mean what happens when we compile.
What steps compiler follow for successful execution of a program?
What happens at compile time and run time?
Can u please give me any link on internet which explains these things properly.
May 5 '10 #1
2 12132
Dheeraj Joshi
1,123 Expert 1GB
This may help you.

Regards
Dheeraj Joshi
May 5 '10 #2
donbock
2,426 Expert 2GB
You are asking about issues that are profoundly implementation-dependent. I'll provide a generic description, but you need to realize that your system may accomplish similar goals in a wildly different fashion.

Consider this simple C program:
Expand|Select|Wrap|Line Numbers
  1. world.c:
  2.    #include <stdio.c>
  3.    int main(void)
  4.       {
  5.       printf("Hello World\n");
  6.       return 0;
  7.       }
You may think that you've been invoking the compiler all this time to compile programs like this, but you've actually been invoking the compiler driver. The compiler driver invokes the following programs one after the other for you.
  • The preprocessor replaces single #include lines with the entire contents of the indicated file, omits conditionally uncompiled code, expands macros, and expands any other preprocessor directives.
  • The compiler translates a preprocessed source file into assembly language.
  • The assembler translates assembly language into an object module.
  • The linker combines one or more object modules with referenced archived object modules from libraries (such as the code for printf) and also with the C run-time library (more on that later) to create an executable image.

Now suppose you type the name of your executable image file on the command line. The command line interpreter (or shell) is itself a program that accepts input from the user and parses it into a command word plus argument list. It searches a specific list of directories for a file with the same name as the command word. If it finds a matching file then it magically determines if that file is an executable image, a batch file, or any of the other types of files that it knows how to handle. For an executable image, the shell invokes that executable image, passing it the argument list.

I mentioned earlier that the linker combines your program with the C runtime library. One element of the C runtime element is the startup function (variously called c0, crt, crt0). The startup function is the entry point into the executable image. It sets all uninitialized variables with file scope to zero; it sets all initialized variables to their initial values; it packs up the argument list into argc and argv; and it calls your main function. When main returns, the startup function does whatever cleanup is warranted and exits back to the shell.

By the way, notice that the startup function calls your main function without referencing any prototype you may or may not have provided for main -- it takes for granted that your main honors the C Standard and returns an int, not void. There is no point providing a function prototype for main, it won't be used.
May 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Joe Cybernet | last post by:
people keep interrupting my script by pressing their Stop buttons. how can I prevent?
4
by: grocery_stocker | last post by:
#include <iostream> //for cin and cout #include <iomanip> // for setw() #include <string> // for strlen() strcmp() strrev() #include <fstream> //ifstream and ofstream: file (input & output)...
0
by: chefo | last post by:
Well, the subject describes my problem quite well :-) Here is some additional information. I use a common base class for all the pages in my project. It is derived from Page and it is not...
3
by: Double Echo | last post by:
Hi all, I'm using PHP 4.4.2, and use PHP on both the command-line and the web. I am running PHP on SuSE 10 Linux , in a VMware 5.5 workstation, using Apache 2.0.55 , on my Dell laptop. ...
1
by: sk | last post by:
hello , this is sk, new to this group problem>>>> i have a .ascx file any changes done to this file do not get mapped it does not throw any errors but the compiler executes the code that was...
2
by: Frank | last post by:
My program gets a WM_QUERYENDSESSION message and asks the user if it is OK to shutdown. While he's thinking about it, Windows displays a box saying the program is not responding. If the user...
4
by: yogesh | last post by:
mysql in c++ initialize error occurs a simple program is executed in redhat9.0 , using gcc 3.2.2 compiler version ... #include <stdio.h> #include <mysql.h> #include <string.h> int main() {
4
by: Martin Arvidsson, Visual Systems AB | last post by:
Hi! I am creating an applications that executes crystal reports. When developing i want the reports to be read from a certain folder and when the program is not executed i want to use the...
7
by: oju | last post by:
Sometimes my scripts calls the server twice even if is a simple stright-forward one. I usually runs a trace in the background when I'm testing new scripts. And quite often I can see, that the...
0
by: Deshi | last post by:
As per my knowledge i come to know that when we compile source code assembly get created created at <Drive>:\WINDOWS\Microsoft.NET \Framework\v2.0.50727\Temporary ASP.NET Files (VS2005)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.