home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.tar / ftp.whtech.com / Geneve / 9640news / CAT12 / CTUTORL.ARK < prev    next >
Text File  |  2006-10-19  |  21KB  |  400 lines

  1. ?
  2.  
  3.  
  4. ---  c99  ---
  5. A Tutorial Introduction for Dartmouth TIBBS;
  6. by  Scott Flinn
  7. _______________
  8.  
  9.      The 'C' programming language, like many I have had the occasion to use in
  10. anything less than a trivial way, is very subtle in its appeal, to the extent
  11. that I am now completely incapable of explaining what it is about the language
  12. that attracts me so strongly to it.  My first dabblings in C led to a well
  13. developed monolog on its indispensable attributes, and a full week of work
  14. completed my metamorphosis into a veritable C freak, destroying forever any
  15. chance I may have had to explain in a rational way why it might be a good idea
  16. to have a look at it.  For this reason, I will make no attempt to convince you
  17. that you are, in fact, in much greater danger of having disaster befall you if
  18. you choose to ignore c99.  This is quite probably a fact, but I will still make
  19. no attempt to convince you of it.  Instead, I will make the assumption that,
  20. since you have persisted to this point through a tedious paragraph of turgid
  21. rubbish, you have already found sufficient reason to give the language some
  22. attention and probably already realize that it is the only realistic
  23. alternative to Forth for those desiring more speed or power than Basic can
  24. offer.
  25.  
  26.      This tutorial will make a typical assumption, and so begins with the
  27. standard disclaimer that it makes no effort to introduce proper programming
  28. technique or methodology.  It is assumed from this point on that the reader is
  29. familiar with TI Basic at least to the extent that useful (though perhaps
  30. simple) programs can be produced.  The similarities (as well as some of the
  31. outrageous differences) will be pointed out, and a simple method for producing
  32. equally simple programs will be presented.  This first tutorial article is
  33. geared toward the basic mechanics of writing, compiling, loading, and running a
  34. c99 program on the TI-99/4A.  Not in this article, but to be included in later
  35. tutorials will be discussions on function use, character strings, and some of
  36. the more useful features of c99 not found in Basic.
  37.  
  38.      For those uninitiated to compiler languages, the sight of a C program
  39. listing will make Basic look extremely appealing.  The first task, which many
  40. find to be the most formidable, is that of realizing that the strange
  41. appearance of a C program, and the seemingly inhuman procedure required to
  42. produce one are really just elaborate illusions generated by a small handful of
  43. arbitrary rules who, not wanting to be broken and violated, make a valiant
  44. attempt to drive away the faint at heart.  You must see these rules for what
  45. they are - selfish, inconsiderate, but very harmless and almost trivial to
  46. master.  Let's start with the most unusual of these.
  47.  
  48.      There are four aspects of any C program which are not present in
  49. Basic.  Namely: 1) declared variables
  50.                 2) program and function names
  51.                 3) semicolons to end statements
  52.                 4) source files vs. compiled files
  53.  
  54.      Without a doubt, the most despised aspect of compiled languages is the
  55. need to declare all variables used in the program before they are actually
  56. referenced in that program.  The last straw for many people is that the place
  57. in the program in which these variables are declared determines what parts of
  58. the program can refer to them (ie. use them).  The first hurdle to get over is
  59. to notice that variables used in C programs are no different than those used in
  60. Basic.  If you have been using the variable A for example, and want to add 1 to
  61. it, you would use A = A + 1.  (There are actually betters ways of doing this in
  62. C, but A = A + 1 is FINE for now).  The only difference is that before you can
  63. do this, or anything involving the variable A for that matter (eg. A = 6, or B
  64. = A / 2), you must tell the program that you want to be able to use that
  65. variable.  Telling the program about variables that you want to be able to use
  66. it what is meant by 'variable declarations'.  Since this is the most hated
  67. aspect of compiled languages, particularly by those new to them, I thought I
  68. should at least mention it here, to give you plenty of warning.  I will return
  69. to them later, but first you must know how to write programs of any kind,
  70. preferably starting with those that don't use things as complicated as
  71. variables.
  72.  
  73.      Following is a listing of your very first c99 program.  If you have
  74. already successfully written programs in c99, you may skip this part - you have
  75. my permission to do that! If not, then this is a great starting point:
  76.  
  77. main()
  78.  
  79.  { puts("Hello world\n\n");
  80.  }
  81.  
  82.      That's it...that's all you need.  Type it in EXACTLY like that and it will
  83. work.  Type it in where? Ah...well, let's begin with a detailed description of
  84. the steps required t{_o write, compile, load, and run a c99 program.  Once you
  85. have successfully run the program above, then you will know how to run any c99
  86. program, and a knowledge of c99 will then be the last step.
  87.  
  88.      The program source code (ie. the stuff above that begins with 'main()' and
  89. ends with '}') can be typed in using any program that produces a D/V 80 text
  90. file.  The best one to use is the Editor/Assembler (henceforth to be abreviated
  91. E/A) editor.  The TI Writer editor is also fine, as long as the file is saved
  92. using the PRINT option rather than SAVE (to avoid a line of tab data being
  93. saved at the end).  Now, once you have the above text typed into a file, you
  94. are ready to start compiling.  The compiler is called 'C99C' and also requires
  95. the files 'C99D' and 'C99E'.  This can be loaded either from E/A Option 5, or
  96. from any E/A simulator (such as Funlwriter).  When the program is loaded it
  97. will begin execution automatically, and will start asking questions
  98. immediately.  DO NOT PANIC.  Depending on the version of the compiler you have,
  99. there are a number of different questions you can be asked.  Following is a
  100. list of possibilities along with the correct responses for the above program:
  101.  
  102.  1)  Include c-text?             N or n
  103.  2)  Inline push code?           N or n
  104.  3)  Main program?               Y or y  (only in older versions of c99)
  105.  4)  Input filename?             DSKn.whatever-you-called-the-program-file
  106.  5)  Output filename?            DSKn.a-new-filename
  107.  
  108.      And at this point compilation will begin.  BE SURE to put the disk with
  109. the program on it back in the drive if you removed it to load the compiler.
  110. Since it is a very short program, the compilation should be fairly quick.  If
  111. you have not properly entered the program, then (depending on the version of
  112. c99 you are using) it will either tell you when it has finished that it has
  113. found errors, or it will show you the errors as it encounters them,}i and will
  114. wait for you to hit the spacebar to continue.  In either case, get out of the
  115. compiler in any way you like and find the discrepancy between your program file
  116. and the program listed above.  Then try it again.
  117.  
  118.      If compilation is successful, however, the message 'COMPILATION COMPLETE'
  119. will be issued, and you will be asked if you want to RERUN? Respond with N or
  120. n, and you will be returned to either E/A or the color bar screen.  You have
  121. now compiled your program! (You are now roughly half done!) The next step is to
  122. assemble your compiled program.  The output file that you specified to the
  123. compiler should now contain the compiler's output - namely a TMS9900 assembly
  124. program that can be assembled with the TI assembler (either with E/A or
  125. Funlwriter).  To do this, load the assembler (by selecting the assembler option
  126. in either case).  The input file for the assembler is the OUTPUT file from the
  127. c99 compiler.  The new output file is any, as yet unused filename.  After
  128. providing this information to the compiler program, you will be asked for a
  129. list filename.  Respond with a carriage return.  You will then be prompted to
  130. specify some options.  Again respond with a carriage return, and assembly
  131. should commence.  Again, if errors are encountered, you will be informed, and
  132. you should start again.  If not, you will now have a program that can be run
  133. and are ready for the last step (exciting, isn't it).  Exit the assembler.  At
  134. this point, I do not know how to avoid using either E/A or Mini Memory.  If you
  135. have one of these cartridges, however, then select the 'LOAD AND RUN' option.
  136. The first file you must load is CSUP (which is on the c99 release diskette).
  137. This file must ALWAYS be loaded before c99 programs will run.  Next load the
  138. file produced by the assembler (the one you specified for output).  Pressing
  139. enter will produce the 'PROGRAM NAME' prompt to which you must respond START.
  140. Sit back and watch the fun.  If the words 'Hello world' do not appear on your
  141. screen, carefully retrace your steps.
  142.  
  143.      Congratulations.  You have just plowed you way through the exceedingly
  144. bizarre sequence of steps required to compile and run c99 programs.  Now do it
  145. again.  That's right, do it AGAIN! It's the only way you'll get the hang of it.
  146. Try changing the message in quotation marks next time (eg. "Hello Fred\n\n"
  147. might be nice).  In particular, see if you can figure out what the characters
  148. '\n' in the message do.  Add a couple more \n's - try taking them all out.  The
  149. important part is that you feel comfortable with the compilation steps.  To
  150. make it a little easier, here is a very brief summary of the steps that must be
  151. followed.  First, however, an IMPORTANT word about file names.  With C, you
  152. tend to use a lot of different files, and it is sometimes hard to remember all
  153. the names you use.  A very good suggestion (if you don't mind me saying so) is
  154. to pick ONE name for all of the steps (eg. FRED might be nice), and distinguish
  155. between the different intermediate steps using the last two characters.  Like
  156. so:
  157.  
  158.         DSK1.FRED;C           ;C  indicates that this file contains the c99
  159.                                   program
  160.  
  161.         DSK1.FRED;A           ;A  indicates that this is the output of the
  162.                                   compiler.  It is the file that you must
  163.                                   now assemble, hence the A
  164.  
  165.         DSK1.FRED;R           ;R  indicates that this file is ready to (load
  166.                                   and) run
  167.  
  168. Now for the advertised list of compilation steps:
  169.  
  170.  1)  Write or modify the program with a D/V 80 editor
  171.  2)  Save the file, and load the c99 compiler
  172.  3)  Answer the questions as described above (or as described in the
  173.      c99 user's manual, which is really very good and is something you
  174.      should probably read)
  175.  4)  input filename  = file;C
  176.      output filename = file;A
  177.  5)  Load the assembler, and assemble file;A to produce file;R - no list file,
  178.      no options
  179.  6)  Load CSUP from LOAD AND RUN option
  180.  7)  Load file;R and push enter
  181.  8)  Respond to PROGRAM NAME with START
  182.  9)  Observe results.  If not as desired, curse vigorously.
  183.  
  184.      If you have made it this far, then the fourth problem in the list above
  185. has been solved.  We still have three to go, so let's tackle them bodily.  The
  186. next thing to consider is the program name 'main'.  In C, every program must
  187. have this name, which must be followed by (), indicating that there are no
  188. parameters to be passed to it.  Actually, most C programs eventually have many
  189. distinct blocks of program statements, each of which is labeled.  Execution of
  190. the program begins with the block labeled 'main()', so it must always be
  191. present.  The idea of separate blocks of statements in C is identical to that
  192. of user defined subprograms in Extended Basic.  If a block of lines is to be
  193. used more than once, it is often convenient to put these together into a
  194. subroutine (called 'functions' in C) which can be called simply by using its
  195. name with the appropriate parameters.  The idea of functions has been
  196. introduced here at the outset because, while it is possible to write programs
  197. without functions, it is deemed VERY bad programming practice.  It is therefore
  198. a very good idea to get over the shock of having to use them before you lose
  199. your nerve.
  200.  
  201.      The last problem to be introduced is that, rather arbitrarily, every
  202. single statement must end in a semicolon.  This actually makes the compiler's
  203. task easier and, in many cases, makes the resulting C code easier to read
  204. (believe it or not!) For those of you familiar with Pascal, the semicolon in C
  205. is actually part of the statement, not merely a statement separator.  So, for
  206. example, the statement immediately before an 'else' statement must have a
  207. semicolon just as every other line must.
  208.  
  209.      The last part of this extremely brief introduction will be the much
  210. dreaded return to the topic of variable declarations.  In c99, there are two
  211. different data types corresponding very roughly to the types numeric and string
  212. in Basic. Specifically, c99 uses integer or character variables.  An integer
  213. variable can hold any integer in the range -32768 to 32767.  c99 does not
  214. support floating point numbers directly, so the range can be no bigger than
  215. this.  In addition, there is no way to represent fractional parts.  So the
  216. statement A = 1/2 will set A to 0, since 0.5 cannot be represented in integer
  217. form.  Similarly, A = 5/3 would give A the value of 1, which is the integer
  218. part of 5/3 = 1.667. Character variables are very nearly the same, although
  219. they serve an entirely different purpose.  For the technically inclined,
  220. integer variables are 16 bit and character variables are 8 bit (for the even
  221. more technically inclined, all arithmetic involving BOTH types of data are done
  222. using 16 bit operands, all the character data being extended logically to 16
  223. bits before being combined.  So mixing the data types is usually legal,
  224. although very much frowned upon).
  225.  
  226.      An additional word must be said about character variables.  These
  227. variables are rarely used alone since most functions almost always deal with
  228. integer data. The most common use for character variables is in arrays.  C has
  229. no string manipulation facilities.  Unlike basic, it is not possible to have a
  230. string variable that can be easily manipulated; instead, arrays of characters
  231. must be used, and string manipulations involve manipulating each character
  232. variable in the array.  Actually, the manipulation of string data (ie. arrays
  233. or characters involves one of the trickier aspects of C and should be left
  234. until some of the other aspects are mastered.  DO NOT let this deter you.  The
  235. next tutorial article in this series will contain a complete description of
  236. character strings, the various ways of representing and manipulating them, and
  237. several very useful functions which perform more common manipulations on them.
  238. In the meantime, a small number of examples of slightly easier uses of strings
  239. can be found in the examples which follow.
  240.  
  241.      So what about the actual declaration of these things...I've been stalling,
  242. haven't I? Well, it is actually pretty easy.  Any block of program lines
  243. enclosed within {braces} is treated as a single statement, making it possible
  244. to do a large number of things in IF-THEN-ELSE statements, for example.  In
  245. particular, the main program is a block of statements which must be separated
  246. from other functions.  Surrounding all of the lines in 'main()' with {} ensures
  247. that it will be treated as one block before any other lines in the program file
  248. are considered.  Now, any variables used with a block of lines surrounded by {}
  249. must be declared immediately following the opening brace {.  If the variable is
  250. going to be used to store integer data, then it must be declared using the
  251. statement 'int var-name', and must then be restricted to integer data.
  252. Similarly, character variables used withing a block must be declared as 'char
  253. var-name'.  Note that, unlike string variables in Basic, no $ is required for
  254. character variable names.  In fact, there is no distinction at all between
  255. integer and variable names.
  256.  
  257.      Now for the tricky part.  A variable can only be used in the block of
  258. lines in which it has been declared, which includes blocks of lines within that
  259. block. The usual practice is to declare all of the variables used in a
  260. function, including the main program function 'main()', immediately after the
  261. opening brace for that function.  They can then be referenced from anywhere
  262. within that function.
  263.  
  264.      That is more than enough for now.  The next thing on the agenda is LOTS of
  265. practice.  The following programs illustrate most of the things outlined above.
  266. Try compiling them.  Make small changes to see what happens.  Do whatever you
  267. like with them, but whatever else, do compile and run them.  A few hours spent
  268. now in frustration will pay endless rewards through the truly unbelievable
  269. power that you will 6quickly become easier than Basic ever was.
  270.  
  271.      The next section, incidentally, contains lots of exciting information about
  272. functions and things.  This is where the power of C really begins to show.  Even
  273. if you intend to stop here for now, you should at least look at the sample
  274. program at the start of the next section to see what you are missing.
  275.  
  276.  
  277.  
  278.  
  279. SAMPLE PROGRAMS:
  280.  
  281. /******************************************************************************/
  282.  
  283. /* comments are a very important part of C programs, much more than in basic
  284.    in C, when a slash-asterisk pair is encountered, everything that follows is
  285.    interpreted as a comment until an asterisk-slash pair is found             */
  286.  
  287. main()  /* begins EVERY c99 program */
  288.  
  289.  { int  i;    /* i is a variable we want to use.  Note the semicolon */
  290.  
  291.    for (i=32; i<= 126; i=i+1)  /* the for statement is just a bit different */
  292.     { putchar(i);  /* puts character whose ascii value is i on the screen */
  293.       putchar(10); /* ASCII 10 is a linefeed - this skips to a new line */
  294.     } /* this ends the block of lines executed every time the for loop repeats
  295.          you can think of it as replacing the NEXT from Basic */
  296.  }
  297.  
  298. /******************************************************************************/
  299.  
  300. /* the same program without the comments is, for clarity: */
  301.  
  302. main()
  303.  
  304.  { int i;
  305.  
  306.    for (i = 32; i <= 126; i = i + 1)
  307.     { putchar(i);
  308.       putchar(10);
  309.     }
  310.  }
  311.  
  312. /* fiddle with the limits in the for statement and see what happens */
  313.  
  314. /******************************************************************************/
  315.  
  316. main()
  317.  
  318.  { int  i;
  319.    char *c, *d;
  320.  
  321.    c = "string";
  322.    d = c;
  323.    for (i = 0; i < 6; i++)  /* i++ means i=i+1 */
  324.     { puts("Character ");
  325.       putchar(i+48);
  326.       puts(" of the string is ");
  327.       putchar(*d++);
  328.       putchar(10);
  329.     }
  330.    puts("\n\nThe whole string is ");
  331.    puts(c);
  332.  }
  333.  
  334. /******************************************************************************/
  335.  
  336. extern printf(); /* printf is an external function, and requires this before it
  337.                     can be referenced.  Don't worry about it.  Just remember,
  338.                     any program that uses printf must have this statement in it.
  339.                     Also, the file PRINTF from the c99 release diskette must be
  340.                     loaded along with CSUP and your own file */
  341.  
  342. main()
  343.  
  344.  { int  i;
  345.  
  346.    for (i = 0; i < 100; i++)
  347.       prizTCHsquared is %d\n", i, i*i);
  348.  }
  349.  
  350. /* play with printf, particularly the %d things, and see if you can figure it
  351.    out */
  352.  
  353. /******************************************************************************/
  354.  
  355.     This next program is a good deal more complicated and should give you
  356. something to chew on until I get around to writing the next tutorial.  A
  357. reference book on C (such as "The C Programming Language" by Kernighan and
  358. Ritchie, available at the Dalhousie Book Store) is highly recommended.  In
  359. addition, if you understand the programs above, a thorough reading of the c99
  360. user's manual would now be a good idea.
  361.  
  362.  
  363. extern printf();
  364.  
  365. #define FALSE  0     /* all occurrences of FALSE are replaced with  0 */
  366. #define TRUE  -1     /* all occurrences of TRUE  are replaced with -1 */
  367.  
  368. main()
  369.  
  370.  { int n, test, prime, key;
  371.  
  372.    tscrn(2, 8); /* change screen color */
  373.    locate(1, 15);
  374.    puts("Factor Finder");
  375.  
  376.    for (n = 1; n < 32767; n = n + 2)
  377.     { prime = TRUE;  /* see #define above */
  378.       test = 2;
  379.       locate(8, 4);
  380.       printf("Testing %d ->", n);
  381.       while ((test < n/2) & (prime == TRUE))
  382.        { key = poll(1);
  383.          if ((n % test) == 0)   /* n % test is remainder when test divides n */
  384.           { prime = FALSE;
  385.             locate(10, 5);
  386.             printf("%6d divides %6d", test, n);
  387.           }
  388.          else
  389.             test++;  /* same as test = test + 1 */
  390.        }
  391.     }
  392.  }
  393.  
  394. /******************************************************************************/
  395. )3=GUUUUUUUU
  396.  
  397. Download complete.  Turn off Capture File.
  398.  
  399.  
  400.