home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP2.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  15.3 KB  |  323 lines

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