C Programming for beginners, A short tutorialLoirak -> Programming -> C Tutorial |
||
Learn C Programming, A short C TutorialSo you want to be a hero.... I mean programmer, well by reading this tutorial maybe we'll be able to help you accomplish your goal. First a little about C and its derivative C++. It is considered to be the most commonly used programming language. If you want a job in programmming, you will need to know this language. But what makes C the language that everyone wants to use. Well, its been around for quite some time. The original C since the 70s, and C++ since the early 80s, which is like eons in computer time. C is the most versatile high level language. C permits just about anything, allowing programmers to write straight to the metal. Its code runs very fast. Note that this tutorial will not cover C++ (C with classes).Table of Contents:
1. Your First ProgramIf you've ever read another tutorial or a book on a programming language, then you've probably seen a program like this before. The hello world program. It is a perfect starting place for our tutorial. So, type the following program into your favorite editor. If you are using Windows then try notepad or DOS Edit, if using a *Nix variant use VI or emacs. Without further adew here is the program :/* hello world program */ #include "stdio.h" void main() { printf("\nHello World\n"); // print to screen }Save this code into a file, and call the file hello.c, then compile it by typing at a command prompt: gcc hello.cIf you are using *Nix you should have an a.out file now, but if you are using Windows you might be wondering what is gcc, and where do I type the above line into. On Windows getting up and started with C is little bit more difficult than with a *Nix variant. You can get a variation of gcc to output Windows code called DJGPP at www.delorie.com/djgpp/ Setting up the compiler is probably the hardest part to getting started programming, and unfortunately this site doesn't cover setting up DJGPP. Another compiler option is to get Microsoft Visual C++, an excellent editor, with lots of nice features. The draw back is that the standard edition costs around $100. But if you can afford a copy, get it. If you have Visual C++ and it is already installed then load it, click File, then New, then select Win32 Console application from the project list. Type in a project name, and then press ok. Next it will ask you what it should automatically create. Select the hello world application, and then you should get a program very similar to the one above. F7 compiles, and Ctrl-F5 will run it. From now on the tutorial will refer to the *Nix method and output files. If you compiled the program in Windows instead of *Nix you would have a hello.exe instead of a a.out
This creates an executable file a.out, which is then executed simply by typing its name. The result is that the characters Hello World are printed out, preceded by an empty line. A C Program contains functions and variables. The functions specify the tasks to be performed by the program. The above program has one function called main. This function tells your program where to start running. main functions are normally kept short and calls different functions to perform the necessary sub-tasks. All C codes must have a main function. Also notice that C is case-sensitive. The commands have to be written like they are above. C also denotes the end of statement with a semi-colon like Java & Pascal. Brackets signify either to "{" begin a group of statements, or "}" end a group of statements. The // or /* comment */ designates a comment. Anything after two slashes the compiler ignores. The last part of the program you should notice is the #include. This simply includes a group of functions from the filename specified between then less than and greater than signs (<...>). The file above stdio.h contains a list of standard functions for C to use, the function the our above program uses is printf. Printf takes a string of characters between quotation marks, and outputs them to the screen. Now that you understand the hello world program it's time to learn more about
data types, and the printf statement.
2. Data Types and PrintfHere is our next program to discuss. It declares several variables, performs some computations, and then outputs the results of those computations to the screen.#include <stdio.h> void main() { int numcandy; // declare a number variable double cost; // declare a variable that can store decimals printf("How many lollipops do you want: "); scanf("%d", &numcandy); // get input from user cost = 0.55 * numcandy; // do some math printf("\nPlease pay %f to the cashier!\n", cost); } This program when run, well prompt the user for how many lollipops they would like. After they enter a number and press enter, it will print out how much it will cost them to buy that many lollipops assuming lollipops cost 55 cents each. The main, {} brackets, comments, and include should be familiar. The new part of this program is the scanf, variables, and the use of printf to print out numbers. of this program that we haven't seen before. First lets talk about the variables. This program has two variables numcandy, and cost. These are the names of the variables. Before a variable can be used it must be declared. Declaring a variable in C is easy, you simply tell the compiler the type, and the name you want the variable to have. So in the above example. The line int numcandy; tells the compiler you want a variable of type integer with the name numcandy. Then you can simply use numcandy through out your program to store int's (integers). Integers are non decimal numbers like -13, 0, 10, 5, etc. The other variable in the above program is a double which is used for storing numbers with decimals. Next the scanf, and printf statements. After running the program you will notice that the scanf function simply gets a value from the user. It waits for the user to enter a number, and then press enter. It puts this value into our variable numcandy. Next notice some special character sequences contained in both the scanf and printf. First the printf has the sequence "\n". This sequence simply means move to the next line. Next we have the %f and %d. These tell printf and scanf what type of variables to expect. %f corresponds to double, and %d is for int. So if you changed the above programs printf line to ...pay $%d to... then you would get wierd output since printf would try to print cost as an integer, which doesn't make sense, because cost is a double. You need to make sure the "%" go with the right types. Variable names are arbitrary (with some compiler-defined maximum length, typically 32 characters). C uses the following standard variable types: int -> integer variable short -> short integer long -> long integer float -> single precision real (floating point) variable double -> double precision real (floating point) variable char -> character variable (single byte) The printf function can be instructed to print integers, floats and strings properly. The general syntax is printf( "format", variables );where "format" specifies the converstion specification and variables is a list of quantities to print. Some useful formats are %.nd integer (optional n = number of columns; if 0, pad with zeroes) %m.nf float or double (optional m = number of columns, n = number of decimal places) %ns string (optional n = number of columns) %c character \n \t to introduce new line or tabSo using this new knowledge, you could change the above program where it prints the cost to ...pay $.2f to... which would print the number, and then only two decimal places of the double. Look back up at the program one more time at the line cost = 0.55 * numcandy;. This is the math of the program. This line is fairly self explanatory. It takes the value which is stored in numcandy multiplies it with 0.55 and puts the resulting value into cost. Here are a list of the standard math operators you can use in your programs: + add - subtract * multiply / divide = equals (assignment operator)Now that we've covered printf and the basic data types, we can move onto program control meaning loops and conditions. 3. Loops and ConditionsReal programs are than just getting input, performing some math, and then printing out the results. The next obvoius step would be to add some sort of control. Like for the above example program if they put in that they wanted to buy more than 1000 lollipops. We could print out, that the store doesn't have that many. This section will describe the basic loop and condition statements available in C, and then will end with a short example program putting a few to use.First Conditional statements. C has three types of conditional statements if, else, and switch. Here is the syntax for if and else: if (condition_1) { ...block of statements executed if condition_1 is true ...statements are either more ifs and conditions or normal normal lines of code that end with semi-colons } else if (condition_2) { ...statements executed if condition_2 is true and condition_1 was false } else { ...statements executed otherwise meaning 1 and 2 were false }and any variant that derives from the above. Either by omitting branches or by including nested conditionals. Meaning having an if inside of another if. Now that we can make an if we need to make the condition that goes inside of the parenthesis of the if. Conditions are logical operators involving the comparison of quantities (of the same c type). Here are a list of the basic conditional operators. < smaller than <= smaller than or equal to == equal to != not equal to >= greater than or equal to > greater thanand the boolean operators && and || or ! notNow that you know the operators here are some sample conditions. For instance using the above example. if (numcandy > 1000) { printf("The store does not have that much candy in stock!"); } else .... print out the cost .....The boolean operators are for combining more than one condition into a single if statement. So for instance if (money>500 && cost<10) ...This condition would only be true if both money was greater than 500, and cost less than 10. If you replace the && with || then the condition would be true if either money is bigger than 500 OR cost less than 10. I think you get the picture. Another couple things to notice about conditional statements in C is that the equality operator is == (two equals) and not = one equals sign. The single equals sign as we learned in lesson two is the assignment operator. You use it to assign values to variables so when you want to test if a variable is equal to something use two equals signs == and not one. Remember THIS! It is very important. Next with C conditional statements you can also omit (leave out) the operator. if you had a conditional statement: if (numcandy) {...}This would evaluate to true and the statements inside the {} brackets would be executed, if numcandy was not equal to zero. Understand? If you have no operators inside the parenthesis then C will simply check to see if the value is anything but zero. Another conditional use is in the switch construct: switch (variable) { case const_1: { ...block of statements... break; } case const_2: { ...block of statements... break; } default: { ...block of statements.. } }The appropriate block of statements is executed according to the value of the expression, compared with the constant expressions in the case statement. The break statements insure that the statements in the cases following the chosen one will not be executed. If you would want to execute these statements, then you would leave out the break statements. This construct is particularly useful in handling input variables. Here is an example, if you had a integer variable called race position. switch (race_position) { case 1: { printf("First place, you get gold!\n"); break; } case 2: { printf("Second place, you get silver!\n"); break; } case 3: { printf("Third place, you get bronze!\n"); break; } default: // all other numbers (anything but 1,2,3) { printf("You dont get anything, you lose!\n"); break; } } // end switchYou could have easily done this same thing with three if's and and else. But we wanted to demonstrate the switch. As an excersize rewrite the above code segment to use if's instead of the switch. That's it for conditional statements, now we come to the next part of lesson three loops. C provides two basic types of loops a while and a for loop. While and for loops continue to repeat basically until certain conditions are met. Here is the syntax for while and for loops: while (condition) { ...block of statements to execute... }and the for loop: for (expression_1; expression_2; expression_3) { ...block of statements to execute... }The while loop continues to loop until the condition becomes false. The condition is tested upon entering the loop. Any logical construction (see below for a list) can be used in this context. The for loop is a special case, and is equivalent to the following while loop: expression_1; while (expression_2) { ...block of statements... expression_3; }For instance, the following structure is often encountered: i = initial_i; while (i <= i_max) { ...block of statements... i = i + i_increment; }This structure may be rewritten in the easier syntax of the for loop as: for (i = initial_i; i <= i_max; i = i + i_increment) { ...block of statements... }Infinite loops are possible (e.g. for(;;)), but not too good for your computer budget! C permits you to write an infinite loop, and provides the break statement to ``breakout'' of the loop. For example, consider the following (admittedly not-so-clean) re-write of the previous loop: angle_degree = 0; for ( ; ; ) { ...block of statements... angle_degree = angle_degree + 10; if (angle_degree == 360) break; }The conditional if simply asks whether angle_degree is equal to 360 or not; if yes, the loop is stopped. Finally, to wrap up lesson three we will introduce #define's and then give the sample program. You can define constants of any type by using the #define compiler directive. Its syntax is simple--for instance #define ANGLE_MIN 0 #define ANGLE_MAX 360would define ANGLE_MIN and ANGLE_MAX to the values 0 and 360, respectively. C distinguishes between lowercase and uppercase letters in variable names. It is customary to use only capital letters in defining global constants. Then inside your program you could insert ANGLE_MAX anytime you wanted to use the number 360. Now here is the sample program. You should be able to understand all the code there in. Basically there is a #define for a number of lollipopps, and you sell lollipopps until you run it, but there is also a limit too how many you can get at one time. Then after all are sold, the amount of money for all purchases is printed out. #include <stdio.h> #define START_NUMBERLOLLIPOPS 100 #define MAX_AT_ONCE 30 void main() { int numcandy; double cost; int numberlollipopps; double totalcost; numberlollipopps = START_NUMBERLOLLIPOPS; // set start value to our constant totalcost = 0; while (numberlollipopps > 0) { printf("(%d left) How many lollipops do you want (-1 quits): ", numberlollipopps); scanf("%d", &numcandy); if (numcandy == -1) // since this if has only one statement brackets are not needed break; // exit out of the while loop else if (numcandy > MAX_AT_ONCE || numcandy <= 0 || numcandy > numberlollipopps) { printf("You cannot have that many, enter another number\n"); } else { cost = 0.55 * numcandy; printf("\nPlease pay $%.2f to the cashier!\n", cost); totalcost = totalcost + cost; numberlollipopps = numberlollipopps - numcandy; } } printf("All the lollipopps have been sold for : $%.2f\n", totalcost); } 4. Pointers and ArraysThe C language takes a lot of flack for its ability to peek and poke directly into memory. This gives great flexibility and power to the language, but it also makes it one of the great hurdles that the beginner must have in using the language. Arrays are very interesting since they can be accessed through pointers or array syntax, that is why they are grouped into the same lesson. With that said, let's get started.All variables obviously have to be stored into memory, but where are they stored? Imagine memory as this big long street with houses on it. Each variable is a house on a street. Each house can hold a number of people (value of variable). But how do you find out how many people (what value is stored) are in a particular house. You have to have some kind of address. Then with the address, you can go to the house and then ask it: How many are there? Then you can get the value of a variable. This is the basic concept behind pointers. This should seem very logical, if not please reread this paragraph tell makes sense. Let us use this new knowledge to examine a couple of statements. int var_x; int* ptrX; var_x = 6; ptrX = &var_x; *ptrX = 12; printf("value of x : %d", var_x);The first line causes the compiler to reserve a space in memory for a integer. The second line tells the compiler to reserve space to store a pointer. As you can notice the way you declare a pointer is simply to add a "*" asterick to the end of the datatype. A pointer is a storage location for an address. The third line should remind you of the scanf statements. The address "&" operator tells C to goto the place it stored var_x, and then give the address of the storage location to ptrX. The fourth line is a bit more complex. An asterick * in front of a variable tells C to dereference the pointer, and go to memory. Then you can make assignments to variable stored at that location. Since ptrX points to var_x, line 4 is equivalent to this command: var_x = 12; Pretty cool, eh? You can reference a variable and access its data through a pointer. Windows and all sorts of programs do this all the time. They hand pointers of data to each other, and allow other applications to access the memory they have. Now you may be wondering, why are pointers so comlpex, or I've heard the using pointers can cause problems. It can, and for those who aren't careful misuse of pointer can do a lot of damage. Suppose that we forget to type in line 3 ptrX = &var_x; when we entered the program. What would happen if we executed it, who knows? Without this line ptrX is never assigned an address. Basically it points to some random data anywhere in memory. If you executed line 4 without line 3. You could get very wierd results. Since ptrX hasn't been pointed to our var_x, maybe its points to system memory, and then you assign a value someplace you shouldn't and your computer crashes. This may not always happen, but it is certainly a possibility, so be very careful when using pointers. Make sure they are assigned to something before you use them. With that basic understanding of pointers, it is time to move to arrays. The most obvious use of arrays would be an array of characters also commonly knows as a string. The following program will make a string, access some data in it, print it out. Access it again using pointers, and then print the string out. It should print out "Hi!" and "012345678" on different lines. The explanation of the code and arrays will follow #include <stdio.h> #define STR_LENGTH 10 void main() { char Str[STR_LENGTH]; char* pStr; int i; Str[0] = 'H'; Str[1] = 'i'; Str[2] = '!'; Str[3] = '\0'; // special end string character printf("The string in Str is : %s\n", Str); pStr = &Str[0]; for (i = 0; i < STR_LENGTH; i++) { *pStr = '0'+i; pStr++; } Str[STR_LENGTH-1] = '\0'; printf("The string in Str is : %s\n", Str); }First off, let's talk about the array notation to declare an array in C, you use [] square braces. The line of the program char Str[STR_LENGTH]; declares an array of ten characters. Basically this is just ten individual chars which are all put together in memory into the same place. An apartment complex in memory to use our pointer metaphor. They can all be access through our variable name Str along with a [n] where n is the element number (apartment number at same address). Also notice that when C declares an array of ten. The elements you can access are numbered 0 to 9. Accessing the first apartment corresponds to accessing the zeroeth element in C. Arrays are always like this, so learn to deal with it. Always count from 0 to size of array - 1. Next notice that we put the letters "Hi!" into the array, but then we put in a '\0' You are probably wondering what this is. If you recall in lesson two on printf, there are special charcter sequences that do special things like "\n" stands for new line. Well time to learn a new one. "\0" stands for end string. All character strings need to end with this special character '\0'. If they do not, and then someone calls printf on the string. Then printf would start at the memory location of your string, and continue printing tell it encounters '\0' So you will end up with a bunch of garbage at the end of your string. So make sure to terminate your strings properly.
The next part of the code to discuss is our pointer access to the string. Just like we learned, I declared a character pointer with an
asterick and gave it the name pStr. I then pointed pStr to the starting address of our character string using the line
pStr = &Str[0];. Now pStr points to the start of our char array Str.
Then I used a for loop, and started at 0, went through 10 elements of the array (STR_LENGTH) and assigned the corresponding value of i.
The line pStr++; may seem a bit confusing. C has a bunch of short cuts to manipulate variables the ++ just means
add one to the variable (in this case it moves the pointer to the next element in the array). The ++ syntax here is equivalent to
pStr = pStr + 1;. After manipulating the string, I terminated it with '\0' and printed it out. That about
does it for pointers and arrays, here are a few quick notes.
You should note that you will see other shortcuts in C like -- (subtracts one) or +=3; (adds three). I won't
bother covering them, since you should be able to figure them out just by looking at them. Another note is that you can make arrays of any
of C's types, I just used char arrays since they seem to be the most common. Here is a sample line to make an array of five integers: int arrayofint[5];.
5. FunctionsThis part should be quite simple compared to the last part. Functions are easy to use; they allow complicated programs to be parcelled up into small blocks, each of which is easier to write, read, and maintain. We have already encountered the function main and made use of printf from the standard library. Now let's look at writing and using our own functions.Calling a FunctionThe call to a function in C simply entails referencing its name with the appropriate arguments. The C compiler checks for compatibility between the arguments in the calling sequence and the definition of the function. When someone writes a function for someone to use that funciton will often be contained in a different C source file. Sometimes, however we may not have access to all the source code for all the functions. This is the case for most standard library functions like printf. However, we still know how to use printf, and the arguments it requires because those arguments are listed in the header file called stdio.h, which we have been including in our programs.Library functions are generally not available to us in source form. Argument type checking is accomplished through the use of header files (like stdio.h) which contain all the necessary information. The most commonly used header files are <stdio.h> -> defining I/O routines <string.h> -> defining string manipulation routines <math.h> -> defining mathematical routines <stdlib.h> -> defining number conversion, storage allocation and similar tasks <stdarg.h> -> defining libraries to handle routines with variable numbers of arguments <time.h> -> defining time-manipulation routinesTo find out more about there header files and the functions they contain you can either by a book about C or visit our Other Resources. In addtion to those header files, we can of course make our own functions and header files. A function has the following layout: return-type function-name ( argument-list-if-necessary ) { ...local-declarations... ...statements... return return-value; }If return-type is omitted, C defaults to int. The return-value must be of the declared type. A function may simply perform a task without returning any value, in which case it has the following layout: void function-name ( argument-list-if-necessary ) { ...local-declarations... ...statements... }Arguments are always passed by value in C function calls. This means that local ``copies'' of the values of the arguments are passed to the routines. Any change made to the arguments internally in the function are made only to the local copies of the arguments. In order to change (or define) an argument in the argument list, this argument must be passed as an address, thereby forcing C to change the ``real'' argument in the calling routine. As an example, consider exchanging two numbers between variables. First let's illustrate what happen if the variables are passed by value: #include <stdio.h> void exchange(int a, int b); void main() { /* WRONG CODE */ int a, b; a = 5; b = 7; printf("From main: a = %d, b = %d\n", a, b); exchange(a, b); printf("Back in main: "); printf("a = %d, b = %d\n", a, b); } void exchange(int a, int b) { int temp; temp = a; a = b; b = temp; printf(" From function exchange: "); printf("a = %d, b = %d\n", a, b); }Run this code and observe that a and b are NOT exchanged! Only the copies of the arguments are exchanged. The RIGHT way to do this is of course to use pointers. Also note that in the above code how the function exchange was prototyped. It was declared with a semicolon, and ZERO statements before the main function. This is called forward declaration. This allows the C Compiler to compile main, without not yet knowing the code for exchange. All it needs to know is what exchange arguments look like. This way we can put the exchange function after our main function. We could have easily put exchange before main and gotten rid of the declaration. The next code segment will fix exchange to use pointers, and move exchange above main to eliminate the need for the forward declaration. #include <stdio.h> void exchange ( int *a, int *b ) { int temp; temp = *a; *a = *b; *b = temp; printf(" From function exchange: "); printf("a = %d, b = %d\n", *a, *b); } void main() { /* RIGHT CODE */ int a, b; a = 5; b = 7; printf("From main: a = %d, b = %d\n", a, b); exchange(&a, &b); printf("Back in main: "); printf("a = %d, b = %d\n", a, b); }The rule of thumb here is that
#include <stdio.h> int addints(int a, int b) { return a+b; } void main() { int a; int b; int sum; a = 6; b = 7; sum = addints(a, b); printf("The sum of a and b is : %d\n", sum); } 6. Other ResourcesThat concludes our tutorial on C, you should have a fairly good handle on all the basics of the C langauge now. There are still quite a few things that such a short tutorial could not cover like structs, file I/O, typedefs, c standard library functions, and more. But we have gathered a few links to some further resources here. Where you can learn more about. We also have two books listed that come highly recommended. Good luck with you adventures as a hero.... I mean programmer. If you have any comments or questions feel free to contact us.
|
|
|||||