INTRODUCTION TO C
Part Two

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: 9/16/95

TYPE DECLARATION STATEMENTS

Any C variable can be made integer, floating, or character using a declaration statement as follows:


In C, characters are represented by single quotes ('). This is to distinguish characters in C programs fom variables. Keep this in mind if you assign character values to character variables.

e.g. char initial='i';

EXAMPLES OF C EXPRESSIONS

ARITHMETIC ASSIGNMENT STATEMENT

Example:

	y=a*b*b;

Upon encountering this statement, the computer evaluates the C expression, assigns the value to the variable and then stores y in a predefined location on the computer stack.

The equal sign (=) should be called the assignment operator.

ASSIGNMENT SHORTCUTS

variable += expression
adds the value of the following expression to the variable and assigns the result to the variable
-=, *=, /= may also be used for subtraction, multiplication, and division respectively.

variable ++ (or -- )
increments (or decrements) the variable by 1 after its value is used in the current expression

++ variable (or -- )
increments (or decrements) the variable by 1 before its value is used in the current expression
You can also combine type declarations and assignment statements. e.g. int x = 5 ;

An Example
int x, y, z ;
x = 10 ;
x += 5 ;
y = x++ ;
z = ++y - x++ ;

What are the final values of x, y, and z?

EASY? Well, not exactly.

DEFAULT INPUT/OUTPUT

The standard form for I/O is:

The underscore corresponds to a conversion specification for the type of variable being scanned or printed.

Any number of variables may be used, by adding extra conversion specifications in the midst of the text. Additional variables are separated by commas.

When scanning input into a variable, the variable name must be preceded by an &.

INPUT EXAMPLE

SEPARATED WITH COMMAS

#include < stdio.h> /* standard header file for I/O */
int
main (void)
{
}

The \n character is the symbol that is used in C to signify a return, or newline. When this character is encountered in the midst of a string, the computer outputs a return and then continues printing out the rest of the characters. If no \n characters are ever used, output will continue on one line until the edge of the screen is reached and wrap-around occurs.

OUTPUT EXAMPLE

printf("Velocity = %f m/s", vel);

AN EXAMPLE

#include
#include /* math header file for atan function*/

int
main (void)
{

    double PI, radius, area;
    FILE *inp, /* prepare for file I/O */ *outp;
    PI = atan (1.0)*4 /* get exact value of PI */
    inp = fopen("in.dat", "r");
    outp = fopen("proj2.dat", "w");
    fsanf (inp, "%lf", &radius);
    area = PI*radius*radius;
    fprintf(outp, "RADIUS = %f\n", radius);
    fprintf(outp, "AREA = %f\n", area);
    fclose(inp);
    fclose(outp);
    return (0);
}