20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Compil<strong>in</strong>g Programs<br />

9<br />

When all the syntactic and semantic errors have been removed from the program, the<br />

compiler then proceeds to take each statement of the program and translate it <strong>in</strong>to a<br />

“lower” form. On most systems, this means that each statement is translated by the compiler<br />

<strong>in</strong>to the equivalent statement or statements <strong>in</strong> assembly language needed to perform<br />

the identical task.<br />

After the program has been translated <strong>in</strong>to an equivalent assembly language program,<br />

the next step <strong>in</strong> the compilation process is to translate the assembly language statements<br />

<strong>in</strong>to actual mach<strong>in</strong>e <strong>in</strong>structions.This step might or might not <strong>in</strong>volve the execution of a<br />

separate program known as an assembler. On most systems, the assembler is executed<br />

automatically as part of the compilation process.<br />

The assembler takes each assembly language statement and converts it <strong>in</strong>to a b<strong>in</strong>ary<br />

format known as object code, which is then written <strong>in</strong>to another file on the system.This<br />

file typically has the same name as the source file under Unix, with the last letter an “o”<br />

(for object) <strong>in</strong>stead of a “c”. Under W<strong>in</strong>dows, the suffix letters "obj" typically replace the<br />

“c” <strong>in</strong> the filename.<br />

After the program has been translated <strong>in</strong>to object code, it is ready to be l<strong>in</strong>ked. This<br />

process is once aga<strong>in</strong> performed automatically whenever the cc or gcc command is<br />

issued under Unix.The purpose of the l<strong>in</strong>k<strong>in</strong>g phase is to get the program <strong>in</strong>to a f<strong>in</strong>al<br />

form for execution on the computer. If the program uses other programs that were previously<br />

processed by the compiler, then dur<strong>in</strong>g this phase the programs are l<strong>in</strong>ked<br />

together. Programs that are used from the system’s program library are also searched and<br />

l<strong>in</strong>ked together with the object program dur<strong>in</strong>g this phase.<br />

The process of compil<strong>in</strong>g and l<strong>in</strong>k<strong>in</strong>g a program is often called build<strong>in</strong>g.<br />

The f<strong>in</strong>al l<strong>in</strong>ked file, which is <strong>in</strong> an executable object code format, is stored <strong>in</strong> another<br />

file on the system, ready to be run or executed. Under Unix, this file is called a.out by<br />

default. Under W<strong>in</strong>dows, the executable file usually has the same name as the source file,<br />

with the c extension replaced by an exe extension.<br />

To subsequently execute the program, all you do is type <strong>in</strong> the name of the executable<br />

object file. So, the command<br />

a.out<br />

has the effect of load<strong>in</strong>g the program called a.out <strong>in</strong>to the computer’s memory and <strong>in</strong>itiat<strong>in</strong>g<br />

its execution.<br />

When the program is executed, each of the statements of the program is sequentially<br />

executed <strong>in</strong> turn. If the program requests any data from the user, known as <strong>in</strong>put, the<br />

program temporarily suspends its execution so that the <strong>in</strong>put can be entered. Or, the<br />

program might simply wait for an event, such as a mouse be<strong>in</strong>g clicked, to occur. Results<br />

that are displayed by the program, known as output, appear <strong>in</strong> a w<strong>in</strong>dow, sometimes called<br />

the console. Or, the output might be directly written to a file on the system.<br />

If all goes well (and it probably won’t the first time the program is executed), the program<br />

performs its <strong>in</strong>tended functions. If the program does not produce the desired<br />

results, it is necessary to go back and reanalyze the program’s logic.This is known as the<br />

debugg<strong>in</strong>g phase, dur<strong>in</strong>g which an attempt is made to remove all the known problems or<br />

bugs from the program.To do this, it will most likely be necessary to make changes to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!