C Programming for Absolute Beginners Author: Joseph Bouhabib Ed: At the last minute, Joe has asked me to note that all the example programs in this drawer run fine and compile fine with the PDC compiler from Fish 351 (found also on our C programming 5-pack). By coincidence we have two series of C for Beginners running this issue, one by Jason Lowe and this by Joe - each takes a different tack, so check them both out.  ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ CHAPTER THREE Sorry about the delay in getting this article out. I was so upset at Tim Strachan's description of me in the last article I couldn't think straight. I AM NOT a lawyer who'd rather be a programmer, I'm a lawyer who'd rather be an idle billionaire with plenty of free time to program. The defamation writ is on its way MR EDITOR. From the response I have had to my first two articles, I can draw the following conclusions: 1.Those of you who are trying to learn C Programming used PDC. 2.Those that are trying to use PDC have had very little success. 3. I have not used PDC before. From next time, I will make sure that any examples I give work both with DICE (Matt Dillon's shareware C environment) and PDC. As I mentioned in my second article, I will get straight into C Programming as we haven't actually done anything in the first two articles. Firstly however, I would like to correct some of the mistakes in the first two articles. The major one is that the example of the source code which I gave was incorrectly set out. It should have read as follows: /* first easy program */ #include main() { printf ("Hello World!\n"); } Further more, please ignore any thing after the words "Character Strings" in my first article as these were copied from someone else's work and I haven't bothered to make them understandable. The other thing I would like to mention is that in my second article I listed a program which is related to CROBOTS. Although in my second article I said that I would explain what this meant, I think that I will leave that for the time being, as the information contained in this article should cover all aspects of that program.  Use a Text editor The most important mistake (for those of you who may not realise the difference) is that NOT ALL WORDPROCESSORS can be used to write source code. For Dice or any other C compiler to work, it must be able to save the source code in ASCII format (i.e. without any formatting controls that are specific to the wordprocessor or the Amiga). Most wordprocessors have this capability but it's safer to use a text editor. What I hope to do by the end of this article is have you actually compile a useful (my interpretation of useful, not yours) program. Firstly, I will go through the procedure to install Dice onto a hard disk system. As I have a harddrive, this was quite easy to do using the hard disk installing program which comes with the registered version of Dice. When that program is run, it asks for a destination directory. I followed the onscreen prompts for inserting the three disks that came with the registered version of Dice, as requested. NOTE: The whole thing takes about two and half megabytes to install properly. CAUTION: Make sure (whether or not using a hard drive or floppy disk system) that your system is properly and fully backed up. The first time I installed the system I had a lot of problems, especially with my mountlist. After the files are copied over, the installation program will attempt to amend your startup-sequence. It will also replace your mountlist with an entry which will drastically affect the way your system operates. In fact, my original mountlist was 1749 bytes long, and this was replaced with a mountlist which was only 80 bytes long. I found this out the hard way. So long as your mountlist contains a null entry as follows: null: Handler = L:null-handler Stacksize = 500 Priority = 5 GlobVec = 1 # Then after installation is complete you should delete the mountlist which Dice installs and restore your own mountlist amended accordingly, to include the above lines. You then need to add the following line to your start upsequence: Execute SYS:S/Startup-sequence.DICE Dice will actually save your original startup-sequence, but will add `startup-sequence.Dice' to the S: directory. You then need to amend `startup-sequence.Dice' to include at least the following line, which for some reason is not included in the installation program. You will have to add DCC:BIN to your path. Do this by inserting the line: path add DCC:BIN Also, at the end of `startup-sequence.Dice' you should add the following line: Setenv DCCOPTS -1.3. Of course, if you are using WB2.0 you should change 1.3 to 2.0. THE COMPONENTS OF A PROGRAM All C programs share certain basic components and characteristics. All C programs consist of one or more functions, each of which contains one or more statements. In C, a function is a named subroutine that can be called by other parts of the program. Functions are the building blocks of C. A statement specifies an action to be performed by the program i.e.statements are the parts of your program that actually perform operations. Statements are contained within functions. All C statements end with a semicolon. C does not recognize the end of the line as a terminator. This means there are no constraints on the position of statements within a line. Also, you may place two or more statements on one line. The simplest form of a C function is shown here. function-name () { statement sequence; } Where function-name is the name of the function and the statement sequence may be one or more statements. Technically, a function can contain no statements, but obviously, this means it wouldn't be able to do anything. There are a number of limitations on what you can call a function. It must be comprised of only the upper- and lowercase letters of the alphabet, the digits 0-9, and the underscore (_). C is case-sensitive, which means that as "function" and "Function" are two different names. Main () A C program must have a function called "main ()". This is the function at which execution of your program begins. When your program begins running, it starts executing the statements inside the main () function, beginning with the first statement after the opening curly brace and ending when the closing curly brace is reached. The actual curly brace only exists in the source code and does not exist in the compiled version of your program, but it is helpful to think of it in this way.  Library Functions Another important component of all C programs is library functions. All C compilers supply a set of library functions which your program may use. This collection of functions is usually referred to as the C standard library. This concept will be discussed in detail later. However, before I go on, one of the most common library functions is called printf(). This function is included in the example at the beginning of this article. It is C's general purpose output function. Although printf() is quite versatile, its simplest form is printf("string-to-output"); The printf() function outputs the characters that are contained between the beginning and ending double quotes to the screen. The double quotes themselves are not displayed on the screen. In C, one or more characters enclosed between double quotes is called a string. The quoted string between printf ()'s parentheses is said to be an argument to printf(). In general, information passed to a function is called an argument. In C, calling a library function is a statement; therefore, it must end with a semicolon.  To Call a function To call a function, you specify its name followed by a parenthesized list of arguments that you will be passing to it. If the function does not require any arguments, the parenthesized list will be empty. If there is more than one argument, the arguments must be separated by commas.  The header file Another component common to most C programs is the header file. In C, information about the standard library functions is found in various files supplied with your compiler. The files all end with a .H extension. The C compiler uses the information in these files to handle the library functions properly. The "#include" directive, which again is present in our example above tells the compiler to read in another file and include it with your program. Again this is a topic to be dealt in detail later. The most commonly required header file is called STDIO.H. The directive #include "stdio.h" will include the STDIO.H file in your program. You can specify the file name in either upper-or lower case, but lowercase is the traditional method. The STDIO.H header file contains, inter alia, information related to the printf() library function. The #include directive does not end with a semicolon because it is not a C keyword that can define a statement. Instead, it is an instruction to the C compiler itself. Examples: Included with this article should be the following files: 1. hello.c 2. hello 3. another.c 4. another 5. faren.c 6. faren When you click on the icons for the files with .c extensions, all you will see is the source code. The other programs are the compiled versions. Compiling hello.c with DICE The first file, hello.c is the the program listed at the beginning of this article. It is in a form ready to compile. To try it with Dice, type at the Shell prompt: dcc hello.c -0 hello NOTE: This is assuming tha you have set up Dice correctly. This program is so simple it should compile with any c compiler you are using. The second file, hello is the actual compiled version. To see what it does, type from the Shell: hello Not very earth shattering is it? Still, we're only beginners. We'll look at something a little more interesting in a minute. But first, if you look at the source code, even though it is only six lines long, it illustrates those aspects common to all C programs. Let's examine it line by line. The first line is a comment which does not affect the source or the compiled program. The second line of the program is #include "stdio.h" It causes the file STDIO.H to be read by the C compiler and to be included with the program. This file contains information related to printf(). The second line, main () begins the main () function which all C programs must have. This is where program execution begins. After main () is an opening curly brace which marks the beginning of statements that make up the function. The next line in the program is printf ("Hello world!\n"); This is a C statement. It calls the standard library function, printf(), which causes the string to be displayed. The \n makes the program go to a new line. The program ends when the closing curly brace is encountered. The file another.c is just a tiny bit more interesting than hello.c. Run it to see what it does. If you think it's totally useless, how about changing it and running your own message from the startup-sequence. The point about another.c is that statements are executed in order beginning with the opening curly brace and ending with the closing curly brace. The program Faren Now let's try something a little more interesting and useful. The fifth program included will print out a table of Fahrenheit temperatures and their Celsius (approximately) equivalents. The formula is: C = (5/9)(F-32) Look at the source code provided. The line int lower, upper, step, fahr, celsius; contains variables. These will be the main subject of the next article. In C, all variables must be declared before they are used, usually at the beginning of the function in which they are used and before any statements. A declaration consists of a ~type" and a list of variables which have that type. In the above line, the ~int' stands for integers (ie whole numbers). the variables are pretty self explanatory, don't you think? The lines lower = 0; upper = 300; step = 20; fahr = lower; set the variables to their starting values. Note the semicolon ending each statement. The next line contains the `while" statement and its condition. The argument tests the condition in the parenthesis and if true, the statements in the curly braces are executed. I'll leave the rest of the explanation for now, because you should now be able to change this program to print a Celsius to Fahrenheit table. You should also be able to "write" your own program to change other Imperial units to their metric equivalents. Good luck! It's really not all that hard. PS The compiled programs hello and faren I've included run OK from the Shell, but for some reason give an error message when run with IconX. I haven't figured out why yet. Another runs OK. (Ed: see the reasons next issue.) Joseph Bouhabib 10 Jennings Avenue Bass Hill 2197  ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[ ]] 32 [[