C programming - For Wings

Forward:

   I must begin by saying, I myself am a beginner. And I have much still to learn. But from my struggles and my questions, I have received some very valuable information from Jolse Maginnis in many late night IRC conversations. I'm going to share in a tutorial style format what I have learned about C programming for Wings (Formerly Known as JOS).

   If you are already a C programmer, there may not be a lot you can learn from this but feel free to peruse it at your leisure. There are a few JOS specific ways of doing things, so perhaps there will be something useful after all.

   When I use the term "A Program", I refer to "A 'C' Program."

   Without further a due, lets proceed...

Table Of Contents:

The basic structure of a C program
Variables
Opening a File
Dealing with Textfiles
Strings and Arrays
Loops: For, While and Do/While
Logic: if, else, else if
CommandLine Arguments
Making your own functions();
Global vs. Local vars
How to get user input

The basic structure of a C program

   A program is written with a text editor. Any text editor will do, but the more features it has for copying/cutting/pasting text, searching and replacing, jumping straight to specific line numbers and more, the better a text editor, the easier time you have reorganizing your program and finding your way around it.

   Every C program has a Main function. Every C program is made up of Functions that Call other functions, logical branchs, and loops. Every line in C ends with a ; except for lines that are defining parts of a loop. Every function has a name, and those names can be used to "call" the function. Each function can return either somevalue, or nothing if you don't need it to return anything. If I'm ahead of you, let's look at the first example:


#include <stdlib.h>

void main() {
  printf("I Love %s", "The Commodore64");
}

   The Above is an ACTUAL program. It can be compiled, and it can be run. And all it will do is print the words "I Love The Commodore64" onto the screen. Lets look at all the parts so there is no confusion.

   A Function is a section of Code that Has a Name, You can give it some information, it can do some calculation or some transformation on that code, and it can return some information to who ever called it originally.

   The word main, is the Name of the Main Function. We know it is a function because it is followed by (). Every Program Must have 1 Function Called main. Everything in C is case sensitive, and all function names must be unique. When you create a function, you give it a name, and then other parts of the program may use that function any time they need it.

   void main() { } this is the standard format for creating a function. Everything that the function will do goes between the { and the }. The function name is before the (), and everything that will ever be passed Into the function is put in between the (). The word void at the beginning says what type of information will be returned by this function. In this case Void means the function will not return anything.

So, in the program above, inside main is the line:


  printf("I Love %s", "The Commodore64");

   If you will notice, printf is followed by ( some information and then another ). These two brackets tell us that printf is infact a Function! And the stuff in between the () is the information we are giving to that function. printf is its Name. And we end the line with a semicolon. Between the () are two sets of information, both sets are between "" and the two sets are seperated a comma. Each set of information is called an argument. So, we call the function printf() and we pass it 2 arguments. The function is specifically written to take these sorts of arguments, and it uses them to print out the text to the screen.

   But where did the printf() function come from?? On the FIRST line of the program above, is the line:

#include <stdlib.h>

   The stdlib.h is the standard library. The standard library is a file FULL of premade functions. Each with a unique name, and each capable of taking arguments and doing something with them. All we have to do is include the lib, and we can then freely make use of anything that is inside it. Can you see how powerful this is? Jolse Wrote into the stdlib the function printf, and he programmed it for printing text to the screen. Now we never ever have to worry about how text ever gets on the screen, or how the screens are shared by a multi tasking Computer, or how the text is understood by different types of video output. Even if this doesn't make sense to you, it doesn't matter. The point is, as a programmer, all we care about is that we want to put text on the screen, we don't want to have to know or think about the itty bitty technical details of HOW the text will get onto the screen.

   So, that's the first lesson. type the above program into your text editor and save the file as lesson1.c Now type lcc lesson1.c -o lesson1 if all goes well the compiler will say nothing, but if you list the directory again, you will see a file called lesson1. This is a program, and if you download it to your commodore and run it you will see the results.

Variables

   A variable is a name which you choose to have assigned to a specific type of information. You can then assign that name to a hold a peice of information, and you can later change the information. And whenever you want to use the peice of information you can use the name that represents it. An Example:

