Example: quiz answers

Programming in C A Tutorial - Bell Labs

Programming in C A TutorialBrian W. KernighanBell Laboratories, Murray Hill, N. IntroductionC is a computer language available on theGCOSandUNIX operating systems at Murray Hill and (inpreliminary form) on OS/360 at Holmdel. C lets you write your programs clearly and simply_it has de-cent control flow facilities so your code can be read straight down the page, without labels or GOTO s; itlets you write code that is compact without being too cryptic; it encourages modularity and good programorganization; and it provides good data-structuring memorandum is a Tutorial to make learning C as painless as possible.

Programming in C A Tutorial Brian W. Kernighan Bell Laboratories, Murray Hill, N. J. 1. Introduction C is a computer language available on the GCOS and UNIX operating systems at Murray Hill and (in

Tags:

  Programming, Language, Tutorials, Programming in c

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Programming in C A Tutorial - Bell Labs

1 Programming in C A TutorialBrian W. KernighanBell Laboratories, Murray Hill, N. IntroductionC is a computer language available on theGCOSandUNIX operating systems at Murray Hill and (inpreliminary form) on OS/360 at Holmdel. C lets you write your programs clearly and simply_it has de-cent control flow facilities so your code can be read straight down the page, without labels or GOTO s; itlets you write code that is compact without being too cryptic; it encourages modularity and good programorganization; and it provides good data-structuring memorandum is a Tutorial to make learning C as painless as possible.

2 The first part concentrateson the central features of C; the second part discusses those parts of the language which are useful (usuallyfor getting more efficient and smaller code) but which are not necessary for the new user. This isnota ref-erence manual. Details and special cases will be skipped ruthlessly, and no attempt will be made to coverevery language feature. The order of presentation is hopefully pedagogical instead of logical. Users whowould like the full story should consult theC Reference Manualby D. M. Ritchie [1], which should be readfor details anyway. Runtime support is described in [2] and [3]; you will have to read one of these to learnhow to compile and run a C will assume that you are familiar with the mysteries of creating files, text editing, and the like inthe operating system you run on, and that you have programmed in some language A Simple C Programmain( ) {printf("hello, world").}

3 }A C program consists of one or morefunctions,which are similar to the functions and subroutines ofa Fortran program or the procedures of PL/I, and perhaps some external data such afunction, and in fact all C programs must have of the program begins at the first state-ment usually invoke other functions to perform its job, some coming from the sameprogram, and others from method of communicating data between functions is by arguments. The parentheses followingthe function name surround the argument list; heremainis a function of no arguments, indicated by ( ).The {} enclose the statements of the function.

4 Individual statements end with a semicolon but are other-wise a library function which will format and print output on the terminal (unless some other des-tination is specified). In this case it printsC Tutorial - 2 -hello, worldA function is invoked by naming it, followed by a list of arguments in parentheses. There is noCALL state-ment as in Fortran A Working C Program; Variables; Types and Type DeclarationsHere s a bigger program that adds three integers and prints their ( ) {int a, b, c, sum;a = 1; b = 2; c = 3;sum=a+b+c;printf("sum is %d", sum);}Arithmetic and the assignment statements are much the same as in Fortran (except for the semi-colons) format of C programs is quite free.

5 We can put several statements on a line if we want,or we can split a statement among several lines if it seems desirable. The split may be between any of theoperators or variables, butnotin the middle of a name or operator. As a matter of style, spaces, tabs, andnewlines should be used freely to enhance has four fundamentaltypesof variables:intinteger (PDP-11: 16 bits; H6070: 36 bits; IBM360: 32 bits)charone byte character (PDP-11, IBM360: 8 bits; H6070: 9 bits)floatsingle-precision floating pointdoubledouble-precision floating pointThere are alsoarraysandstructuresof these basic types,pointersto them andfunctionsthat return them,all of which we will meet in a C program must be declared, although this can sometimes be done implicitly bycontext.

6 Declarations must precede executable statements. The declarationint a, b, c, sum;declaresa, b, c,andsumto be names have one to eight characters, chosen from A-Z, a-z, 0-9, and_, and start with a non-digit. Stylistically, it s much better to use only a single case and give functions and external variablesnames that are unique in the first six characters. (Function and external variable names are used by variousassemblers, some of which are limited in the size and case of identifiers they can handle.) Furthermore,keywords and library functions may only be recognized in one ConstantsWe have already seen decimal integer constants in the previous example_1, 2, and 3.

7 Since C is of-ten used for system Programming and bit-manipulation, octal numbers are an important part of the lan-guage. In C, any number that begins with 0 (zero!) is an octal integer (and hence can t have any 8 s or 9 sin it). Thus 0777 is an octal constant, with decimal value character is one byte (an inherently machine-dependent concept). Most often this is expressedas acharacter constant,which is one character enclosed in single quotes. However, it may be any quantitythat fits in a byte, as inflagsbelow:char quest, newline, flags;quest = ? ;newline = \n ;flags = 077;C Tutorial - 3 -The sequence \n is C notation for newline character , which, when printed, skips the terminal tothe beginning of the next line.

8 Notice that \n represents only a single character. There are several other escapes like \n for representing hard-to-get or invisible characters, such as \t for tab, \b for back-space, \0 for end of file, and \\ for the backslash are discussed in section Simple I/O _getchar, putchar, printfmain( ) {char c;c = getchar( );putchar(c);}getcharandputcharare the basic I/O library functions in one character fromthe standard input (usually the terminal) each time it is called, and returns that character as the value of thefunction. When it reaches the end of whatever file it is reading, thereafter it returns the character repre-sented by \0 (asciiNUL,which has value zero).

9 We will see how to use this very one character out on the standard output (usually the terminal) each time it is called. Sothe program above reads one character and writes it back out. By itself, this isn t very interesting, but ob-serve that if we put a loop around this, and add a test for end of file, we have a complete program for copy-ing one file to a more complicated function for producing formatted output. We will talk about only thesimplest use of it. Basically,printfuses its first argument as formatting information, and any successive ar-guments as variables to be output. Thusprintf ("hello, world\n");is the simplest use_the string hello, world\n is printed out.

10 No formatting information, no variables, sothe string is dumped out verbatim. The newline is necessary to put this out on a line by itself. (The con-struction"hello, world\n"is really an array about this shortly.)More complicated, ifsumis 6,printf ("sum is %d\n", sum);printssum is 6 Within the first argument ofprintf,the characters %d signify that the next argument in the argument listis to be printed as a base 10 useful formatting commands are %c to print out a single character, %s to print out an en-tire string, and %o to print a number as octal instead of decimal (no leading zero).


Related search queries