home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctutor1.zip / CHAP02.TXT < prev    next >
Text File  |  1989-11-10  |  14KB  |  349 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                     Chapter 2
  6.                                          GETTING STARTED IN C
  7.  
  8.  
  9. YOUR FIRST C PROGRAM
  10. ____________________________________________________________
  11.  
  12. The best way to get started with C is to        =============
  13. actually study a program, so load the file        TRIVIAL.C
  14. named TRIVIAL.C and display it on the           =============
  15. monitor.  You are looking at the simplest
  16. possible C program.  There is no way to
  17. simplify this program or to leave anything out. 
  18. Unfortunately, the program doesn't do anything.
  19.  
  20. The word main is very important, and must appear once, and
  21. only once in every C program.  This is the point where
  22. execution is begun when the program is run.  We will see later
  23. that this does not have to be the first statement in the
  24. program but it must exist as the entry point.  Following the
  25. main program name is a pair of parentheses which are an
  26. indication to the compiler that this is a function.  We will
  27. cover exactly what a function is in due time.  For now, I
  28. suggest that you simply include the pair of parentheses. 
  29.  
  30. The two curly brackets, properly called braces, are used to
  31. define the limits of the program itself.  The actual program
  32. statements go between the two braces and in this case, there
  33. are no statements because the program does absolutely nothing. 
  34. You can compile and run this program, but since it has no
  35. executable statements, it does nothing.  Keep in mind however,
  36. that it is a valid C program.
  37.  
  38.  
  39. A PROGRAM THAT DOES SOMETHING
  40. ____________________________________________________________
  41.  
  42. For a much more interesting program, load the   =============
  43. program named WRTSOME.C and display it on         WRTSOME.C
  44. your monitor.  It is the same as the previous   =============
  45. program except that it has one executable
  46. statement between the braces.
  47.  
  48. The executable statement is a call to a function supplied as
  49. a part of your C library.  Once again, we will not worry about
  50. what a function is, but only how to use this one named
  51. printf().  In order to output text to the monitor, the desired
  52. text is put within the function parentheses and bounded by
  53. quotation marks.  The end result is that whatever is included
  54. between the quotation marks will be displayed on the monitor
  55. when the program is run. 
  56.  
  57. Notice the semi-colon at the end of the line.  C uses a
  58.  
  59.                                                      Page 2-1
  60.  
  61.                              Chapter 2 - Getting Started in C
  62.  
  63. semi-colon as a statement terminator, so the semi-colon is
  64. required as a signal to the compiler that this line is
  65. complete.  This program is also executable, so you can compile
  66. and run it to see if it does what you think it should.
  67.  
  68.  
  69. ANOTHER PROGRAM WITH MORE OUTPUT
  70. ____________________________________________________________
  71.  
  72. Load the program WRTMORE.C and display it on    =============
  73. your monitor for an example of more output        WRTMORE.C
  74. and another small but important concept.  You   =============
  75. will see that there are four program
  76. statements in this program, each one being a
  77. call to the function printf().  The top line will be executed
  78. first, then the next, and so on, until the fourth line is
  79. complete.  The statements are executed in order from top to
  80. bottom.
  81.  
  82. Notice the funny character near the end of the first line,
  83. namely the backslash.  The backslash is used in the printf()
  84. statement to indicate that a special control character is
  85. following.  In this case, the "n" indicates that a newline is
  86. requested.  This is an indication to return the cursor to the
  87. left side of the monitor and move down one line.  It is
  88. commonly referred to as a carriage return/line feed.  Any
  89. place within text that you desire, you can put a newline
  90. character and start a new line.  You could even put it in the
  91. middle of a word and split the word between two lines.  The
  92. C compiler considers the combination of the backslash and
  93. letter n as one character.
  94.  
  95. A complete description of this program is now possible.  The
  96. first printf() outputs a line of text and returns the
  97. carriage.  The second printf() outputs a line but does not
  98. return the carriage so that the third line is appended to the
  99. second, then followed by two carriage returns, resulting in
  100. a blank line.  Finally the fourth printf() outputs a line
  101. followed by a carriage return and the program is complete.
  102.  
  103.  
  104. After compiling and executing WRTMORE.C, the following text
  105. should be displayed on your monitor;
  106.  
  107. This is a line of text to output.
  108. And this is another line of text.
  109.  
  110. This is a third line.
  111.  
  112. Compile and run this program to see if it gives you this
  113. output.  It would be a good idea at this time for you to
  114. experiment by adding additional lines of printout to see if
  115. you understand how the statements really work.
  116.  
  117.                                                      Page 2-2
  118.  
  119.                              Chapter 2 - Getting Started in C
  120.  
  121. LET'S PRINT SOME NUMBERS
  122. ____________________________________________________________
  123.  
  124. Load the file named ONEINT.C and display it    ==============
  125. on the monitor for our first example of how       ONEINT.C
  126. to work with data in a C program.  The entry   ==============
  127. point main should be clear to you by now as
  128. well as the beginning brace.  The first new
  129. thing we encounter is line 3 containing int index;, which is
  130. used to define an integer variable named index.  The word int
  131. is a keyword in C, and can not be used for anything else.  It
  132. defines a variable that can have a value from -32768 to 32767
  133. in most C compilers for microcomputers.  The variable name,
  134. index, can be any name that follows the rules for an
  135. identifier and is not one of the keywords for C.  The final
  136. character on the line, the semi-colon, is the statement
  137. terminator.
  138.  
  139. Note that, even though we have defined a variable, we have not
  140. yet assigned a value to it.  We will see in a later chapter
  141. that additional integers could also be defined on the same
  142. line, but we will not complicate the present situation. 
  143.  
  144. Observing the main body of the program, you will notice that
  145. there are three statements that assign a value to the variable
  146. index, but only one at a time.  The statement in line 4
  147. assigns the value of 13 to index, and its value is printed out
  148. by line 5.  (We will see how shortly.)  Later, the value of
  149. 27 is assigned to index, and finally 10 is assigned to it,
  150. each value being printed out.  It should be intuitively clear
  151. that index is indeed a variable and can store many different
  152. values but only one value at a time of course.
  153. Please note that many times the words "printed out" are used
  154. to mean "displayed on the monitor".  You will find that in
  155. many cases experienced programmers take this liberty, probably
  156. due to the printf() function being used for monitor display.
  157.  
  158.  
  159. HOW DO WE PRINT NUMBERS?
  160. ____________________________________________________________
  161.  
  162. To keep our promise, let's return to the printf() statements
  163. for a definition of how they work.  Notice that they are all
  164. identical and that they all begin just like the printf()
  165. statements we have seen before.  The first difference occurs
  166. when we come to the % character.  This is a special character
  167. that signals the output routine to stop copying characters to
  168. the output and do something different, namely output the value
  169. of a variable.   The % sign is used to signal the output of
  170. many different types of variables, but we will restrict
  171. ourselves to only one for this example.  The character
  172. following the % sign is a d, which signals the output routine
  173. to get a decimal value and output it.  Where the decimal value
  174.  
  175.                                                      Page 2-3
  176.  
  177.                              Chapter 2 - Getting Started in C
  178.  
  179. comes from will be covered shortly.  After the d, we find the
  180. familiar \n, which is a signal to return the video "carriage",
  181. and the closing quotation mark.
  182.  
  183. All of the characters between the quotation marks define the
  184. pattern of data to be output by this statement, and after the
  185. pattern, there is a comma followed by the variable name index. 
  186. This is where the printf() statement gets the decimal value
  187. which it will output because of the %d we saw earlier.  We
  188. could add more %d output field descriptors within the brackets
  189. and more variables following the description to cause more
  190. data to be printed with one statement.  Keep in mind however,
  191. that the number of field descriptors and the number of
  192. variable definitions must be the same or the runtime system
  193. will get confused and probably quit with a runtime error.
  194.  
  195. Much more will be covered at a later time on all aspects of
  196. input and output formatting.  A reasonably good grasp of these
  197. fundamentals are necessary in order to understand the
  198. following lessons.  It is not necessary to understand
  199. everything about output formatting at this time, only a fair
  200. understanding of the basics.
  201.  
  202. Compile and run ONEINT.C and observe the output.
  203.  
  204.  
  205. HOW DO WE ADD COMMENTS IN C?
  206. ____________________________________________________________
  207.  
  208. Load the file named COMMENTS.C and observe it  ==============
  209. on your monitor for an example of how            COMMENTS.C
  210. comments can be added to a C program.          ==============
  211. Comments are added to make a program more
  212. readable to you but the compiler must ignore
  213. the comments.  The slash star combination is used in C for
  214. comment delimiters.  They are illustrated in the program at
  215. hand.  Please note that the program does not illustrate good
  216. commenting practice, but is intended to illustrate where
  217. comments can go in a program.  It is a very sloppy looking
  218. program.
  219.  
  220. The first slash star combination introduces the first comment
  221. and the star slash at the end of the first line terminates
  222. this comment.  Note that this comment is prior to the
  223. beginning of the program illustrating that a comment can
  224. precede the program itself.  Good programming practice would
  225. include a comment prior to the program with a short
  226. introductory description of the program.  The next comment is
  227. after the main program entry point and prior to the opening
  228. brace for the program code itself.
  229.  
  230. The third comment starts after the first executable statement
  231. in line 5 and continues for four lines.  This is perfectly
  232. legal because a comment can continue for as many lines as
  233.  
  234.                                                      Page 2-4
  235.  
  236.                              Chapter 2 - Getting Started in C
  237.  
  238. desired until it is terminated.  Note carefully that if
  239. anything were included in the blank spaces to the left of the
  240. three continuation lines of the comment, it would be part of
  241. the comment and would not be compiled.  The last comment is
  242. located following the completion of the program, illustrating
  243. that comments can go nearly anywhere in a C program. 
  244.  
  245. Experiment with this program by adding comments in other
  246. places to see what will happen.  Comment out one of the
  247. printf() statements by putting comment delimiters both before
  248. and after it and see that it does not get executed causing a
  249. line of printout.
  250.  
  251. Comments are very important in any programming language
  252. because you will soon forget what you did and why you did it. 
  253. It will be much easier to modify or fix a well commented
  254. program a year from now than one with few or no comments. 
  255. You will very quickly develop your own personal style of
  256. commenting.
  257.  
  258. Some C compilers will allow you to "nest" comments which can
  259. be very handy if you need to "comment out" a section of code
  260. during debugging.  Since nested comments are not a part of the
  261. ANSI standard, none will be used in this tutorial.  Check the
  262. documentation for your compiler to see if they are permitted
  263. with your implementation of C. 
  264.  
  265.  
  266. GOOD FORMATTING STYLE
  267. ____________________________________________________________
  268.  
  269. Load the file GOODFORM.C and observe it on     ==============
  270. your monitor.  It is an example of a well        GOODFORM.C
  271. formatted program.  Even though it is very     ==============
  272. short and therefore does very little, it is
  273. very easy to see at a glance what it does. 
  274. With the experience you have already gained in this tutorial,
  275. you should be able to very quickly grasp the meaning of the
  276. program in it's entirety.  Your C compiler ignores all extra
  277. spaces and all carriage returns giving you considerable
  278. freedom in formatting your program.  Indenting and adding
  279. spaces is entirely up to you and is a matter of personal
  280. taste.  Compile and run the program to see if it does what
  281. you expect it to do.
  282.  
  283. Now load and display the program UGLYFORM.C    ==============
  284. and observe it.  How long will it take you to    UGLYFORM.C
  285. figure out what this program will do?  It      ==============
  286. doesn't matter to the compiler which format
  287. style you use, but it will matter to you when
  288. you try to debug your program.  Compile this program and run
  289. it.  You may be surprised to find that it is the same program
  290. as the last one, except for the formatting.  Don't get too
  291. worried about formatting style yet.  You will have plenty of
  292.  
  293.                                                      Page 2-5
  294.  
  295.                              Chapter 2 - Getting Started in C
  296.  
  297. time to develop a style of your own as you learn the language. 
  298. Be observant of styles as you see C programs in magazines,
  299. books, and other publications. 
  300.  
  301. This should pretty well cover the basic concepts of
  302. programming in C, but as there are many other things to learn,
  303. we will forge ahead to additional program structure.
  304.  
  305.  
  306. PROGRAMMING EXERCISES
  307. ____________________________________________________________
  308.  
  309. 1.   Write a program to display your name on the monitor.
  310.  
  311. 2.   Modify the program to display your address and phone
  312.      number on separate lines by adding two additional
  313.      printf() statements.
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.                                                      Page 2-6
  349.