Free C Lessons!!!  Yah!


   The first thing that people usually do when learning a language is
take a look at a simple example of what a program looks like.  A program
that prints "Hello World!" on the screen is of the norm.  ;)
 
   Probably before you type in the program, you'll want to find yourself
a C compiler.  For people who don't have C compilers available in a shell
account or at home, yet have 386s or better, I might be able to find a
version of gcc that you can use.
 
----------------- hello.c ------------------------
 
/* hello.c - A program to display "Hello World!" on the screen. */
#include <stdio.h>
 
int main(void)
   {
   printf("Hello World!\n");
   getchar();
   }
 
--------------------------------------------------
 
   I'll put the stuff you're supposed to type in between the ---- lines.
I'll also explain what some of the things in the program are at the end.
For this lesson, you should probably read all of the comments, but don't
worry about getting confused, because the question board is upstairs and
I'll elaborate later on.
 
   For starters, I'll explain how to make comments in C.  If you notice the
'/*' and '*/', you probably notice that there is no code in between them..
well, there isn't .. and that's because '/*' starts a comment and '*/' ends
a comment.  Comments can take up part of a line or many lines (there will
be more examples in the future).
 
   Next you'll probably notice the '#include <stdio.h>'.  Right now I'll
just say that the '#include' part tells the compiler to include the text
file 'stdio.h' (from a special folder called include) in the C program
during compilation (more on this later).
 
   Now, just as you start saying, "Ah, he's just like the old books!"  I'm
sure you probably will have to agree that reading has to take some time.  ;)
The line 'int main(void)' defines a function (more on this later).  This
happens to be the first function that is run in the C program and lives up
to it's name 'main'.  :)
 
   The '{' and '}' help contain a code segment (more on this later).  For
now we'll just say all functions need to have their code surrounded by them.
 
   The command 'printf("Hello World!\n")' prints off "Hello World!" and the
semicolon after it (as well as the semicolon after the command 'getchar()')
tell the compiler that that is the end of that command (more on this
later).
 
   The command 'getchar()' is to allow you to press the return or enter key
to continue (just in case someone is using an ide like Borland C/C++).


Lesson by Joshua Cantrell

Lesson 2:  Comments in C

   To start off small, I've decided to introduce comments.  Remember the
Hello World program?  I had one comment in it at the very beginning.  Below
I'm going to put comments all over the Hello World program, yet it won't
change anything the program does, it will only change how it looks.

---------------------------- comments.c ---------------------------

/* comments.c to show how comments work. */
#include <stdio.h>    /* This puts the file stdio.h in the code during
compilation..  (more on this later.) */

int main(void)    /* The main function! */
   {
   printf("Hello World!\n");      /* This prints "Hello World! on the screen */
   /* This waits until the user presses the return or enter keys. */ getchar();
   }

-------------------------------------------------------------------

   Notice that the comments can go anywhere as long as they surround the
text meant as a comment.  They can wrap lines, go at the end of a command,
go before a command or anywhere, except in the middle of a command like
"pri/* a comment */ntf("Hello World!");" and you can't put a comment within
a comment like "/* a comment /* another comment */ */" because it doesn't
seem to like two "/*" in a row.  ;)


Lesson by Joshua Cantrell

Lesson 3:  What are those braces and semicolons for?

   Well, I don't ever recall being told what those braces are for....
They have a general purpose which I'm going to say is called defining a
special grouping of code.  The brackets surrounding an area always
tell the compiler to execute the surrounding commands together.  For example,
in the previous programs, all of the commands between the '{' and '}' are
executed in a group.  I could put another group of commands in that group
by adding more braces and sticking some commands in there.  Like so...

----------------- braces.c ------------------------
 
/* braces.c - A program to display "Hello World!" on the screen. */
#include <stdio.h>
 
int main(void)
   {
   printf("Hello World!\n"); 
      {
      printf("Looky!  This is in it's own grouping of code!\n");
      printf("Still in the same group!\n");
      printf("Still there!\n");
      }
   getchar();
   }
 
--------------------------------------------------

   My convension for putting braces on the screen is to indent them by three
characters going into each sub grouping (if you don't understand... hopefully
you can learn by example), however you can place them like you can the
comments and they can appear anywhere as long as they surround a bit of
code (or no code, you can make it so they just surround nothing as well).

   Looking ahead you might think... wait a minute?  Shouldn't those commands
