📄 ct1
字号:
.sp |2.5i.ce 3.ps 12.ft GProgramming in C _ A Tutorial.ps 10.sp.ft RBrian W. Kernighan.ft I.spBell Laboratories, Murray Hill, N. J..sp |4.1i.ft R.ps 10.fi.vs 12p.NHIntroduction.PPC is a computer languageavailable on the.UC GCOSand.UC UNIXoperating systems at Murray Hill and (in preliminary form) on OS/360 at Holmdel.C lets you write your programs clearly and simply _it has decent control flow facilities so your code can be readstraight down the page, without labels or GOTO's;it lets you write code that is compact withoutbeing too cryptic;it encourages modularity and good program organization;and it provides good data-structuring facilities..PPThis memorandum is a tutorial to make learning C as painless as possible.The first part concentrates on the central features of C;the second part discussesthose parts of the language which areuseful (usually for getting more efficientand smaller code)but which are not necessary for the new user.This is.ulnota reference manual.Details and special cases will be skipped ruthlessly,and no attempt will be made to cover every language feature.The order of presentation is hopefully pedagogicalinstead of logical.Users who would like the full story should consult the.ulC Reference Manualby D. M. Ritchie [1],which should be read for details anyway.Runtime support is described in[2] and [3];you will have to read one of these to learn howto compile and run a C program..PPWe will assume that you are familiar with the mysteries of creating files,text editing, and the likein the operating system you run on,and that you have programmed in some language before..NHA Simple C Program.PP.E1main(~) { printf("hello, world");}.E2.PPA C program consists of one or more.ulfunctions,which are similar tothe functions and subroutines of a Fortran program or the proceduresof PL/I,and perhaps some external data definitions..UL mainis such a function, and in fact all C programs must have a.UL main\*.Execution of the program begins at the first statement of .UL main\*..UL mainwill usually invoke other functions to perform its job, somecoming from the same program, and others from libraries..PPOne method of communicating data between functionsis by arguments.The parentheses following the function name surround the argument list;here.UL mainis a function of no arguments, indicated by (~).The {} enclose the statements of the function.Individual statements end with a semicolonbut are otherwise free-format..PP.UL printfis a library function which will format and printoutputon the terminal (unless some other destination isspecified).In this case it prints.E1hello, world.E2A function is invoked by naming it,followed by a list of arguments in parentheses.There isno.UC CALLstatement as in Fortran or .UC PL/I..NHA Working C Program; Variables; Types and Type Declarations.PPHere's a bigger program that adds three integers and prints their sum..E1main(~) { int a, b, c, sum; a = 1; b = 2; c = 3; sum = a + b + c; printf("sum is %d", sum);}.E2.PPArithmetic and the assignment statements are muchthe same as in Fortran (except for the semicolons)or.UC PL/I.The format of C programs is quite free.We can put several statements on a line if we want,or we can split a statement among several lines ifit seems desirable. The split may be between any of the operators or variables,but.ulnotin the middle of a name or operator.As a matter of style,spaces, tabs, and newlines should be used freelyto enhance readability..PPC has four fundamental.ultypesof variables:.DS\fGint\fR integer (PDP-11: 16 bits; H6070: 36 bits; IBM360: 32 bits)\fGchar\fR one byte character (PDP-11, IBM360: 8 bits; H6070: 9 bits)\fGfloat\fR single-precision floating point\fGdouble\fR double-precision floating point.DEThere are also.ularraysand.ulstructuresof these basic types,.ulpointersto themand.ulfunctionsthat return them,all of which we will meet shortly..PP.ulAllvariables in a C program must be declared,although this can sometimes be done implicitly by context.Declarations must precede executable statements.The declaration.E1int a, b, c, sum;.E2declares.UL a,.UL b,.UL c,and.UL sumto be integers..PPVariable names have one to eight characters, chosen from A-Z, a-z, 0-9, and \(ul,and start with a non-digit.Stylistically, it's much better to use only a single caseand give functions and external variables names that are unique in the firstsix characters.(Function and external variable names are used by various assemblers, some of which are limitedin the size and case of identifiers they can handle.)Furthermore, keywords and library functionsmay only be recognized in one case..NHConstants.PPWe have already seen decimal integer constants inthe previous example _1, 2, and 3.Since C is often used for system programming and bit-manipulation, octalnumbers are an important part of the language.In C, any number that begins with 0(zero!)is an octal integer (and hence can't haveany 8's or 9's in it).Thus 0777 is an octal constant, with decimal value 511..PPA ``character'' is one byte(an inherently machine-dependent concept).Most often this is expressed as a .ulcharacter constant,which is one character enclosed in single quotes.However, it may be any quantity that fits in a byte,as in.UL flagsbelow:.E1char quest, newline, flags;quest = '?';newline = '\\n';flags = 077;.E2.PPThe sequence `\\n' is C notation for ``newline character'', which, when printed, skipsthe terminal to the beginning of the next line.Notice that `\\n' represents only a single character.There are several other ``escapes'' like `\\n' for representing hard-to-get or invisiblecharacters,such as`\\t' for tab,`\\b' for backspace,`\\0' for end of file,and`\\\\' for the backslash itself..PP.UL floatand.UL doubleconstantsare discussed in section 26..NHSimple I/O _ getchar, putchar, printf.PP.E1main( ) { char c; c = getchar(~); putchar(c);}.E2.PP.UL getcharand.UL putcharare the basic I/O library functions in C..UL getcharfetches one characterfrom the standard input(usually the terminal)each time it is called, and returns that characteras thevalue of the function.When it reaches the end of whatever file it is reading,thereafter it returns the character represented by `\\0'(ascii.UC NUL,which has value zero).We will see how to use this very shortly..PP.UL putcharputs one character out on the standard output(usually the terminal)each time it is called.So the program abovereads one character and writes it back out.By itself, this isn't very interesting,but observe that if we put a loop around this,and add a test for end of file,we have a complete program forcopying one file to another..PP.UL printfis a more complicated functionfor producing formatted output.We will talk about only the simplest useof it.Basically,.UL printfuses its first argument as formatting information,and any successive argumentsas variables to be output.Thus.E1printf ("hello, world\\n");.E2is the simplest use _the string ``hello, world\\n''is printed out.No formatting information, no variables,so the string is dumped out verbatim.The newline is necessary to put this out on a line by itself.(The construction.E1"hello, world\\n".E2is really an array of.UL chars\*.More about this shortly.).PPMore complicated, if.UL sumis6,.E1printf ("sum is %d\\n", sum);.E2prints.E1sum is 6.E2Within the first argument of.UL printf,the characters ``%d'' signify that the next argumentin the argument list is to be printed as abase 10number..PPOther useful formatting commands are ``%c'' to print out a single character,``%s'' to print out an entire string,and ``%o'' to print a number as octal instead of decimal(no leading zero).For example,.E1n = 511;printf ("What is the value of %d in octal?", n);printf (" %s! %d decimal is %o octal\\n", "Right", n, n);.E2prints.E1.fiWhat is the value of 511 in octal?Right! 511 decimal is 777 octal.E2Notice that there is no newline at the end of the firstoutput line.Successive calls to.UL printf(and/or.UL putchar,for that matter)simply put out characters.No newlines are printed unless you ask for them.Similarly, on input, characters are read one at a timeas you ask for them.Each line is generally terminated by a newline (\\n),but there is otherwise no concept of record.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -