DOC PREVIEW
MIT 16 070 - The mechanics of program construction

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Fesq, 2/9/01 1 16.070Crash Course in C"A little learning is a dangerous thing." (Alexander Pope)• The mechanics of program construction• Structure of a C program• Simple examples of C programsFesq, 2/9/01 2 16.070The Mechanics of Program ConstructionStages of Program DevelopmentRefine designArchitecture designDetailed designEdit source filesCompile source filesLink Object filesTest and debug executable programFesq, 2/9/01 3 16.070Writing C Code• Once the program has been designed, it needs to be written in alanguage that can be translated into a format that the computer canunderstand− English is a very loose language− Computers are very rigid machines− English description of the program must be converted to a computerlanguage (C, Ada, Fortran, Pascal, Java, Basic, Assembler, etc.)• Writing a C program involves creating and editing C language textfiles, called source files• However, a computer can not execute, or run, a C source fileFesq, 2/9/01 4 16.070Creating an Executable Programsource fileobject filesource fileobject filesource fileobject filesource fileobject filelinkRuntime LibraryExecutable CodeFesq, 2/9/01 5 16.070Compiling Source Files• Once your program is written in C, it is ready to be translated into amachine-readable language• A compiler translates C statements into machine statements− A compiler creates object code, which is an intermediary step betweensource code and final executable code− The compiler checks for syntax errors; e.g., Missing punctuation− The compiler performs simple optimization on your code; e.g., eliminate aredundant statementFesq, 2/9/01 6 16.070Linking Object Files• The linker links together all object modules to form an executableimage of the program• The output of the linker is an executable image, which can be loadedinto memory and executed• The linker resolves any references to library functions− If your program uses a library routine, like sqrt, the linker finds the objectcode corresponding to this routine and links it within the final executableimage• The linker is automatically invoked by the compilerFesq, 2/9/01 7 16.070Loading Your Program• The loader loads your program into the computer's memory• On most systems, this is performed automatically by the operatingsystem when you run the program• Most embedded systems require you to explicitly run a loader programto get the program into memoryFesq, 2/9/01 8 16.070Mechanics of Program Construction - Review• Let's review the steps involved in building a C program. This assumesthat you have already designed the program and defined it using thesteps above1. Use an editor to create/edit the program and any data files− Use meaningful names for files and variables; e.g., flightsim.c vs program3.c2. Type in your program, complete with parentheses, braces, semicolons3. Build (compile and link) your program. If compiler identifies errors (e.g.,missing brace), edit your program and recompile.4. Execute your program.− Run your program using different test cases− If program does not execute properly, edit and return to step 35. After program runs successfully, make printouts of source code and of testresults.Fesq, 2/9/01 9 16.070 Structure of a C program• Program consists of one or more functions• Function consists of header and body− Header contains preprocessor statements and declarations− Body, enclosed in {brackets} contains statements, each terminated bysemicolon• One function must be one called main()− The main function is where execution of the program begins− The main function begins at the line containing main() and ends at theclosing brace on the last line of the source listing− The body of the main function contains one or more statements thatdescribe the functionality of your program• Program execution starts in main, and progresses, statement bystatement, until the last statement in main is completedFesq, 2/9/01 10 16.070Style of a C Program - Commenting and Indenting Your Code• Properly commenting and indenting your code is an important part ofprogramming− Enhance readability− Allow others to understand more quickly− Allow you to recall sooner• Provide information at beginning of each source file that describes thecode, date modified, by whom• Intersperse comments within the code to explain intent• Style guide on the class webpage. Use it!Fesq, 2/9/01 11 16.070 fcn-type fcn-name (arg-declarations) { declarations; C-statements; }Anatomy of a C Function• A function, including the main function, contains the followingelements− Function type− Function name− Left Parenthesis− Argument declarations*− Right parenthesis− Left curly bracket− Declarations*− C statements*− Right curly bracket* = optionalFesq, 2/9/01 12 16.070A Simple Example• Let's define a function that computes the square of the number 51. File name: What shall we call the file? _______________2. Function name: What shall we call the function? _______________3. Function type: What type of value will the function return?_____________4. Executable statements: Write the statement to perform the computation ___5. Return statement: Specify the output of the function __________________Fesq, 2/9/01 13 16.070A Simple Example of C Code/**************** L. Fesq ** This function ** calculates the ** square of 5 *****************/int main (void){5 * 5;return 0;} /* end main */Fesq, 2/9/01 14 16.070Critiquing our Code• This function computes the square of 5, but what does it do with theresult?• It would be helpful to store the result so we can use it again• Use a variable to allocate memory to store the result, and to label itwith a relevant name:int main (void){int answer;answer = 5 * 5;return 0;}Fesq, 2/9/01 15 16.070Critiquing our Code - cont.• If we try running our simple example, what happens? Will we be ableto see any results? How/why not?• Add a line (or two) of code to help you see what is going on in thisprogram.#include <stdio.h>int main (void){int answer;answer = 5 * 5;printf ("The square of 5 is %d\n", answer);return 0;}Fesq, 2/9/01 16 16.070An Extension to our Simple Example• The function would be more useful if it computes the square of anyinteger number• How can we provide input to the program?#include <stdio.h>int main (void){int num;int answer;printf ("Enter the integer that you would like squared: \n");scanf ("%d",


View Full Document

MIT 16 070 - The mechanics of program construction

Documents in this Course
optim

optim

20 pages

Load more
Download The mechanics of program construction
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The mechanics of program construction and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The mechanics of program construction 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?