int num = 1;

   int means that the variable will always be an integar. In Wings, integars are numbers between 0 and 65535. num is the name of this variable. and its first value is set equal to 1. So, imagine we wanted to do some math. SIMPLE Math, don't worry.

int a = 1;
int b = 2;

printf("%d + %d = %d", a, b, a+b);

   We made two variables and called them a and b. a is set to 1, b is set to 2. Then we have the familiar printf() function. Now, how many arguments did we give to printf()? Each argument is seperated by a comma, so 4. printf() Means "print formatted." The first argument of printf() is a string of text to be printed out, but in that string of text there is formatting information. And the arguments Following the first, are the bits of information that get inserted into the formatted string.

   In the string there are 3 %d's, and after the string, there are 3 more arguments. The trailing arguments fall into the formatting places in the string respectively. %d means that the information it will be is a decimal number. so we print out "%d + %d = %d" All the printed string says is, "a number + another number = a number," and the next three arguments say what those numbers all are. a, b, and a+b. The values of the variables and the result of the addition fill in automatically. So if you ran the program it would print on the screen "1 + 2 = 3".


There are several types of variables:

   to name a few. All declarations in C programs must match up. For example you can't try to assign a string of characters to a variable that is supposed to only hold a number. and you can't give a function that is expecting a string of characters a long number. And so on. There is a lot more to variables, but for now, that's where we leave it. I'll talk about variables as we move into more complex things

Opening a File

   To open a file we must make a FILE variable. And then use premade file handling functions to work out magic. Most of the file handling functions are in stdio.h. So you must include it at the top of any C program that is to deal with files.


Let's open a file:
FILE * myfile;

myfile = fopen("/wings/init", "r");

   That's it. Now, the first thing we did was declare a variable name as being of the FILE type. There is a * in there, what it means is that the variable will be a pointer to a file. You must point to a file because the file is on a disk and is read in a bit at a time. There is a lot more to pointers but that's as far as we'll go with them at the moment.

   The next line we set what the variable will be equal to. As you should be able to tell, fopen is the name of a function. fopen(), which is in the stdio.h library. We must pass it 2 arguments. The first is the file, and the second is how it is to be opened. /wings/init is the standard path/filename style used on the Wings command line. For example if I were to type ned /wings/init, it would open the init file with the ned text editor. If you don't understand directories in wings, it's the same as it works in unix, and you can read a bit about it in my Article How To Use Wings. The second argument "r" means the file is being opened as Read Only. So That's it! The file is open. Now we can do stuff to it. Like read a line in from it. Reading a line in from it is what I will cover next. To close a file after you are finished with it, fclose(myfile);

Dealing with Textfiles

   So, lets say we open a file with the variable name fp, (file pointer). And now we want to read in a line from it:

FILE * fp;
char * string = NULL;
int size;

fp = fopen("textfile.txt", "r");
getline(&string, &size, fp);

printf("%s", string);

   There. Now, this program has quite a bit more in it. But it's all still in the same basic format as before. getline() is a function that takes 3 arguments. The First is the character string variable, the second is an integar variable, and the third is the filepointer variable from which the line is being read.

For Programmers who already have a good grasp of things:
The * and the & before variable names are a way of Dealing with Pointers:
If we think of a variable as being a box that contains information, the following forms can be used in C to get the information you want:
  box    What value is IN box
 &box    Address of box
  ptr    Address of box
 &ptr    Address of ptr
 *ptr    What value is IN box
  • The Box is really the Name we give a variable, like this, int box = 1;
  • To insert &box into your code, it will use the ram address location of where the box is, opposed to what value is stored at that loaction.
  • We can create a pointer that points to the Address of the variable. Like this, int ptr = &box;
  • If we want to know the ram address location of a pointer, You can use the &ptr, the same way you used &box
  • And the Real Power of the pointer is that you can get the value of what is stored at the location of the original variable, through the pointer using a *

