home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / 2014.11.minnie.tuhs.org.tar / minnie.tuhs.org / UnixArchive / PDP-11 / Trees / V6 / usr / doc / ctut / ct1 < prev    next >
Text File  |  1975-06-27  |  9KB  |  363 lines

  1. .sp |2.5i
  2. .ce 3
  3. .ps 12
  4. .ft G
  5. Programming in C _ A Tutorial
  6. .ps 10
  7. .sp
  8. .ft R
  9. Brian W. Kernighan
  10. .ft I
  11. .sp
  12. Bell Laboratories, Murray Hill, N. J.
  13. .sp |4.1i
  14. .ft R
  15. .ps 10
  16. .fi
  17. .vs 12p
  18. .NH
  19. Introduction
  20. .PP
  21. C is a computer language
  22. available on the
  23. .UC GCOS
  24. and
  25. .UC UNIX
  26. operating systems at Murray Hill and (in preliminary form) on OS/360 at Holmdel.
  27. C lets you write your programs clearly and simply _
  28. it has decent control flow facilities so your code can be read
  29. straight down the page, without labels or GOTO's;
  30. it lets you write code that is compact without
  31. being too cryptic;
  32. it encourages modularity and good program organization;
  33. and it provides good data-structuring facilities.
  34. .PP
  35. This memorandum is a tutorial to make learning C as painless as possible.
  36. The first part concentrates on 
  37. the central features of C;
  38. the second part discusses
  39. those parts of the language which are
  40. useful (usually for getting more efficient
  41. and smaller code)
  42. but which are not necessary for the new user.
  43. This is
  44. .ul
  45. not
  46. a reference manual.
  47. Details and special cases will be skipped ruthlessly,
  48. and no attempt will be made to cover every language feature.
  49. The order of presentation is hopefully pedagogical
  50. instead of logical.
  51. Users who would like the full story should consult the
  52. .ul
  53. C Reference Manual
  54. by D. M. Ritchie [1],
  55. which should be read for details anyway.
  56. Runtime support is described in
  57. [2] and [3];
  58. you will have to read one of these to learn how
  59. to compile and run a C program.
  60. .PP
  61. We will assume that you are familiar with the mysteries of 
  62. creating files,
  63. text editing, and the like
  64. in the operating system you run on,
  65. and that you have programmed in some language before.
  66. .NH
  67. A Simple C Program
  68. .PP
  69. .E1
  70. main(~) {
  71.     printf("hello, world");
  72. }
  73. .E2
  74. .PP
  75. A C program consists of one or more
  76. .ul
  77. functions,
  78. which are similar to
  79. the functions and subroutines of a Fortran program or the procedures
  80. of PL/I,
  81. and perhaps some external data definitions.
  82. .UL main
  83. is such a function, and in fact all C programs must have a
  84. .UL main\*.
  85. Execution of the program begins at the first statement of 
  86. .UL main\*.
  87. .UL main
  88. will usually invoke other functions to perform its job, some
  89. coming from the same program, and others from libraries.
  90. .PP
  91. One method of communicating data between functions
  92. is by arguments.
  93. The parentheses following the function name surround the argument list;
  94. here
  95. .UL main
  96. is a function of no arguments, indicated by (~).
  97. The {} enclose the statements of the function.
  98. Individual statements end with a semicolon
  99. but are otherwise free-format.
  100. .PP
  101. .UL printf
  102. is a library function which will format and print
  103. output
  104. on the terminal (unless some other destination is
  105. specified).
  106. In this  case it prints
  107. .E1
  108. hello, world
  109. .E2
  110. A function is invoked by naming it,
  111. followed by a list of arguments in parentheses.
  112. There is
  113. no
  114. .UC CALL
  115. statement as in Fortran or 
  116. .UC PL/I.
  117. .NH
  118. A Working C Program; Variables; Types and Type Declarations
  119. .PP
  120. Here's a bigger program that adds three integers and prints their sum.
  121. .E1
  122. main(~) {
  123.     int a, b, c, sum;
  124.     a = 1;  b = 2;  c = 3;
  125.     sum = a + b + c;
  126.     printf("sum is %d", sum);
  127. }
  128. .E2
  129. .PP
  130. Arithmetic and the assignment statements are much
  131. the same as in Fortran (except for the semicolons)
  132. or
  133. .UC PL/I.
  134. The format of C programs is quite free.
  135. We can put several statements on a line if we want,
  136. or we can split a statement among several lines if
  137. it seems desirable. The split may be between any of the operators or variables,
  138. but
  139. .ul
  140. not
  141. in the middle of a name or operator.
  142. As a matter of style,
  143. spaces, tabs, and newlines should be used freely
  144. to enhance readability.
  145. .PP
  146. C has four 
  147. fundamental
  148. .ul
  149. types
  150. of variables:
  151. .DS
  152. \fGint\fR    integer (PDP-11: 16 bits; H6070: 36 bits; IBM360: 32 bits)
  153. \fGchar\fR    one byte character (PDP-11, IBM360: 8 bits; H6070: 9 bits)
  154. \fGfloat\fR    single-precision floating point
  155. \fGdouble\fR    double-precision floating point
  156. .DE
  157. There are also
  158. .ul
  159. arrays
  160. and
  161. .ul
  162. structures
  163. of these basic types,
  164. .ul
  165. pointers
  166. to them
  167. and
  168. .ul
  169. functions
  170. that return them,
  171. all of which we will meet shortly.
  172. .PP
  173. .ul
  174. All
  175. variables in a C program must be declared,
  176. although this can sometimes be done implicitly by context.
  177. Declarations must precede executable statements.
  178. The declaration
  179. .E1
  180. int a, b, c, sum;
  181. .E2
  182. declares
  183. .UL a,
  184. .UL b,
  185. .UL c,
  186. and
  187. .UL sum
  188. to be integers.
  189. .PP
  190. Variable names have one to eight characters, chosen from A-Z, a-z, 0-9, and \(ul,
  191. and start with a non-digit.
  192. Stylistically, it's much better to use only a single case
  193. and give functions and external variables names that are unique in the first
  194. six characters.
  195. (Function and external variable names are used by various assemblers, some of which are limited
  196. in the size and case of identifiers they can handle.)
  197. Furthermore, keywords and library functions
  198. may only be recognized in one case.
  199. .NH
  200. Constants
  201. .PP
  202. We have already seen decimal integer constants in
  203. the previous example _
  204. 1, 2, and 3.
  205. Since C is often used for system programming and bit-manipulation, octal
  206. numbers are an important part of the language.
  207. In C, any number that begins with 0
  208. (zero!)
  209. is an octal integer (and hence can't have
  210. any 8's or 9's in it).
  211. Thus 0777 is an octal constant, with decimal value 511.
  212. .PP
  213. A ``character'' is one byte
  214. (an inherently machine-dependent concept).
  215. Most often this is expressed as a 
  216. .ul
  217. character constant,
  218. which is one character enclosed in single quotes.
  219. However, it may be any quantity that fits in a byte,
  220. as in
  221. .UL flags
  222. below:
  223. .E1
  224. char quest, newline, flags;
  225. quest = '?';
  226. newline = '\\n';
  227. flags = 077;
  228. .E2
  229. .PP
  230. The sequence `\\n' is C notation for ``newline character'', which, when printed, skips
  231. the terminal to the beginning of the next line.
  232. Notice that `\\n' represents only a single character.
  233. There are several other ``escapes'' like `\\n'  for representing hard-to-get or invisible
  234. characters,
  235. such as
  236. `\\t' for tab,
  237. `\\b' for backspace,
  238. `\\0' for end of file,
  239. and
  240. `\\\\' for the backslash itself.
  241. .PP
  242. .UL float
  243. and
  244. .UL double
  245. constants
  246. are discussed in section 26.
  247. .NH
  248. Simple I/O _ getchar, putchar, printf
  249. .PP
  250. .E1
  251. main( ) {
  252.     char c;
  253.     c = getchar(~);
  254.     putchar(c);
  255. }
  256. .E2
  257. .PP
  258. .UL getchar
  259. and
  260. .UL putchar
  261. are the basic I/O library functions in C.
  262. .UL getchar
  263. fetches one character
  264. from the standard input
  265. (usually the terminal)
  266. each time it is called, and returns that character
  267. as the
  268. value of the function.
  269. When it reaches the end of whatever file it is reading,
  270. thereafter it returns the character represented by `\\0'
  271. (ascii
  272. .UC NUL,
  273. which has value zero).
  274. We will see how to use this very shortly.
  275. .PP
  276. .UL putchar
  277. puts one character out on the standard output
  278. (usually the terminal)
  279. each time it is called.
  280. So the program above
  281. reads one character and writes it back out.
  282. By itself, this isn't very interesting,
  283. but observe that if we put a loop around this,
  284. and add a test for end of file,
  285. we have a complete program for
  286. copying one file to another.
  287. .PP
  288. .UL printf
  289. is a more complicated function
  290. for producing formatted output.
  291. We will talk about only the simplest use
  292. of it.
  293. Basically,
  294. .UL printf
  295. uses its first argument as formatting information,
  296. and any successive arguments
  297. as variables to be output.
  298. Thus
  299. .E1
  300. printf ("hello, world\\n");
  301. .E2
  302. is the simplest use _
  303. the string ``hello, world\\n''
  304. is printed out.
  305. No formatting information, no variables,
  306. so the string is dumped out verbatim.
  307. The newline is necessary to put this out on a line by itself.
  308. (The construction
  309. .E1
  310. "hello, world\\n"
  311. .E2
  312. is really an array of
  313. .UL chars\*.
  314. More about this shortly.)
  315. .PP
  316. More complicated, if
  317. .UL sum
  318. is
  319. 6,
  320. .E1
  321. printf ("sum is %d\\n", sum);
  322. .E2
  323. prints
  324. .E1
  325. sum is 6
  326. .E2
  327. Within the first argument of
  328. .UL printf,
  329. the characters ``%d'' signify that the next argument
  330. in the argument list is to be printed as a
  331. base 10
  332. number.
  333. .PP
  334. Other useful formatting commands are ``%c'' to print out a single character,
  335. ``%s'' to print out an entire string,
  336. and ``%o'' to print a number as octal instead of decimal
  337. (no leading zero).
  338. For example,
  339. .E1
  340. n = 511;
  341. printf ("What is the value of %d in octal?", n);
  342. printf ("  %s! %d decimal is %o octal\\n", "Right", n, n);
  343. .E2
  344. prints
  345. .E1
  346. .fi
  347. What is the value of 511 in octal?
  348. Right! 511 decimal is 777 octal
  349. .E2
  350. Notice that there is no newline at the end of the first
  351. output line.
  352. Successive calls to
  353. .UL printf
  354. (and/or
  355. .UL putchar,
  356. for that matter)
  357. simply put out characters.
  358. No newlines are printed unless you ask for them.
  359. Similarly, on input, characters are read one at a time
  360. as you ask for them.
  361. Each line is generally terminated by a newline (\\n),
  362. but there is otherwise no concept of record.
  363.