go in a nice orderly fasion down the screen?  Yes, if you want other people
to read it you should...  :)  The semicolons are used to identify the end of
the line for that command.  This is why I can put two printf commands on the
same line and why I can spread out the printf commands for quite a few lines,
because the compiler doesn't know the command is done with until it hits
the semicolon!  So remember, always put a semicolon after a command, or the
compiler might get confused!   Here's the example:

----------------- semicoln.c ------------------------
 
/* semicoln.c - A program to display "Hello World!" on the screen. */
#include <stdio.h>
 
int main(void)
   {
   printf("Hello World!\n"); 
      {
      printf(
         "Looky!  This is in it's own grouping of code!\n"
);
      printf("Still in the same group!\n");  printf(
                           "Still there!\n"
                        );
      }
   getchar();
   }
 
--------------------------------------------------


Lesson by Joshua Cantrell

Lesson 4a:  Simple variable types... the character.

   In all most higher level programming languages like C, you have variables
where you store information.  There are always those variables that are more
complex than others, and I'll try to explain the simple ones that are found
in C.
   Variables are defined by using an identifier (a shortened version of the
name) placed before the name of the variable.  All variables must be placed at
the beginning of areas with braces (eg., see Lesson 3).  Variable names are
case sensitive, so the variable "cat" does not equal "Cat" or "CaT".  You are
safely withing ANSI C if you use only alphabet characters, underscores, and
numbers in your variable names.  The variable names can't have numbers in
the front, but can have numbers within or at the end.

Correct Variable Names:
Spacy   vErBos   Exp2_30   T90sx gs_cat

Incorrect Variable Names:
45gemini   ^Ooof   *Ahhhh   )--->


   You put values in variables by using the equal sign '=' after the name
of the variable and before the value.  Don't forget to put the semicolon
at the end of the statement!  ;-)


character - This is typically a character as in the alphabet, but also
    includes anything that is displayed on the screen.  It is defined by
    putting single quotation marks around the character.  Its identifier
    is "char".

Examples of defining characters:
char character;
char char_temp2;

Examples of puting values in characters:
character = '^';
character = '9';
character = '#';
char_temp2 = '%';

   All characters have numerical values, but you can't type them all in
from the keyboard, so C made ways for us to add them without having to
memorize the exact values.  The names of some of these and their
C equivalents:

newline ----------------> '\n'
carriage return --------> '\r'
delete character -------> '\d'
tab key ----------------> '\t'

   Some characters used to define the boundaries of a string (more on this
type of complex variable later) and character, the single and double quotation
marks, also need the backslash to be placed in front of them for the C
compiler to see that they do not start or end a string or character.  In order
to make a backslash you have to put a backslash in front of it so C knows
it's not a predefined character like the newline and carriage return defined
previously.  Examples:

single quotation mark --> '\''
double quotation mark --> '\"'
backslash --------------> '\\'

   The numbers that represent characters can also be used in place of the
character surrounded by the single quotation marks.  Normal characters can
carry the values from -128 to 127 in signed form and 0 to 255 in unsigned
form (more on signed and unsigned in next Lesson).  This also allows
characters to be treated as really small numbers.  Some examples of defining
characters as numbers:

character = 20;
char_temp2 = -57;    // If char_temp2 is a signed variable.
char_temp2 = 200;    // If char_temp2 is an unsigned variable.

Examples of some numbers that represent characters:
'A'  <----------> 65
'a'  <----------> 97
'1'  <----------> 49
'^'  <----------> 94
'\\' <----------> 92

   A main function with character variables in it would look like so:

main()
   {
   char variable;
   char variable2;
   variable = 'x';
   variable2 = '4';
   }

   You have to have the variable types defined in the beginning, so the
following won't work:

main()
   {
   char variable;
   variable = 'x';
   char variable2;
   variable2 = '4';
   }

   In order to make an unsigned character, you just add unsigned to the
beginning making the identifier look like "unsigned char" like so:

main()
   {
   unsigned char variable;
   char variable2;
   variable = 'x';
   variable2 = '4';
   }

--------------------------------------------------


Lesson by Joshua Cantrell

joshuacantrell@yahoo.com
(My Home Page) - (My Personal Page) - (My Computer Science Page) - (My Resume)