If this is confusing right now, don't worry, I still find it confusing too.

   getline() will set size, equal to how many bytes was read in. and string will now point to the start of the string of text. The string of text will in fact be in ram somewhere, and the filepointer variable will have changed it's location from the start of the file, to where ever in the file the second line starts. So if you call getline() again, with fp as the third argument it will grab the next line, not the first one again.

   And finally, we printf() with the first argument being just a %s. This is to be substituted by a string, which is what we give it in the next argument. And the first line of the text file gets printed to the screen.


You may also read in a single character at a time.
char character;
FILE * fp;

fp = fopen("textfile.txt", "r");
character = fgetc(fp);

   Now character will contain just a single character of the textfile, and the filepointer will be only 1 character in from the start. If you were to call getline() now, it would get everything from where the file pointer is, to the end of that line. Keep reading, and we'll get into stings, and arrays.

Strings and Arrays

   We've been discussing Strings up above. However, in the strictest sense, C does not deal with strings. At least, not in the conventional sense that BASIC does. C uses strings much more the way Assembly uses strings. Definition: A string is a null terminated array of characters.

   Before we can understand that, we need to know what an array is. An array is a collection of items. You can have an array of numbers, or an array of characters, or an array of arrays. And there are a few more types too that we'll cover later.

   A single character takes up a single byte of RAM. So, we make an array of characters like this:

char characterarray[10];

   The [10] following it, says that it's an array, and it's maximum length will be 10 times the total size of what ever type of variable it is. The char at the start of the line, as we discussed before, defines the variable as a character type. and 1 character is 1 byte, so the total size of the characterarray is 10 bytes.

   Now, lets say we want to put individual letters into the array. You simply use the variable name like always, but you specify the "array index" after it. Like this:

characterarray[5] = 'C';
characterarray[8] = 'z';

   There. Now, characterarray has a capital C in the spot 5 and a small z in spot 8. And anytime we want to access those values, we use it the same way as before, but add in the index. to continue the two lines from above:

printf("This is a %c Program, Not a %c Program!!", characterarray[5], characterarray[8]);

   I'm sure you can see how the correct values are substituted in to print out the right sentence. Now. here is the most important thing. The first array index isn't 1, it's 0. And therefore the last is 9.

   Once again, C doesn't understand Strings per se. It only understands Character arrays. But, how then does printf() know where the end of the "string" is when it uses a %s? After all, what if you started reading the values of characterarray[11], characterarray[12], characterarray[13], and characterarray[14] ? Each of these are valid bytes, and correspond to values in ram. but what's in those bytes nobody knows. And we want the string to stop printing at the end of the first 10 bytes only. So, we put in a "Null Terminator." Which is simply... a zero. So watch this to understand more:

char array[10];

array[0] = 'H';
array[1] = 'E';
array[2] = 'L';
array[3] = 'L';
array[4] = 'O';
array[5] = '!'; 
array[6] = 0;  

printf("%s", array);

   What do you think will print on the screen? In printf() we tell it to print a string, and as the string argument we give it our freshly made, null terminated, character array. printf() will start at array[0] and keep reading through the array one byte at a time, until it encounters the 0 byte. at which time it will know that the string is finished, and it will stop and the rest of the program will continue on. Pretty cool eh? I thought so. :)

   But it gets better, we don't always have to treat strings just like arrays even tho that is the underlying way that C is dealing with them. Because we have the string library. Just include string.h at the top of your C program and you can use any of the string functions premade up inside it.

strcpy(array, "Hey there");

   strcpy() takes 2 arguments the first is the variable of where the start of the string will be, and the second argument is the text to be put in it. However, there is no null terminator yet. So we just add that on:

array[9] = 0;

   Done. Ok. So, now lets do something more complex. When you press return in a texteditor in wings, it actually adds a byte of the value 10. This byte value doesn't represent a visible character, it is read by the text editor to start a new line. So imagine we use getline to read in a line from a text file. It will keep reading characters from the textfile and putting them each into the next element of the array until it hits the char10, it will add the char10 to the end of the array, and then add on the zero to terminate the array. Now if you use printf() to print the string, it will Print that new line character and on your screen the cursor will physically move to the next line when it gets to that character.

   If we don't want it to do this, we must remove that newline character from the string. So lets do that:

char * array = NULL;
int size;
FILE * fp;

fp = fopen("textfile.txt", "r");
getline(&array, &size, fp);

array[strlen(array)-1]  = 0;

   Is this confusing? It's not really that confusing. The index that we use, instead of being a fixed number, is made up of a few things. strlen() is a function in the string.h library that takes a null terminated array of characters as it's argument, and it returns an integar equal to the number of characters in the array, not counting the 0 terminator at the end. but then, whatever that value is, we subtract 1 from it, which moves the array index 1 character closer to the start of the string, and we set this array element equal to 0.

   The newline character is represented by a \n By the way. So lets say it read in a string, and the string was something like this, "hello\n". strlen() tells us the whole string is 6 characters long. So we take the size, subtract 1 from it to make it 5, and say to put a 0 terminator in the array with index 5. H is index 0, e is index 1, l is index 2, the next l is index 3, the o is index 4 and then index 5 which used to be \n is replace by 0. and that newline character is gone! I hope that makes sense. if it doesn't just read it over a few times and try to actually compile it and see how it works. For now that's a good start with strings.

Loops, For, While and Do/While

   So we've learned quite a bit about C so far, except our programs just go in a straight line and don't do anything that requires repeated actions, or doing one action instead of another and so on. Lets talk about loops.

   The for loop is a loop that goes around and around until some condition is met at which time the loops is broken out of. Like this:

int i;

for (i = 0; i < 5; i++) {
  getline(&string, &size, fp);
  printf("line %d: %s", i, string);
}

   we make an int variable called i. Then we define a for loop. The for loop has 3 parts between the (), seperated by semicolons. First it sets i equal to an initial value. Then it makes a condition on i that must be true in order for the stuff between the { } to be done. And the third part is something that gets done to i everytime the loop goes through.

   In our case above, i starts as 0. in order for the loop to keep going i must be less than 5. and each time the loop goes through once, i increases by one value. So, the first time, i is 0, which IS less than 5, so it's true, and the loop starts. we getlin() from the file (previously opened of course), and then we printf() whatever value i is, so 0, then the string we got. That's all that is between the { } so it goes back to the for(). i increases to 1, and the condition is checked again. 1 is still less than 5 so it starts the loop again. it gets an other line, prints it out, and the loop continues until i is increased to 5. At which time the condition of 5 < 5 is no longer true, and the loop is skipped and the rest of the program can continue.

   That is the most standard For loop. You should read and re-read that until you understand how it works. Another example, perhaps simpler:

int i;

for(i=0; i < 10; i++) {
  printf("\n %d", i+1);
}

   i starts at 0 and goes until it's 10. The loop never gets done while i is equal to 10, because as soon as it equals 10, the condition of being Less than 10 is not true. Inside the loop we print the value of i+1. And the \n as we saw before makes each thing printed out go on it's own line. So while the first time the loop equals 0, we print out 1, and continue until i = 9 and we print out 10. The output would look like this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10

   There are other sorts of loops. The While loop works like this:

int i = 0;
while(i < 10) {
  printf("\n %d", i+1);
  i++;
}

   This while loop example has the Exact same result as the second for loop example. But basically while loops just have a condition. it is up to you to to change the values of the condition in some way to make the evaluation false. As soon as it's false, it will not continue to loop, and will move on to the next part of the program.

   a Do/While Loop works like this:

int i = 0;

do {
  printf("\n %d", i);
  i++
} while(i < 10);

   In the Do/While loop, it does the loop, BEFORE the condition has to be met. So it garauntees that the contents of the loop will be done at least once.

Logic: if, else, else if

   Our programs can now run straight through, loop and loop some more. But they can't make decisions about what to skip or what not to skip based on what values certain variables hold. This is called branching. Well known to Assembly coders. We have the if statement. The Else, and the else if statements.

   If/else/else if statements are like this:

int i = 0;
int j = 1;

