Site hosted by Angelfire.com: Build your free website today!
Programming in C

To Reader: This tutorial is not meant to teach you the entire programming language. I have yet to even find a book that teaches you the entire language. Do not look at this tutorial as a resource for you to study off of. Go out and get some books from the library. Also, I am aware that there are typos in this tutorial. If you spot some, go ahead and e-mail me.

You're about to learn one of the most important languages - C. C was written by Dennis Ritchie who worked at Bell Laboratories for quite some time. It was then that he designed the compiler for a PDP-11 running a UNIX. Huh? Okay, okay, I'll try and keep the boring meter set on low. You see, C is a programming language, but needs a compiler in order to run the codes you type in. Contrary to what others might tell you, code is another word for a program. But anyways, let's move on . . .

Now you're probably wondering why I decided to write this tutorial. Well, to be honest, I had nothing better to do and also, every other tutorial I come across always jumps right in without explaining anything so I wanted to make a simplistic tutorial. I'm going to assume that you've had no experience in programming and that you're a complete beginner. If you begin to stress out at the beginning of learning C, don't worry about it. You can e-mail me anytime at A1agent007@aol.com. Yeah, it can get frustrating at times and might seem a bit cryptic, but when you start to get all freaked, just think of me who typed part of this out, and then lost it to a computer crash and had to start over. I bet you can guess which operating system I'm running, cough, cough, Windows, cough, 95.
Correction: I now use Windows 98 and have had zero problems. But I had Windows 95 when I wrote this tutorial. Blah, blah, blah . . .

Anyways, enough of me wasting your time with pointless rambling, let's get on to what you'll need before you begin. Well, duh - first of all you'll need to have access to a computer. I've only used C in MS DOS (if you have Windows 95 or 98, just use your MS DOS prompt to run the program or if you have multiple operating systems on different partitions or on one, you can always use MS-DOS in whatever version). And if you're on a MAC, I can't help you because I'm not that familiar with MACs. But C is cross platform so if you're running a MAC and have a IBM/IBM-compatible friend, you can give him or her the program you wrote and it also works the other way around. You will need a C compiler, preferrably one that goes by the ANSI standard. Okay, I'm rambling again, I apologize. Anyways, let's get our feet wet in some C already, whadda ya say?

First and most importantly, let's start with naming identifiers. Don't run off on me yet, let me explain. An identifier is usually used when doing functions, variables, and other mathematical C stuff (math is in everything, especially C!). With variables, it's all case sensitive. So let's say you're naming an identifier Nrrrrd. Nrrrrd is not the same as nrrrrd or NrRrRd, and so on. Up to 31 characters can be used and once you hit character number 32, it will be ignored. Jeannie, I still don't get a word you're saying. Okay, okay, so maybe I jumped a little too far. When you're in C, you're going to be naming variables and so don't worry about all of the details yet. Once we get farther into this tutorial, everything will come together. Now once in a while, I may assign for you to write a program. But don't worry, I don't expect for you to be able to write one right away. And if you have any trouble, just tell me.

Now let's start learning some more about C. We'll go over the basic commands in a C program and the formatting. For one, in almost all C programs, every line ends with a semicolon. Also, you'll need to remember that a forward slash (/) and a backward slash (\) are two completely different things when assigning them in C. Some parts in a C program won't need semicolons, but you'll be able to use your common sense when putting in semicolons and when not once you've mastered this tutorial.

When executing a program, the program's commands are usually executed line by line no matter if you've got the latest pentium chip or not. Remember that C isn't as fast as assembly language, but C takes less coding so the time you save in typing out the coding makes up for the speed (believe me).

Also, remember that C is pretty much case-sensitive. It prefers that you use all lowercase when you're typing in the code. Do remember that all of C's commands and library functions must be typed in lowercase. Also, in a C program, the directive (which are used to build in specific values like PI) is always ahead of the # character. Yeah, you're probably thinking that I'm filling your brain with too many terms, but let me remind you once more - we're just on the introduction and when we get farther into the tutorial, you'll begin to understand.

