home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource4 / 252_01 / chap02.txt < prev    next >
Text File  |  1990-02-15  |  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 i