Topic : C Tutorial
Author : Ryan Parsons
Page : 1 Next >>
Go to page :


Introduction


If you want to learn how to program in C, you will need to get a compiler from somewhere. You can get them for free online the one I recommend is DJGPP. It is fairly easy to use if you also get Rhide with it. Although it is large and takes awhile to download and set up, it works well.

If you have never been introduced to programming before, then you could be wondering what a compiler is... A compiler takes the program that you write in a language and turns that into the actual application that the computer's processor can read. Basically, it turns the stuff you wrote into a bunch of ones and zeroes. Now the compiler is not what you type your program into, you type the actual program into an IDE(I forget what it stands for). Anyway, Rhide is the IDE for DJGPP, and even though you don't need it, it makes it a lot easier to use if you get it.




Part one
Lesson one


Okay, I'm going to try not to assume that you have DJGPP but it might end up that way anyways. The first program that you make is what is most beginners first program. Simply type this program into your compiler and hit Run, Compile, whatever hit for your compiler.
NOTE: I haven't figured out how to type it in yet and I don't really care, but there are supposed to be < > brackets around stdio.h in the following code and around stdlib.h and time.h when they are used.

     #include stdio.h
     int main(void)
     {
     printf("Hello world!\n");
     return 0;
     }


Alright then, if you typed it in without any mistakes, then it should have printed out Hello world! to the screen. If it didn't, then you need to make sure that you typed everthing correctly. Now you probably are wondering exactly what you just did. Okay, the first line tells the compiler to include the library "stdio.h" in your program. "stdio.h" is the library in C that contains all the standard input and output functions that you will use. The next line indicates that the following is the main function. "printf" is the function that prints out the contents of the quotation marks to the screen. The \n tells the function to put a return to a new line after it prints out Hello world. "Return 0" is just returning a value to indicate that the function has finished successfully.

Lesson two


This lesson will show you the different types of variables that are available in C. The four basic types you will learn about now are int(integers), float (decimals), double (long numbers), and char (characters). Type the following code into your compiler if you want, but it has no direct ouput, so you don't really need to compile it, it is mainly just to show you the different variable types.


     #include stdio.h

     int main(void)
     {
     int integer;
     float decimal;
     double longnumber;
     char character;

     integer = 5;
     decimal = 7.3;
     longnumber = 29347;
     character = 'A';

     return 0;
     }


Okay, this is not as hard as it might seem to you right now. The first part you have already seen. The first new part is "int integer;". This means that you are declaring an integer named integer. Once you declare a variable, you can use that variable anywhere you want to in that program. You can also give any variable you declare any name that you want to, with some limitations. The names should be in all lowercase, with only charcters and numbers and the _. But try to keep your names short and to the point so it doesn't become a pain to type them in all the time. Okay back to integers now; integers are useful, but they do have limitations to them. An integer is usually only two bytes long, which gives it a range from -256 to positive 256 (thats not exact, but the exact numbers are somwhere around there). Integers also can not contain a decimal. Now the next line declares a float. They can be positive or negative also, but they have a larger range of possible values, and they can contain a decimal point. A double is similar to a float in that it can be a decimal and negative, but it is usually able to store a much larger number. A character is just that, it is a single character. Later you will learn how to use characters to make strings. Below the declarations of the variables, each of them is assigned a value; the examples show how to assign a value to each type of variable.

Lesson three


The purpose of this lesson is to show how to display the contents of the different variable types to the screen.

     #include stdio.h

     int main(void)
     {
     int integer = 5;
     float decimal = 7.1;
     double longnumber = 539845;
     char character = 'A';

     printf("The contents of the variable integer are %d\n", integer);
     printf("The contents of the variable decimal are %f\n", decimal);
     printf("The contents of the variable longnumber are %f\n", longnumber);
     printf("The contents of the variable character are %c\n", character);

     return 0;
     }


The first four lines of the declare four different variable types. An integer, a float, a double and a character. It also shows another way of assigning the value of a variable at the same time as the variable is being declared. The next four lines show how to display each variables content to the screen. In a standard printf statement, you use the % operator and then the proper format specifier (%d is used for integers, %f for floats and double, and %c for a character). If you don't know what the \n means (I don't remember if I already told about it) it is the newline character (include it anywhere in the printf statement to have cause the program to return at that point. After you have put the proper format specifier in the place where you want the variables contents displayed, then after the quotations that end what is displayed to the screen, you put a comma, then the name of the variable that is to be displayed to the screen at that point. Another fact that will come in useful, if you want to use multiple variables in one printf statement, then you type it like so:
printf("Variable one is %d, Variable two is %d\n", variable_one, variable_two);

Lesson four


This lesson is designed to show the different ways of modifying the value of a variable.

     #include stdio.h

     int main(void)
     {
     int number = 7;

     printf("Number = %d\n", number);
     number = 8+6;
     printf("Number = %d\n", number);
     number += 3;
     printf("Number = %d\n", number);

     return 0;
     }



The first line of the program declares an integer number, and gives it a value of 7. The next line displays it in a printf statement to the screen so that the compiled program will show that the changes did in fact occur. The next line assigns the value of 8+6 to number, and then displays the result in the next line in a printf statment. The next line might be a little confusing, but it is actually a simpler way of saying- number = number + 3; -The same thing applies to it if you to subtract 3 from number. You could type- number -= 3; - instead of- number = number - 3; -This next line of the program introduces two new concepts: the ability to modify the value of a variable as it is being printed out to the screen, and the ability to increment the value of the varible one at a time using the following two formats:

     printf("Number = %d\n", ++number);
    
     printf("Number = %d\n", number++);


The first example shows how to add one to the value of the variable before anything else in the line of code is performed; therefore, the value of number is

Page : 1 Next >>