As you write your C program, you'll be inserting comment tags. Comment tags won't show up on the programs display or anything, they're just something programmers like to insert into their source code like little notes explaining what's going on because let's say, that when you go back to edit your program a few months later, you may have forgotten what a few things mean so these comment tags are like little notes that the computer ignores, but are just visable to those who have access to the source code (the programmer). Comment tags are incredibly useful because, for example, say you write a transaction program, but you can't remember which part of the code contains the transfer command. Now if you had placed a comment tag where the transfer code was, you would have saved youself a lot of time in searching for it. When you start writing some long, long, and I do mean long programs, you'll find that the comment tags come in real handy. Another thing that you should have a pretty good understanding of are structures. Structures are like little trees that hold all of the information about a certain thing. Such as in your bank program, the structure might hold the transaction history of a certain customer. Here is how a comment is made in C: /* comment */. That comment will not be shown by the computer when the program executes. As you read farther on, the first program example will show you an example of a C comment.

Now many of you who have taken Algebra 1 may remember functions. With C, a function is part of the program that carries out one of the tasks. UNIX, which was a operating system written in C, does many different functions, not just one.

In C, there are also statements within a function in which let's say you're going to multiply two numbers and save the solution in a variable. Such as E = 8 *5 so every time you use the variable E it will represent 40. This comes in handy because sometimes an equation has to be changed. An example of this would be a stamp price program. So let's pretend that stamps cost fourty cents. And the post office had to configure in their systems how much 586 stamps would cost. Well, 586 stamps would cost $234.40, but rather than put in the stamp price throughout the entire program for other mathematical configurations, they could just put a variable called F in and have that represent $.40 and so if the price ever went up, they could just change the equation rather than go throughout the entire source code and change the numbers. Get what I'm saying?

Now I think you're ready to move on to C if you read the above. So let's begin. First, before you open your compiler, let me show you some basic examples so you can learn off of them because I know you're really eager to start learning. Here is example 1. /* my first C program */ #include <stdio.h> #define RICHGUY "Bill Gates" main () { printf(RICHGUY); return 0; } Okay, so you're immediate reaction is probably something like a blank stare, but don't worry. Let's start at the beginning of the program and explain it. Starting at the first line, you'll notice something that says /* my first C program */. That was just a simple comment that programmers like to insert in. Comments can pretty much go anywhere except for in between code because that can confuse the compiler at times. When you run the above program, you will notice that the output screen will not say my first C program. It will just skip over that comment. Got it?

Okay, now moving on you'll notice somewhat of a cryptic command. That #include just pretty much does what it says - includes something. But what does it include? Why it includes a file. <stdio.h> is a library file that your C compiler came with and the stdio.h is supposed to stand for Standard Input/Output. It's a header file that pretty much contains a lot of the info your program will need. If we had inserted other commands such as a clear screen command clrscr(); then we would have to have included the library file conio.h along with stdio.h.

