11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

66 CTotal = Total + 1;Total += 1;Total ++;Unlike Pascal’s two separate kinds <strong>of</strong> procedures (func,or function, which returns a value, <strong>and</strong> proc, or procedure,which does not), C has only functions. Arguments arepassed to functions by value, but can be passed by referenceby using a pointer. (See procedures <strong>and</strong> functions.)Sample ProgramThe following is a brief example program:#include float Average (void);main () {printf (“The average is: %f”, Average() );}float Average (void) {int NumbersRead = 0;int Number;int Total = 0;while (scanf(“%d\n”, &Number) == 1){Total = Total + Number;NumbersRead = NumbersRead + 1;}return (Total / NumbersRead);}}Statements at the beginning <strong>of</strong> the program that beginwith # are preprocessor directives. These make changes tothe source code before it is compiled. The #include directiveadds the specified source file to the program. Unlike manyother languages, the C language itself does not includemany basic functions, such as input/output (I/O) statements.Instead, these are provided in st<strong>and</strong>ard libraries.(The purpose <strong>of</strong> this arrangement is to keep the languageitself simple <strong>and</strong> portable while keeping the implementation<strong>of</strong> functions likely to vary on different platforms separate.)The stdio.h file here is a “header file” that definesthe I/O functions, such as printf() (which prints formatteddata) <strong>and</strong> scanf() (which reads data into the program <strong>and</strong>formats it).The next part <strong>of</strong> the program declares any functions thatwill be defined <strong>and</strong> used in the program (in this case, thereis only one function, Average). The function declarationbegins with the type <strong>of</strong> data that will be returned by thefunction to the calling statement (a floating point value inthis case). After the function name comes declarations forany parameters that are to be passed to the function by thecaller. Since the Average function will get its data from userinput rather than the calling statement, the value (void) isused as the parameter.Following the declaration <strong>of</strong> Average comes the main()function. Every C program must have a main function. Mainis the function that runs when the program begins to execute.Typically, main will call a number <strong>of</strong> other functionsto perform the necessary tasks. Here main calls Averagewithin the printf statement, which will print the average asreturned by that function. (Calling functions within otherstatements is an example <strong>of</strong> C’s concise syntax.)Finally, the Average function is defined. It uses a loopto read in the data numbers, which are totaled <strong>and</strong> thendivided to get the average, which is sent back to the callingstatement by the return statement.A programmer could create this program on a UNIXsystem by typing the code into a source file (test.c in thiscase) using a text editor such as vi. A C compiler (gcc inthis case) is then given the source code. The source code iscompiled, <strong>and</strong> linked, creating the executable program filea.out. Typing that name at the comm<strong>and</strong> prompt runs theprogram, which asks for <strong>and</strong> averages the numbers.% gcc test.c% a.out579.The average is: 7.000000Success <strong>and</strong> ChangeIn the three decades after its first appearance, C became one<strong>of</strong> the most successful programming languages in history.In addition to becoming the language <strong>of</strong> choice for mostUNIX programming, as microcomputers became capable <strong>of</strong>running high-level languages, C became the language <strong>of</strong>choice for developing MS-DOS, Windows, <strong>and</strong> Macintoshprograms. The application programming interface (API) forWindows, for example, consists <strong>of</strong> hundreds <strong>of</strong> C functions,structures, <strong>and</strong> definitions (see application programminginterface <strong>and</strong> Micros<strong>of</strong>t Windows).However, C has not been without its critics amongcomputer scientists. Besides containing idioms that canencourage cryptic coding, the original version <strong>of</strong> C (asdefined in Kernighan <strong>and</strong> Ritchie’s The C ProgrammingLanguage) did not check function parameters to makesure they matched the data types expected in the functiondefinitions. This problem led to a large number <strong>of</strong>hard-to-catch bugs. However, the development <strong>of</strong> ANSIst<strong>and</strong>ard C with its stricter requirements, as well as typechecking built into compilers has considerably amelioratedthis problem. At about the same time, C++ becameavailable as an object-oriented extension <strong>and</strong> partial rectification<strong>of</strong> C. While C++ <strong>and</strong> Java have considerablysupplanted C for developing new programs, C programmershave a relatively easy learning path to the newerlanguages <strong>and</strong> the extensive legacy <strong>of</strong> C code will remainuseful for years to come.Further ReadingKernighan, B. W., <strong>and</strong> D. M. Ritchie. The C Programming Language,2nd ed. Upper Saddle River, N.J.: Prentice-Hall, 1988.Prata, Stephen. C Primer Plus. 5th ed. Indianapolis: SAMS, 2004.Ritchie, D. M. “The Development <strong>of</strong> the C Language,” in History <strong>of</strong>Programming Languages II, ed. T. J. Bergin <strong>and</strong> R. G. Gibson,678–698. Reading, Mass.: Addison-Wesley, 1995.

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

Saved successfully!

Ooh no, something went wrong!