  The Coding Sheets
    Issue Number 1 - 06/07/1995
   An Introduction to C

Notes - if you know even a little C, this will probably not help you.  It is
a very basic, practical approach and does not go into much theory.  It is 
also mainly intended for Borland C.  But it should help a beginner start to 
start in C.  Also, I HIGHLY recommend getting a copy of the Borland C Library 
Refrence.  You will be highly limited without it.  

Have fun. 

The C language in itself is basically nothing.  You need functions, usually 
in the form of include files, to provide even basic functionality.  Include 
files usually have an extension of ".H"(in C++, ".HPP").  For example -

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

These are three commonly used includes.  If you write your own includes,
it is standard procedure to include them like this -

#include "myfile.h"

Including a file is just like cutting and pasting the file into your code.
It is MUCH easier and more powerful, however.

C is very powerful because of it's use of functions.  The basic syntax for
a function is this -

x name(y var, z var2...){
    body of function
}

x is what the function returns.  It is often an int(integer), void(returns
nothing) or char(character).  name is the name of the function.  y and z 
are types (int, char, unsigned, etc.).  They tell the compiler what var and
var2 are.  If you have no variables, simply put "void".  The body of every 
function must be contained by curly braces ({ and }).  The body will consist 
of calling other functions, or whatever. 

The most important function in any program is main.  It is the main body
of the program.  It is called when the program is first run.  All functions
in the program must either be before main in the code, or declared before
main in the code.  Declaring a function is basically just not including the
body of the code.  For example -

int bozo(void);

You may have noticed the semicolon.  All statements in C must be followed by
a semicolon.  This is probably the most common mistake by programmers, and
is a pain to track down.

Declaring variables in C goes like this -

x *name[num]=whatever;

x is the type.  It is the same as for a function.  The * represents a
pointer.  I will not go into this concept in depth, but it is a pointer
to an address in memory.  For simple purposes, it is not usually used.
num, if it is present would make an array.  whatever is whatever you want
to set it equal to.  It is the same as saying - 

x name;
name=whatever;

Examples -

int stupid=3;
char blah[80]="This is an array of chars.";
unsigned int x;

A variable outside of all curly braces is global, and can be used in any
function.  Otherwise, it is local, and can be used only in the set of curly
braces that it is in.

There are two types of comments in C.  For one line - 

//comment, blah, blah, ignored.

For many lines

/* k-comment
blah, blah
hdhs*/

Comments can be nested.

The basic loops in C are a for loop, and a (do)...while loop.  A for loop
goes like this -

for(start;finish;do){
    body;
}

start is what to do before the loop starts (for example, i=0).  finish is
until when to do the loop (for example, i<=10);  do is what to do (for
example, i++(++ adds one to the variable(it is the same as i=i+1),
-- subtracts one from the variable (i=i-1))).  Body is what is executed each
time.  A while loop goes like this - 

while(i<num){
    body;
}

the preferred syntax is -

do{
    body;
}while(i<num);

These both do the same thing. 

Another important thing in C, and the last which I will cover, is the if
statement.  It goes like this - 

if(something){
    body;
}

For example - 

if(i==1){
    body;
}

In this case, it is important to use TWO ='s.  This compares the variables.
if(i=1) would set i equal to 1, and would thus always be true.  if(i==1)
is true only IF i=1.  Other things which may be helpful in if statements - 

!=   - not equal
>    - greater than
<    - less than
>=   - greater than or equal to
<=   - less than or equal to
&    - and (if you are familiar with logic, this is useful)
|    - or (& and | are especially useful for hexidecimal)

Ok, I'll leave you with an example, a twist on a classic.  It will introduce
you top the function printf(printf formatted), which prints text to the
screen.  The "\r\n" is a carriage return/linefeed, which is not normally 
appended by printf.


-Pluto

-----cut here----
//Hello, world!@

#include <stdio.h>

void hello(void){
    printf("Hello, world!\r\n");
}

int main(void){
    int i;
    for(i=1;i<10;i++){
        hello();
    }
    return 0;
}

//end of program


  You can contact The Coding Syndicate at :                               
    Virtual Utopia 303-695-9287                                           
  We need distros, coders, artists and musicians. Please contact us. NOW! 

