Getting Started in C Programming -------------------------------- Background ---------- The C programming language is one of the preeminent applications languages of the 1990s. The language was developed in the 1970s as a development language for the UNIX operating system. Since then, C has evolved into the underlying applications and development language for major spreadsheet, database and word processing programs, the primary language for scientific and engineering applications, and a teaching language for introducing structured programming language concepts in first and second level programming courses. It is commonly used in visualization programming as well. Introduction to Programming Terminology --------------------------------------- computer program A sequence of instructions used to operate a computer to produce a specific result. programming The process of writing these instructions in a language that the computer can respond to and other programmers can understand. programming language The set of instructions that can be used to construct a program. What do programs do? They direct a computer to accept data (input), to manipulate the data (process) and to produce reports (output). library functions These are prepackaged groups of instructions in C. Why so many languages? There are vast differences in the types of input data, calculations needed and output reports required by applications. EX: Scientific and engineering applications require precise numerical outputs, accurate to many decimal places. In addition, these applications typically use many mathematical equations to produce their results. Programming Language History ---------------------------- FORTRAN FORmula TRANslation was developed in 1957 and was specifically developed for translating formulas into a computer readable form. COBOL COmmon Business Oriented Language was commercially introduced in the 1960s and is used for business applications. BASIC Beginners All-purpose Symbolic Instruction Code was developed in the 1960s and is used for creating small, easily developed, interactive programs. Pascal Pascal was developed in the late 1970s to provide students with a firmer foundation in modular and structured programming than could be provided by BASIC. Pascal was named after the 17th century mathematician, Blaise Pascal. C C was initially developed in the 1970s from a language called B, which was developed from the BCPL language. C has an extensive set of capabilities and is a true general-purpose programming language. It can be used for creating interactive programs; for producing sophisticated applications, such as designing operating systems; and for both business and scientific programming applications. The current standard for C is maintained by the American National Standards Institute (ANSI). Algorithms ---------- What is an algorithm? An algorithm is a step-by-step sequence of instructions that describes how to perform a computation. An algorithm answers the question: "What method will you use to solve this computational problem?" Example computational problem: Q: Add the numbers from 1 to 5 Alg: (Abbreviation for algorithm) 1. set index = 1 2. set sum = 0 3. while ($index <= 5) 3.1 @ sum += $index 3.2 @ index++ end 4. echo $sum This algorithm above is written in a format that we already know (the C shell script language). An algorithm may be formulated in any fashion. Here is another way to represent the same problem above. Alg: 1. set index equal to 1 2. initialize sum equal to 0 3. for each value of index from 1 to 5 add it to the sum 4. print back the sum How about using the formula given in the book on page 7: The sum of n numbers from 1 to n is: n(n+1) sum = ------ 2 EX: if n = 5, sum = 5(5+1)/2 sum = 5(6)/2 sum = 30/2 sum = 15 1 + 2 + 3 + 4 + 5 does equal 15. Here is the algorithm: Alg: set n equal to 5 compute sum with formula : sum = n(n+1)/2 print sum Are you getting the idea about what an algorithm is? It doesn't matter what language you write it in. But what does matter is that all the steps are laid out in some logical order. A flowchart is another way of representing an algorithm by use of pictures. See Figure 1-5 in the book. The term pseudocode is used when English phrases are used to describe an algorithm. Another example of pseudocode and the flowchart is given on page 10 of the book. From Algorithms to Programs --------------------------- After selecting an algorithm, it must be converted into a form that can be used by a computer. coding Converting an algorithm into a computer program using a language such as C. Program Translation ------------------- Once a program is written in C, using your favorite editor, it cannot be executed without further translation on the computer. This is because the internal language of all computers consists of a series of 1s and 0s, called the computer's machine language. source program The C program written using an editor. interpreted language Each statement of the source program is translated individually and executed immediately. interpreter The program doing the translation in an interpreted language. compiled language All statements of the source program are translated before any one statement is executed. compiler The program doing the translation in a compiled language. object program The translated source program resulting from the compiler. Compiling a C Program --------------------- Compiling a C program in UNIX may be performed by the general C compiler or other compilers that are available. Currently 2 compilers that may be used are: The standard SGI C compiler and GNU C compiler. To create a C program and compile it, follow these steps: 1. Create the C program by editing a file that has a name ending in .c . EX: vi file.c EX: ned program1.c 2. Compiling with the SGI C compiler is performed by entering: cc filename.c where filename.c is the name of the C program you just created. Compiling with the GNU C compiler is performed by entering: gcc filename.c 3. This will create the executable called: a.out To execute the program then type: a.out If you want to change the name of the executable file (a.out), you may specify the option: "-o filename". This is done like this: EX: cc filename.c -o filename EX: cc prog1.c -o program EX: gcc help.c -o helper In these examples, the first one compiles the C program called "filename.c" and creates the executable file called "filename". The second example compiles the C program called "prog1.c" and creates the executable file called "program". The third example compiles the file called "help.c" and creates the executable file called "helper". "helper" may be executed by typing: helper Introduction to Modularity -------------------------- A well designed program is constructed using a design philosophy similar to constructing a well-designed building or drawing a structure picture. In programming, the term structure has two interrelated meanings. The first refers to the program's overall construction. The second refers to the form used to carry out individual tasks within the program. The first case refers to breaking your algorithm (program) into separate tasks (files). These are then combined together to make up your whole program. Example: You might have a program that computes the sum and difference of two numbers. One module (or separate file) may compute the sum. The other module (another file) computes the difference. These are linked together in one module called the main module. add 2 numbers module / main module \ subtract 2 numbers module main.c This is the main module. add.c This is the add module. subtract.c This is the subtract module. Inside of each module there is a specific job or task being performed. Each of these may be referred to as a function. A function has a name associated with it and is called upon from somewhere else to do a specific task. So inside of main.c there is a function called: main() Inside of the other modules there might be functions called: add() and subtract() in add.c and subtract.c, respectively. Functions --------- What are functions? Functions take some data, perform some operation(s) on it and returns the result(s) back. Format of the function name: 1. The name must begin with a letter or underscore. 2. Only letters, digits or underscores may follow the initial letter. Blank spaces are not allowed. Use the underscore to separate words in a name consisting of multiple words. 3. A name cannot be one of the keywords listed in Table 1-2 on page 15 of the book. A keyword is a word that is set aside by the language for a special purpose and should only be used in a specified manner. 4. Only the first 31 characters of a name are actually used and recorded by the computer. Some systems allow more. 5. All function names are followed by parenthesis like: main() add() subtract() The main() Function ------------------- This function provides for orderly placement and execution of functions in a C program. This function is always required in a C program and is always executed first. Here is an example of it's format: main() { program goes here } This is the simplest form of a program written in C.