if(i < j)
  printf("%d is less than %d", i, j); 
else if (i == j)
  printf("%d is equal to %d", i, j);
else
  printf("%d is less than %d", j, i); 

   Everything inside the () of if or else if are conditions. They evaluate to either true or false. If they are true, you do the first line after the if or else if line. else if's are only done when the above if is not true, and the current if IS true. and else statements are only done if all previous ifs and else ifs are not true.

   in the Example above, the first if is true. because 0 is < 1. So the line directly below the first if gets executed. Then there is an else if... it gets skipped immediatly because of the previous if being true, and finally the else is skipped aswell. Lets look at another example:

getline(&string, &size, fp);
getline(&string2, &size2, fp2);

if(!strcmp(string, string2)) {
  printf("They are the exact same!!\n");
  fclose(fp);
  fclose(fp2);
} else {
  printf("Uh Oh, The files differ. Checking the next lines... \n");
  getline(&string, &size, fp);
  getline(&string2, &size2, fp2);

  if(!strcmp(string, string2)) 
    printf("These are the exact same!!\n");
  else
    printf("Uh Oh, The files are still different.\n");
  fclose(fp);
  fclose(fp2);
}

   OK... This is a little bit more complicated. we getline 2 times. from different files and store each line in a different variable. Then we use a function called strcmp which is from the string.h lib, and compares two strings. if they are the same it returns false, if they are different it returns true.

   so we put the strcmp() inside the if() and put a ! in front of the strcmp() too. The ! just takes whatever it is... true and turns it false. or false and turns it true. So if they are the same, strcmp() returns false, which gets turned into true by the ! and the if gets done. When there is more than 1 line to be done by an if, you must surround it in { } like above. When they are same the first if is done, it prints out a line to the screen and then closes both the filepointers.

   If the two strings were Not the same, the if would be skipped and the else would be done. The Else has { } after it and inside those is another if/else pair. If and else are very powerful. and Very useful. In fact it's almost impossible to create a program that doesn't use if.

CommandLine Arguments

   We know that you can pass arguments to a function with information for the function to use. And we know that every program has a main function, that gets run everytime the program is run. But, how do we pass the main function any arguments?

   The whole program is run from the command line, so you can give it arguments on the command line! Like most programs that you think of, for example ned. If you want to open a text file with ned you type:

ned filename.txt

   filename.txt become part of the arguments passed to the main function in ned.

   To use commandline arguments you just have to assign what sort of information will be passed to the main function. The main function always has 2 arguments. The first is the number of command line arguments, and the second is an array of character arrays. Like this:

void main(int argc, char * argv[]) {

  printf("the number of arguments is %d \n", argc);
  printf("the name of this program is %s \n", argv[0]);
  printf("the first command line argument is %s \n", argv[1]);
}

   Inside the main functions () are 2 argument declarations. int argc, and char * argv[]. If you just were to type the name of the program with no command line arguments, like just ned by itself on the command line, then argc would be an int equal to 1. and argv[0] would be a string (null terminated and with no \n at the end) that is the name of the program as you typed it on the command line.

   So how can this be useful? Watch:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void main(int argc, char * argv[]) {
  FILE * fp;
  char * buf = NULL;
  int size = 0;
 
  if(argc < 2){
    printf("Usage: top filename.txt\n");
    exit(-1);
  }

  fp = fopen(argv[1], "r");
  
  if(!fp) {
    printf("The file %s could not be opened!\n", argv[1]);
    exit(-1);
  }

  printf("The Top 2 lines of %s are: \n", argv[1]);

  getline(&buf, &size, fp);
  printf("%s", buf);

  getline(&buf, &size, fp);
  printf("%s", buf);

  fclose(fp);
}

   There you have it. THAT is a Perfectly useful and Complete C program. Shall we go through it? First we include the 3 libs that we will be using functions from. Then we make the void main function with the argc and argv in it. Then inside we declare a filepointer variable, a character buffer of undetermined length, and an int variable called size. The first thing we do is check if argc is less than 2. If it is, then that means the user didn't give any arguments on the command line... just the name of the program.

   So, we assume they don't know how to use the program. We print the program usage text to the screen, and use the function exit() to quit the program. exit() is in the stdlib.h. If however the user DID supply a filename or path and filename, then it will be argv[1]. So, we try to open that file with fopen for reading. We immediatly check to see if fp is false. if it IS, then it's because fopen was unable to open the file.

   So, we print to the screen the error that fopen couldn't open the file. And we print out what the filename was it was trying open aswell, just to remind the user. and then we quit the program with exit(). But, if the file Was opened properly, then all is well, and we can keep going with the program.

   We print out out line informing the user what is happeing, and then read in a line with getline(), print it to the screen, read in a second line with getline() and print it out too. Then we close the file, and the program ends.

   Now just imagine that you could put many arguments on the command line. like "josmod -h 11000 file1.mod file2.mod file3.mod" 5 arguments. josmod just counts them up. and checks the first one to see if it's -h. if it IS -h then it treats the second arg as the desired sample rate for playback. then it counts up the remaining args and knows that each one is to be a filename. so it starts with the first tries to open it and play it... when that file is through it moves to the next arg. Tries to open it. and if at any time it fails to open one of the files then the program aborts and says it can't open the file. Fantabulous.