Let's move on to that #define thing. Perhaps one of the greatest and most simplest things in the C language is the #define preprocessor directive. Huh? You said simple Jeannie. Okay, I know, I know. In other words, that define thing defines a named constant. The named constant is usually something that might show up a lot in a program. So when it said #define RICHGUY "Bill Gates" it meant that for every time the word RICHGUY showed up in the program, to put the words Bill Gates there instead. Now one of the reasons we put in that #define term in a program is because sometimes something changes. For example, let's assume that Bill Gates becomes poor (like that's really going to happen, but stick with me here) and that Michael Jordan suddenly becomes some multi-billionaire. So then, rather than go throughout a real long program and change everywhere it said Bill Gates to Michael Jordan, you could just change that #define RICHGUY "Bill Gates" to #define RICHGUY "Michael Jordan". Do you see what I'm saying? In the program example I showed you, it didn't contain the named constant RICHGUY a lot so it's not like the #define thing was absolutely necessary, but as you move onto writing programs that are very long, you'll find that the #define comes in quite handy. This would be a good time to mention why you place the include and define preprocessor directives before the main (). This is because the directives execute instructions before your compiler gets your program ready for execution. I know that this doesn't make much sense right now, but it will seem more clearer once we get through a couple explanations and examples.

As we reach farther down the program, you come across something that says main (). That is a function. All programs must contain at least one function and that function main () is the one that is executed before anything else. Other functions can come before it, but if you want your programs to run faster, you'll include that main (). If you have more than one function in the programs that you write, be sure to list those functions right after the } (closing brace). More on additional functions in the next lesson, for right now, just concentrate on one.

The brace { began the body of the program. Nothin much I can really say about that beginning brace right at this moment, so let's move on to the printf command. printf prints on the screen whatever is inside of the paranthesis. The named constant RICHGUY was put inside the paranthesis, but if it had been text that the computer was supposed to immediately display, then the text would have been put inside of quotes. For example, if it had said printf (BILLGATES) then the computer would have assumed that BILLGATES was a named constant and it would have looked for the word it was supposed to define. But if it had said printf ("Bill Gates") then the computer would have just displayed the text Bill Gates on the screen. You'll understand this better as we move on.

return 0; told the compiler, "Look, just return back to the compiler and stop the program."

And finally, the closing brace } told the compiler to end the main function.

Now that wasn't so bad was it? You're on your way to writing C programs in no time. But wait a sec, that was only the beginning, if not the introduction. We gotta get started on some math skills. Everything involves math (everything), whether you like it or not. You're going to need to begin with integers, so let's get started with something easy. Here goes. int value; value = 5 The first line contained the text int. That stood for, you guessed it - integer. Now came value. Value was just something I named a variable. It coulda been John, Christy, whatever. Then came the semicolon so the compiler knew to execute that command. On the second line, we just defined the variable value as 5 saying that it equaled 5.

And do you remember in gradeschool when you first found out about exponents. Well, let's define a double variable which are numbers that are exponential. double good; good = 60E + 2; The next type of data type is a character. Here is yet another example. char letter; letter = 'c'; With single characters, you have to use single quotes. Makes sense.

But let's say you need to define a variable like 4.86. That is what we call a floating point. When writing accounting programs, floating points are used often. Here's an example of defining a float variable. float cash; cash = 4.86;

Well, thsoe were the four data types. But now I think we need to go over the major primary mathematic operators. This will just take a second and it's just something that we can get out of the way so we can use the operators and apply them to programs.

The first operator would be addition. Nothing out of the ordinary. It uses + as its operator. An example of using it would be digi + 5. And all that did was say that for whatever variable digi was, to add 5 to it. The second operator is subtraction which just will use - as its operator. Example: blah - blah1. So just subtract blah1 from blah. Then comes multiplication and division. The multiplying operator uses * and the dividing operator uses /. So now that we've gotten operators and data types down, I think it's safe to say we can move on and interwind everything we've went over together.

Next on our list to learn are keywords. Some of these you've already experienced above. In total, there are 32 keywords that you can use in C. Even int (integer) is a keyword. Without keywords, C wouldn't be able to do a lot. They are listed below. The 32 keywords above are the ones that follow the ANSI-C standard. Some compilers that you buy at the store might include others. The store bought compilers usually contain special features, but that doesn't mean that the ones you get off the web don't include special features too. Sometimes the ones you get for free are the best kind because usually they author will send the compiler source code out and let other people add on more and more features.

So now I think we can jump into some calculations. Here is an example. #include <stdio.h> main() { int answer; answer = 340 + 31; printf("If you add 340 plus 31 together you will get %d\n", answer); } Okay, let's go through this. First, we included stdio.h because that allowed the output with the printf statement (remember, stdio.h just means standard input/output). Then, just the usual main and curly brace came along, but on the next line, you will find something that says int answer;. That just stated that the variable answer will hold a integer. Most of this is review, but then all of a sudden, we come across one of the four operators. That operator is the addition sign. So that line just said that the answer will equal 340 plus 31. And then came the required semicolon so the compiler knows to execute that like. And now, for the part that most people get way too carried away with when it comes to writing programs. The printf statement. Now this is a genius command, don't be mistaken, but sometimes beginning programmers end up doing absolutely no action, calculation, etc and just make the program out to be a bunch of text. Great in theory, but bad for innovation. Oh yeah, back to that line, along comes printf and then inside of the paranthesis are quotes so the the C or C++ or whatever compiler you're using knows that it should be printing text and not a named constant or whatever. But wait a second! What's up with that %d\n thing? The % sign is used to display the value of whatever the variable is. And then the d which followed % means that the value of the variable will be a decimal integer. But we're not finished yet. Along comes the \n which indicates a newline. The \n falls along the category of cursor control formatters. More on that in a second. Back to the program example, let's talk about that variable inside of the paranthesis. That variable was sum. Let me stress again that it could have been anything you wanted it to be, I just called it answer. If you will look back up at the example, you will notice it is outside of the quotes. This is because it has another value to it. Following the closing quote was a comma which broke off the standard text. Finally comes the ending paranthesis, the semicolon, and the closing brace. Ta-da! You just found out how to program with integers.

And before I get you ready for other C programming lessons, let me mention that along with the \n cursor control which means newline, there is also \t for tab; \r for return; \f for form feed; and \v for vertical tab.

Remember at the beginning of this tutorial when we learned a but about variables like integers, characters, double, string, etc? Well, besides the %d for decimal integer, there are also other commands such as %f for float; %e for double; %s for string; and %c for character.

Wow! You've made it this far already? That's pretty good. So now let's take a little break and just make your program do a beep. That's right, it'll beep. So to ring the alarm just do \a. I only use the beep when I wanna scare the users into thinking they screwed up. So sometimes I'll just write a program and I'll say "press any key to continue" and I make the values set so that if they press any key, it'll beep simutaniously. Of course, it's always good to leave a little backdoor in your programs so I always made the letter V allow them to continue. Kinda cruel, but my excuse is that it was a good learning experience.

Sorry, but we're going to have to get back to the NRRRRD topics of programming. Next on our list is conversion characters. Conversion characters can have a pretty good relation to the printf command when you want to print single characters. So for example, you want to print the number 12,387. You may think that you can just type that in the printf function, but actually there's another way. Try this: printf("%d%d,%d%d%d%d", 12,387); Notice how 12,387 weren't in quotes? That's because in a way, the %d was acting as a fixed variable although it can't be a complete variable because it has the same value for different amounts. Um . . . okay . . . Alright, the concept is hard at first to comprehend, so don't worry if you're all confused. The %d is being used because that's a way to print integer data. Why %d? Well, you can also use %i, but from what I've read, not many programmers use that. C has a really good "built in brain" if that made any sense at all. The reason I'm saying this is because when %d shows up, C automatically looks to the right of the string for any data. But I still don't understand why I have to use conversion characters Jeannie, I mean why not just type it out? Because sometimes when your programs get very complex, C can get confused. I know, I said that C has a really good "built in brain" but that doesn't mean C is perfect. It has a long way to go yet. Kind of like this tutorial . . .

If you need to format the things you type out, a good way to do this is with conversion characters. So if you wanted to space out the integers into colums, just add a number such as 3 before the %d so that it knows to do spacing of 3. Also, don't try and fit a number like 12,387 into a three column space because C will just ignore it because there are too many numbers.

A great way to format the integers is by doing this: printf("%4d%4d%4d%4d", 3, 4, 5, 6); Okay, so that's all great and everything, but there aren't just plain old integers in C, remember towards the beginning of this tutorial where we talked about floating points, etc? Well, you use %f for that. The %f will support up to six decimal places, but you can modify that by doing this: printf("The cost for Red Hat Linux is usually %2f", 50.00); But before you go yawning on me yet, you can also use %c. You guessed it, %c is for single characters. But do remember that with single charagers, use for example 'A' because single characters require single quotes. But just in case you need to see an example, I provided one. printf("My name begins with a %c.\n", 'J'); See it? I did throw in a new line, but do you see how it all works? You do? That's great, then we can move on.

I'm crossing my fingers here that you know scientific notation. Sometimes when you work within C, you may get a number that is too large. Such as if you were dealing with Bill Gates net worth (heck, that would bring down the whole system), and that's why programmers developed a genius little way of representing all those zeros. When you get those large numbers, you'll see an E following them rather than a lot of digits. That E stands for exponent. So you take that number before the E (pay attention now) and you multiply it by 10 raiser to the power that follows a minus or plus sign. Um, you lost me Jeannie. That's okay, let's follow the traditions of this tutorial and show an example. Let's say that you get 5.7E+9. You would take 5.7 and multiply it by 10 with 9 zeros following the ten. But if there had been a subtraction sign, you would go to the negative power of ten using 10 to the negative 9th power. Get the idea. So take notice of what the mathematical sign is.

If you want to display numbers in exponential form, there is a simple to use way. printf("%e", 958.3); This comes in handy for very, very large numbers. And if you have a number that's for example 7 characters long (one over the limit) then you can %G. printf("%G", 4938302.4); There are so many other things you can use with conversion characters such as getting a hexidecimal value printf("%x", 450); or making C output an octal integer in octal: printf("%o", 60); I'm not going to go into all the conversion characters because I decided to list the ones that were necessary. So now let's move on to variables, whadda ya say?

With variables, it can simply be explained that variables store information. For example R = 4. R would be the variable. Never use numbers as variables because that doesn't work out very well at all. But you could use R2 as a legal variable. And R2 isn't the same as r2 because variables in C are case-sensitive. When defining variables, you need to include the data type. For example: #include <stdio.h> #include <conio.h> main() { int nrrrrd; clr(); nrrrrd = 31; printf("The value of nrrrrd is %d", nrrrrd); return 0; } You always should define the variable type right after the beginning brace. So I declared that the variable nrrrrd is an integer (int). Then I set the value of nrrrrd to 31. And then, just like when we used conversion characters, I used %d for the value nrrrrd in the printf statement. Just look over that program and it should all fall into place. It should output The value of nrrrrd is 31.

But what if we want to get a number from the user? And store that in a variable and then use it in an equation? I think we're ready for that. #include <stdio.h> #include <conio.h> main() { int number; clr(); printf("Tell me a whole number from one to ten: "); scanf("%d", &number); return 0; } Now that we're up to variables, it would be best to dive into arrays. Here is a example introduction to character arrays. #include <stdio.h> #include <conio.h> main() { char MYArray[6]; clrscr(); MYArray[0] = 'H'; MYArray[1] = 'e'; MYArray[2] = 'l'; MYArray[3] = 'l'; MYArray[4] = 'o'; MYArray[5] = '\0'; printf("%s", MYArray); return 0; } Seems like a seminormal C program except when you get to the 5th line. That char signified that this was going to be a character array named MYArray (and this array would have 6 elements. So each character is an element. C starts with 0 and works its way up (this is the same case with other languages sometimes). On line 7 is when the first element of the array is noticed. This has the value of H. And then you reach the 8th line and so on and so on. Eventually you make your way to the 12th line which as \0 which is the synonomous value for null. Null? Null is a element visible to the computer in a string, but is invisible to us. It's always best you include it in your arrays like this, but not all arrays. Remember that we're on a character array.

We've been using a lot of the printf command so it seemed like a good idea to include it in here. But what's that %s? That's for a string. And with that, it allowed the array MYArray to be printed as Hello on one line because we told it to print MYArray. Character arrays can store strings so that we can print things like this out. This might make no sense now, but C uses that %s so it knows not to just print one part of the array, but all the characters which would make up a string.

Alright, so let's get to operators. Math is in everything and without operators in programming, we couldn't do much mathematically. Now there are other operators that don't perform mathematical operations, but more on those later. The operators we're going to first study are + - / * and %. The + performs addition or can be used in front of a number to show it's positive, the - performs subtraction or can be used in front of a number to show it's negative, the / performs division, the * performs multiplication, and the % is referred to as the modulus or for you math addicts, modulus is synonomous with remainder. Yada, yada, ya, let's get on to an example already. /* This is the lucky number program. Find out what your lucky number is! */ #include <stdio.h> #include <conio.h> main() { int day, month, year, fave, lucky clrscr(); printf("This program will find out your lucky number. \n"); printf("So get ready and do everything the program tells you to do!\n\n"); printf("What day were you born on? As in 1 for the first, etc. "); scanf(" %d", &day); printf("/n "); printf("What month were you born in? As in 1 for January, etc. "); scanf(" %d", &month); printf("/n "); printf("What are the last two digits of the year you were born in? "); scanf(" %d", &year); printf("/n "); printf("What month were you born in? As in 1 for January, etc. "); scanf(" %d", &month); printf("/n "); printf("What is your favorite whole number from one to 100? "); scanf(" %d", &fave); printf("/n "); printf("Please wait while we compute your results. \n"); printf("The computer will beep when it has reached an answer.\n1 "); printf("Done! \n", '/a'); /*here is the equation to find the luck number */ lucky = (day * month) + year /fave; printf("Your lucky number according to our computations is %d.", lucky); printf("\n"); printf("Thanks for playing the lucky number game!"); return 0; } Now that really wasn't that hard. Before the main function, we declared some variables as integers. We used printf to ask the user questions and then used scanf to retrieve the input and stored the answers in the variables. The /a rang the bell and appeared as a character literal although it could have appeared in the quotes. What? Well here's an example of how the /a could have appeared in quotes rather as a character literal. printf("Done! \n /a "); So it didn't matter either way, but I just wanted to show you that as an example. But anyways, the other inteesting part to this program was the equation. lucky = (day * month) + year /fave; The equation included the * + and / operators. By using the paranthesis that told C to perform the day times month part of the equation first. Remember paranthesis come first in the order of operations? Please Excuse My Dear Aunt Sally . . . oh come on, don't look at me like I'm crazy. Please for paranthesis, E for exponents, M for multiplication, D for division, A for addition, S for subtraction. Yeah, that's it!

I can't go on to the next section until we cover compound operators. So here goes.

nrrrrd = nrrrrd + 5; But Jeannie, you used the same variable on both sides of the equal sign . . . what gives? Well, by doing this it's as if you're changing the current value of the variable, in this case adding five to whatever the current value of nrrrrd is. But be sure that the variable nrrrrd has already been declared though, otherwise you'll get an error.

Now there's even more to operators. Relational and logical operators. Relational operators are ==, >, <, !=, <=, >=. Can you take a guess at what each operator does? Well the == just means equal to. The > means greater than, the < means less than, the != means not equal to, the <= means less than or equal to, and finally the >= means greater than or equal to. Let's cut to an example of each. nrrrrd == 4 nrrrrd > 3 nrrrrd < 7 nrrrrd != 5 nrrrrd <= 8 nrrrrd >= 4 So what are the logical operators Jeannie? I'm getting to that part. The logical operators are &&, !, and ||. The two || that are parallel appear as dotted parallel lines on your keyboard. The ! means not, the && means and, and the || means or. Um, you're not making sense here. Okay, I guess we can cut to another example. if (nrrrrd > 2 && nrrrrd != 4) { printf("This is just an example."); } Whoa now, where'd that if come from? If just means that. IF nrrrrd is GREATER THAN two AND nrrrrd is NOT EQUAL to 4 then print the text This is just an example. If statements have a big job. They decide if a part of the program is executed or not. Because let's say nrrrrd was equal to 4, then the text This is just an example never would have been printed. The if starement uses the braces, but be careful where you put your semicolons. You'll be able to tell when and where to use semicolons after you gain a better foundation in C.

There's also the else statement. If the if statement doesn't end up being true, then else comes in. Here's an example.

if (nrrrrd > 2 && nrrrrd != 4) { printf("This is just an example."); } else { printf("Hey there!"); } So if nrrrrd did end up being less than 2 or equaling 4, then the else statement would have came into play and it would have printed the text Hey There!.

The if-else statements are so incredibly useful, but since we're on the topic of operators, let's hit the next one on our list - the typecast operator. There are going to be times in your C programming when you're going to want to change data. Such as cloning a floating-point into a integer and that's where the typecast operator enters. float nrrrrd = 4.3; int blah = 3 int nrrrrdblah; nrrrrdblah = (int) nrrrrd + blah; That may look kinda messed up, but the truth is that the above example is correct. First we go defining a floating point value of nrrrrd as 4.3. Then we give blah a integer value of 3. Now comes int nrrrrdblah (which could have been anything) and I just define that so we can use it in the formula. The tricky part comes in when we have nrrrrdblah (which is an integer variable right now) have a floating point variable in it which is nrrrrd. But that all changed because I used a typecast operator (int) of integer to make the floating point value nrrrrd a integer. Whoa, talk about a mouthful. But did you understand that? If not, think of it like putting on a new outfit. First you're wearing sneakers, but you want to wear a skirt. No can do, that's fashion violation 321. But if you change your sneakers into some dressy platforms, you can wear that skirt. Note if you're a guy reading this example, then think of it this way. You want to wear a tie, but you're wearing a ratty old t-shirt. But if you change your outfit into something dressy, then you can wear the tie. Equations are the same way. You have to change something in order to get what you want. So you can use typecasts to change something. I used the typecase (int), but you can also use (char), (double), and (float).

So now I think we should move on to loops. Loops? Yes, there are many ways to do loops. Loops mainly involve boolean logic of whether being true or false. /* Quiz: How Much Do You Like Computers? */ #include <stdio.h> #include <conio.h> main() { int like_computers clrscr(); printf("On a scale of 1 to 10, how much would you say you like computers? "); scanf("%d, &like_computers); while (like_computers = 10) { printf("Wow! You've passed the quiz! "); } if (like_computers < 6) { printf("So you don't like computers?!? "); else { printf("Cool, so you're a computer fan "); } return 0: } What the heck is up with all the braces? Well, those braces single off the functions. The while statement just means that while the user likes computers on a scale of ten, to print the line Wow! You've passed the quiz! The if statement is that if the user likes computers on a scale of 5 or less they are believe to not like computers. And if they like computers from a scale of 6 to 9, then they get the line Cool, so you're a computer fan.

We'll cover looping more in examples that also form the basis of other concepts. So with that in mind, let's move on.

We've been using return 0; a lot because that's how I learned to use C. My first compiler that I compiled programs on was a Turbo C++ 1.0 Lite. I used a C++ compiler so that when I was ready to move on to C, I had that advantage. But there are other ways to end the program and we're going to learn those ways right now. { break; } Break should be used to break for-loops. Don't use it in a plain old if statement (it can come in an if statement if the if statement is in a for loop), because then if you use break outside of a for-loop, you just wind up with errors.

for (nrrrrd = 40; nrrrrd > 0; nrrrrd--) { break; printf(" %d", nrrrrd); } So now it's time to take a breather. Let's use the goto command. It's used in other languages, so it's about time we talk about it in C. #include <stdio.h> #include <conio.h> main() { char yourname[15] clrscr(); printf("What is your first name? "); scanf(" %s", yourname); printf("\n"); printf("Well hi, %s", yourname); goto goodbye goodbye: printf("Goodbye! "); return 0; } That wasn't the greatest use of the goto command, but it comes in handy for when you're writing a program that may not need a certain section of it displayed if the user's preferences are different.

We've been printing so much text on the screen, but what about the printer? Now that's an idea. To send text to the printer, it's quite easy. fprintf(stdprn, "Ta Da!"); With the fprintf function, you can also use variables. Such as %f for a floating point, etc. But why is the stdprn there? Because this way, it establishes a instruction which stands for standard printer. I don't wanna spend too much time with fprintf because we're going to move more on to loops.

Let's get back on with nested loops. I really didn't stress those. What do you say we go to an example?

for (nrrrrd = 0; nrrrrd <= 50; nrrrrd++) { for (blah = 0; blah <= 50; blah++) { printf("%d%d \t", nrrrrd, blah); } } Um, okay . . . Come on, it really isn't that hard, let's review over it. There are two for statements. One is nested in the other one. The blah loop is inside of the nrrrrd loop and when you put the two together you get a bunch of numbers. Not exactly a pretty program. But a good learning example.

We'll touch more on loops later. But maybe just one more - the if-else loop. We've used a few types of loops earlier on in this tutorial, but I just want to dignify loops a little better. if (nrrrrd = "6") { blah = 3; } else if (nrrrrd = "9") { blah = 4.5; } else { blah = 0; } So if nrrrrd equals 6, then blah equals 3. If nrrrrd equals 9, then blah equals 4.5. And if nrrrrd equals anything else, then just make blah 0.

Alright, now we can move on. Since C is a great language for input and output, let's study character input/output functions. Sounds hard, but it's not. I swear.

There are six common character I/O functions. They are as follows - getchar(), putchar(), putc(), getch(), putch(), getche(). Don't ask me why they made character functions so much alike so that it's confusing, but I guess we just have to live with em. printf("What's your name ?"); a = getchar(); printf("Your first name has a initial of %c", a); Notice how I used a character variable for the getchar() function? You'll be using the %c for all the character input/output functions.

That's all for now. I'll be sure to get back to this tutorial ASAP. Later people and keep programming!

Be prepared for a update to occur soon