INTRODUCTION TO C
Part One

TOPICS

CREDITS

Authored by: John Even

Faculty Supervisor: Al Day

Some material is derived from a lesson by: Prof. Martha Selby

HTML documentation by: Michelle Roberts

Last updated 8/16/95

HOW TO INSERT COMMENTS

Comments are put into the code using /* and */. /* marks the beginning of the comments, while */ marks the ending. All text between /* and */ is ignored by the compiler.

     e.g.   int n;	/* declare n to be of type int */  
	    /* now the looping process will begin */
	    for (n=0; n<5; n++) { printf("Oh yah.\n"); } /* end of looping process*/ 

LAYOUT

The general format of a C program looks like:


INCLUDES

Include statements are a form of preprocessor derective.

Include statements are used as follows:

e.g. #include &ltstdio.h>

These statements cause the preprocessor to replace the #include statement with the code from the corresponding header file.

Stdio.h

stdio.h is the header file which must be included for all programs which are going to have either input from a file or keyboard or output to a file or monitor.

There are many other standard C header files which will be discussed later.

DEFINES

Define statements are also a form of preprocessor directive.

Define statements are used as follows:

e.g. #define PI 3.14159

These statements cause the preprocessor to replace all occurences with the NAME in the #define statement with the replacement string.

PI can now be used as a constant macro (very similar to a variable) throughout all code that follows the #define statement.


It is considered good practice to use all uppercase letters in the names of constant macros. C is case-sensitive.

MAIN FUNCTION

int
main (void)
This marks the start of the main function. Lines following this statement form the body.


body
This is composed of declaration parts and executable statements. The body begins at the { right after the main statement and ends with a return (0); and a }, marking the end of the program.
A semicolon (;) must follow every statement in the body of a function. This tells the compiler that the statement is finshed, allowing it to move to the next statement.

C NUMBERS

  1. Integer
  2. Floating

INTEGER NUMBER

An integer number:
  1. Contains no decimal point
  2. Contains no fractional values
  3. Generally used as counters in looping processes
  4. Computer truncates and reduces results to next lower integer in an integer division
    (4/3=1, 17/5=3, etc.)

FLOATING NUMBER

A floating number:
  1. Contains a decimal point
  2. May be expressed in either of two forms
    • Without an exponent
      (eg 123.4)
    • With an exponent
      (eg 1.234e+002)

C VARIABLES

C variables are quantities that are given names & whose values can vary during program execution.

Three types of variables exist:

  1. Integer variables
  2. Floating variables
  3. Character variables
    (store a single letter or character)

C VARIABLE RULES

  1. The first character of a C variable cannot begin with a digit !
  2. Subsequent characters may be any combination of letters digits and underscorers. Special characters are not allowed!
  3. No more than thirty-one (31) characters may be used in a variable name.
  4. Blank spaces are not permitted!
  5. Only one name may be used for each C variable!
  6. Before a variable name can be used in computation, it must be assigned a value!
  7. C reserved words cannot be used as identifiers.
    (int, double, char, etc)