Making your own functions();

   There is another structural advantage to C programming that is very powerful. Making your own functions. Lots of them. As you already know a program is heavily dependant on calling functions to do things. Like printf, and strcpy, and strcmp as we saw above. There are Hundreds More. all stored in different libraries. usually grouped according to what they do. Like all the str-something() functions are in the string.h lib.

   The ideal concept of functions is that each function does a single job, and has a short yet easy to remember and descriptive name. For example, If I were to do some mathematical formula on 2 numbers over and over and over. I could make a function called mathtransform(), and I could use it all over the place. or, if I wanted to join to strings together and then remove the last character... over and over and over, then I just make a function to do that and I call it fixstring(). an example of fix string might be:

char * fixstring(char * str1, char * str2);

void main(){
  printf("%s", fixstring(" Gregory", "hey there."));
}

char * fixstring(char * str1, char * str2) {
  char bothstrings[strlen(str1) + strlen(str2) - 1];

  str2[strlen(str2)-1] = 0;
  sprintf(bothstrings, "%s%s", str2, str1);
  return(bothstrings);   
}

   Does it make sense? I shouldn't be using anything here that you don't already know, so let's just go over it to make it clear. The FIRST line is a function declaration. it defines exactly what the function will be called, what it takes and what it gives back. char * means it returns a pointer to a string of characters. fixstring is the name we use to call it. and it needs to be given two pointers to two strings of characters.

   Now inside main, we call a printf() the formatting is just to print a string, and the argument for the string to print is the function we just made. Now in this function call, we give it the two arguments it needs. And the program would jump down to where the actual function is, and execute that code.

   The function first makes a new character array variable and sets it's size equal to the length of the two strings it is going to combine. Then it strips the last character off the first string as you saw before. then sprintf() is a new function, it's in one of the libs, it does just what printf is capable of doing except instead of printing the result to the screen, it puts the result into a the character array we just made. And then the function returns the "fixed" string. and the program up above uses the return value as tho it was the actual value typed into the printf argument. and then the program ends.

   So, why didn't we just have the function printf the two strings? wouldn't that be easier than making a new character array and sprintf()ing just to return it and print it out anyway? well, if the ONLY thing you wanted to do with the result was to print it out, then yes. But there is a better way to handle it.

   If you put the printf() right into the fixstring() function, then you would never be able to use the fixstring() without having it print something out. and you might not always want to print it. you might want to fix it, and them compare it to something before printing it. It's very important that a function only does it's own job. it is not the job of "fix string" to "fix string and then print string". SO, what if you were going to need both several times? You need to fix and print several times, aswell as just fix several times. Make another function. Call it printfixedstr() like this:

void printfixedstr(char * str1, char * str2) {
  printf("%s", fixstring(str1, str2));
}

   As you can see, printfixedstr() can be quite useful, but it doesn't mean rewritting the stuff you already wrote to fix the string. You simply build off of other simpler preexisting functions. Now our printfixedstr, calls fixstring(), and fixstring() calls sprintf(). Each individual function does only it's own job, and each more complex function does something more while calling less complex functions to do the grittier work for them.

   This is how all of C programming works, and it is an extremely powerful programming concept.

   Just remember you must declare functions at the top of the file before the main function starts. And the way a function works (what it takes, what it returns) must match the way it is declared at the top. You may also use a functions return value to set a variable. like this:

void main() {
  char * mainstring; 

  mainstring = fixstring(" Gregory.", "Hello there.");
  if(!strcmp(mainstring, "Hello there Gregory."))
    printf("The statement is, %s\n", mainstring);

}

   In this instance, we make mainstring a variable, and set it equal to the return of the fixstring function so that we can use the returned value more than once.

   You'll see functions being used in very clever ways as you learn more and more about C programming.

Global vs. Local vars

   In one function you can have variables names that are the same as the variable names used in an other function. This is because if you declare a variable inside a function it is local to that function only. So, if you want to use generic variable names like buf for buffer, or str for string, or i for index. You may use them over and over, just only once per function.

   A global variable however is a variable that is declared at the top of the program before the main function. If you make a global variable, you are free to use it in every and any function without declaring it in each one. In fact, if you redeclare a variable inside a function that is already a global variable, then the global var will not be used inside that function. The local one will be used instead.

   Now, imagine you want to have something Used and printed all over the program, like a variable containing the users name. If the function is local, then to get it to the other functions that will print it, it has to be passed from one function to the next. So if main calls a function, and that calls a subfunction and that calls another subfunction, and this one wants to print out that variable, the variable would need to be passed from one to the next to the next to the next. This is just a waste.

   So instead you make a global variable called name, or whatever is appropriate. Then no matter how many functions are nested down into eachother, any one of them can just printf("%s", name); and it will use the global var, and do what you expect it to.

   Global and Local variables each have their place. As a rule if the variable is only going to be used in that function, or perhaps get passed to one level down, then keep them local. And if it is a variable that will be used in many places, and called from all sorts of different functions, then make it a global.

How to get user input

   Every programmer needs to know how to get user input. To ask a question, or get a name or anything else like that.

   We can just use getline, with the filepointer being standard in. Standard in is the keyboard. Like this:

char name[15];

printf("What is your name?\n");
getline(&buf, &size, stdin);
buf[strlen(buf)-1] = 0;

strcpy(name, buf);

printf("Your name is %s\n", name);

   So we made an array of 15 characters to hold the name... We might make this a global variable in a real program. Then we print to the screen What is your name? Then we getline() where it is trying to get a line from the keyboard. It will get all of the characters you type in until you press return (as that would signify the end of a line). Then the name itself really shouldn't have that \n at the end of it, so we strip it away. Then we use strcpy() to copy contents of one variable into another. So we transfer the characters from buf into the name array we made.

   Then we print out a formatted string using that variable, and the rest of the program is free to continue, and use the name you gave it anytime.

   Alternatively you could use getchar(stdin); for yes and no, or single character options. However you have to press enter after typing the one character. and any characters after the 1st and before the return are just ignored. Then you can do if/else statements on the input from a getchar(), like:

if('y' == getchar(stdin))
  printf("you Chose yes\n");
else
  printf("you didn't choose yes ... So you get no by default\n");


WELL, That Brings Us to the End Of the First C programming tutorial For Wings. I realize it may not be complete, but I hope it gives you an idea of what you can Do, and how to start programming in C for Wings. Check the Site for a new file that will be added Soon. More C Programming in Wings. To talk about more advanced things. How to connect to Internet Servers. How to change the settings of the console. How to change colors in the console. How to Make Programs for the GUI. How to spawn other programs from within your program. How to make structures for more advanced Data handling. How to get and Use the Current Internet IP address in your Wings Internet Programs. How to Use Application Folders for more advanced application organization. And more! :) I hope you can use this stuff btw. If you want, drop me an email.