Thread: C tutorial
View Single Post
 
 
Posted 2004-06-26, 11:36 PM in reply to Demosthenes's post "C tutorial"
Chapter 1

Introduction Section 1.1
All c programs are comprised of functions which contain statements. Each function has its own name which it is identified by. A function is simply a set of statements which the computer executes. A statement is the actual command to your computer. Every C statement ends with a semicolon.

A function consists of several parts shown below:

PHP Code:
Retutn-type nameOfFunction(arguments)
{
    
statement 1;
    
statement 2;
    
statement 3;
    ...

Remember that this is not actual code, this is just a short example of what a function would look like.

Return Types Section 1.2

In the above example Return-type declares what type of value a function returns. I will show you in a little bit the different return types in C and exactly what it means to return a value. The nameOfFunction is the identifier tagged on to this function. The name can be more or less whatever you want although there are some rules. First of all it can only contain letters of the alphabet, the numbers 0-9 or a underscore. Furthermore a digit cannot start a function name. Another important thing to keep in mind is that the C language is case sensitive. That means that Function1() and function1() are entirely different functions altogether. Arguments are information that can be passed to a function from the main body of a program. For example, refer back to the hello world program in the introduction. In the function printf(), "hello world" is an argument. Printf takes this argument, runs some processes on the computer, and finally outputs your argument onto the dos screen. Also take note of the curly braces. A function statement list begins with your opening curly brace { and closes with the closing curly brace }.

Main Function Section 1.3

A C program can contain as many functions as you want it to, but it must include main() (at least for all the examples covered in this tutorial. If you ever decide to learn windows programming you will find main() is replaced by winMain().) The main() function is where everything takes place. Every function you write, or every statement must be called within the main() function for it to be executed. At the end of the main function your program will exit.

Variables Section 1.4

A variable, in c, is a named location in memory that holds some data. Each variable you use must be declared. The purpose of a variable decleration is to tell the computer what type of data you will be using, how it should interpret it, and how much memory it should set aside for a specific variable. You only have
to declare a variable once to be able to use it within the block of code you are working in. A variable decleration looks like this:

PHP Code:
data-type variable-name
or

PHP Code:
data-type variable-name someValue
C has 5 basic data types listed below:


char
int
float
double
void


A char variable stores characters (i.e. a, d, 4, $). An int variable can hold a whole number. Floats and doubles both hold fractional numbers represnted by decimal points. The main difference between a float and a double is that the double variable can have twice the precision of a float variable. Void is a special variable-type and this will be explained later in the tutorial.

For example if you want to declare an integer variable named someInteger you would declare it as such:

PHP Code:
int someInteger
The keyword int tells the computer to set aside memory for an integer variable, someInteger is the name you give it and all statements must end with a semicolon so there you have it.

You can also initialize a variable. Intilization of a variable means give it a certain value when you declare it:

PHP Code:
int someInteger 8;
    
char someChar 'a'
Initialization is the basic variable decleration followed by an equal sign and then a value. When intializing a char you must encase the value within single-quotes.

In C the equal sign assigns the variable on the left to whatever value is on the right. Basically if we were to say:

PHP Code:
z=2+3
then z would have the value of 5. Also consider this short program:

PHP Code:
#include <stdio.h>

int main(void)
{
    
int z 5;
    
int x 12;
    
x;
    return 
0;

At the end of this short program z would have the value of 17 because it says that z becomes the value of whatever z was before and x. Since those were 12 and 5 it becomes 17.

Arithmetic Operators Section 1.5

A major part of any computer programming language is altering variables and making calculations with them. C has 5 basic arithmetic operators. Addition is the + sign. Subtraction is the - sign. Multiplication is the * sign. Division is the / sign. Finally % is the modulus. Addition, subtracion and multiplication work the same as they do in normal arithmetic. The division works fine with floating point numbers but with integers, if the two numbers do not divide evenly, it will give u the value of however many whole numbers can go into the divisor. For example if you did 7 divided by 2 it would give you the value of 3. This is why we have the modulus. Modulus only works for integers. It returns whatever the remainder is. 7 % 2 would give you 1.

While performing arithmetic, multiplication, division, and modulus take precedence over addition and subtraction much the same as it is in algebra. You can add parenthesis around your operations to have the computer calculate in the order you want it to. For example 7+2*5 entered into the computer would give you 17, but (7+2)*5 would give you 45. This order of operations also applies to variables.

Preprocessor Directives Section 1.6

You might be wondering what a line such as #include <stdio.h> means. The #include is a preprocessor directive. The #include directive is common to most c programs. stdio.h is a header file which is included in your program. A header file stores functions, and stdio.h has many you will be using. The preprocessor is not really a part of a c program, but it's more like directions to the compiler. For now the only preprocessor directive we will need to use is the #include. Later I will show you another one.

Comments Section 1.7

Many programmers like to add comments to their code. Comments help clarify what their code does. Comments are not compiled, they are simply in your code as documentation for you or whoever might be reading your program. Their are two types of comments, the line comment and the block comment. The line comment is started with two forward-slashes. Anything on that same line in your code will not be compiled. The block comment is started with a forward slash
and an asterisk and ended with an asterisk followed by a forward slash. Anything in between is not compiled. A block comment can take up as many lines as you would like. I have demonstrated both type of comments below:

PHP Code:

#include <stdio.h> // this is a preprocessor directive

int main(void)
{
    
printf("hello"); //i can add as much to this line as I want now and it will do nothing
    // printf("world"); nothing on this line will print because the comment is before printf
    /*
    printf("This");
    printf("does");
    printf("not");
    printf("print");
    I am in the middle of a block comment so write now I can say whatever I want
    I end the block comment on the next line
    */ 
    
    
return 0;

Obviously that is not how comments are used but that should give you an example of how to use each type of comment. All that the program above will do is display hello on your dos screen.

Functions explained Section 1.8

If you are planning to make a c program that is not trivial you will want to write your own functions. In C their are two parts to writing a function: The function prototype and the definition of the function.

The function prototype declares a function much like declaring a variable. It consists of three parts and is put before the main function. The first part of the function is your return type. The return type of your method will be whatever the function returns. For example lets say you have a function that calculate the sum of 2 numbers, and you want to assign the value of the sum to variable x. You would have a statement as such:

PHP Code:
x=sum(2,4); 
Since this is returning an integer value (the sum of 2 and 4) you will want the return type to be int. If you wanted a double return type or a char return type you could also do so using the same method.

The next part of the function prototype is the method name itself. Continuing with our previous example of the function sum you would simply type the name sum. If a function simply performs a set of statements and does not need to return anything you would declare that function as void.

The last part of a function prototypes is its arguments. If you need to pass some information to a function for it to do it's computations you must do so through an argument. Arguments are declared the same way as variables. Again, using our previous example we would declare the arguments as int x, int y. If you plan on using more than one argument you must seperate each argument with a comma. You can also have arguments of different types. If you wanted to do int x, double y you could do so. Functions do not require arguments. If no information needs to be passed to a function you can just type void inside the parenthesis of the function prototype. The whole function prototype of sum put together looks like this:

PHP Code:
int sum(int xint y); 
Ofcourse the computer doesn't know what to do with sum yet. You must define the process a function needs to take below the main method. The first line of a function definition is it's prototype again. After that you have a curly-bracket and you define your function. If your function needs to return a value you put that as the last line of a definition. Then you close your function with a curly bracket. I will write a program that uses our sum function and prints it out on the screen.

PHP Code:
#include <stdio.h>
    
int sum(int xint y);  // function prototype

int main(void)
{
    
int x;  // the variable you will assign the value of sum to
    
x=sum(2,4); // assigns the value of 6 to x
    
printf("%d"x); //prints 6 onto your screen
    
return 0;  // return statement for function main
}

    
int sum(int xint y)  // notice you don't put a semicolon here
{
    
int z;
    
z=x+y;  // z is assigned the value of whatever x + y is
    
return z;  // you return the value of z
}  // end of program 
Conclusion Section 1.9

You should now have a good understanding of how a c program is program is written and its elements. You should understand the concept of preprocessor directives, the main function, and how a function is written. If you have any questions pm me and I will try to help you as best as I can. Any feedback would be appreciated. If you caught me saying something wrong, please let me know. I'm not by any means a master at C, so please excuse me if I have screwed anything up. Also, expect chapter 2 in 1-2 weeks.

Last edited by Demosthenes; 2004-07-21 at 12:35 PM.
Old
Profile PM WWW Search
Demosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to beDemosthenes seldom sees opportunities until they cease to be
 
Demosthenes