Notes From the First Lecture on C


Programming in `C'

  Like Pascal, `C' is an imperative language in the Algol tradition.

  There are many minor syntactic differences between Pascal and `C', and
  a number of more major differences.

  In general, `C' can do everything Pascal can do (but not vice-versa).

  `C' is widely used as a systems programming language.  Allows access
  to low-level machine details.


Resources

  Tim Love's ANSI C for Programmers in UNIX Systems (handout).

  Christopher Sawtell's `C' Language Course (07.211 WWW page)

  On-Line C Help File (keyword help) on WWW page.

  comp.lang.c newsgroup.

  FAQ on WWW page (mainly for experienced programmers).


Just syntax
  
  begin and end become { and }
  Assignment is =; comparison is ==
  Semicolon is a statement terminator (not separator)
  Procedures cannot be nested in `C'
  There are no strings in `C' (sic.)
  Variable declarations look like int a, b, c;
  record is called struct
  Variant record are called unions
  
  

Statement syntax
  A Pascal for loop looks like

	for( i = init; i < limit; i = i + 1 )
	    stmt;

  A Pascal while loop looks like

	while( cond ) stmt;


  A Pascal until loop looks like


	do {
	    stmt;
	} while( cond );



A Sample `C' Program

	#include <stdio.h>  /* For puts() */
	main( ) {
	    int i;
	    char *name = "07.211";
	    for( i = 0; name[i] != '\0'; i++ )
	        puts( &name[i] );
	}
  
  Even standard library functions are optional
  Initialised data
  String conventions
  Auto-increment
  Explicit dereferencing



Running `C' on Unix

  Use cc (DEC's `C') or gcc (GNU's `C')

	gcc -Wall -g file.c -o file

  Produces executable file, or errors.  To run file, do

	./file ...any args go here...

  To debug under Emacs (recommended), use M-x gdb RET file RET.
  Set breakpoint in file.c with C-x SPC.  Single step in
  *gbd* with C-c C-s (next statement) or C-c C-n
  (next line).  See ``Debuggers'' on Emacs INFO page (C-h i) for
  details.


`C' can; THINK-Pascal cannot

  Initialise data:

	char *name = "07.211";
  
  Side-effect expressions:

	if( (fd=fopen(file,"r")) == NULL )
	f(i++);

  Pointer arithmetic:

	char *name = "07.211";
	name[1] = *(name + 4);


Literals

  Character values (8-bit integers) are written in single quotes.  The
  backslash introduces a special:

    'A'     printable ASCII 
    '\010'  octal 
    '\x41'  hexadecimal 
    '\n'    line separator 
    '\f'    form feed 
    '\e'     escape
  
  Writing integers: 15L (long); 015 (octal); 0xF7
  (hexadecimal).

  Writing floats: 15.3e3F (single precision); 15.3e3
  (double); 15.3e3L (long double).

  There are no booleans (0=false; non-zero=true).


Arrays

  The first index into an array is always 0.  Declaration gives number
  of elements only.

  There is no subscript checking!

  Declaration syntax: char letters[50];

  Multi-dimensional arrays: char values[50][30][10];

  Index with values[x][y][z].



Structures (records)

  Example:


	struct person {
	    int age;
	    int height;
	    char surname[20];
	} fred, jane;

  struct person is a new record type.

  fred, jane are variables of that type.

  Can refer to fields with fred.age.

  Note: cannot compare struct values.



Unions (variant records)

  Replace struct with union.

  All elements overlay in memory.

	union Cell {
	    long int i;
	    char *sp;
	    double f;
	    void *mem;
	};  


Typedef
  Can use typedef to define type synonym.


	typedef struct {
	    double real, imag;
	} complex;

  Then, use complex z0, z1;

  Typedef does not create any variables.



Selection

	if ( i == 3 )
	    j = 4;
	else
	    j = 5, k = 6;

  Multi-way:

	switch( i ) {
	   case 1:  puts("i is 1"); break;
	   case 2:  puts("i is 2"); break;
	   default: puts("neither");
	}

  Note: cases act like labels; break is needed to avoid
  fall-thru.



Loops

	while( i < 30 ) {
	   stmt;
	}
 
  Can use break to exit inside loop, or continue to jump
  to start of loop.

  For-loops

	for( expr1; expr2; expr3) {
	   stmt;
	}

  Really just a while loop:

	expr1;
	while( expr2 ) {
	    stmt;
	    expr3;
	}

  Except continue jump to expr3.