Quick links

Intro
Compiler
some Windows Shell commands
A second program
Input / Output examples
Funcion example
Character Arrays
Things to try

Assignments:
     HW1 (given in class)
     HW2 and Project 1
     Project 2 and sample input
     Project 3
     Project 4
     Project 5


Intro

You should find a few references to use throughout the course. If you prefer a hardcopy, "The C Programming Language -- ANSI C" by Kernighan and Ritchie is a good text. Online resources will also be quite helpful. Among many others, there are good C tutorials from Drexel and Oslo (in Norway).


Compiler

TinyCC install instructions:

  1. Go to the TinyCC website. Click the Download link, and choose the correct file. I tested "tcc-0.9.26-win32-bin.zip"
  2. Open the zip file and copy the tcc folder to your USB drive (or extract the zip to your USB drive).

Using TinyCC compiler in the lab (similar instructions should work on your own computer, but it may be a little different):

  1. With your USB drive plugged into the computer, open an existing folder on your USB drive. Right click to bring up a menu for any file or folder on your USB drive, and click properties. Make a note of what is listed under "Location." It should begin with something like \\tsclient\D -- if your drive shows a letter other that D change the following instructions to whatever letter is listed.
  2. Click the Window's Start menu. Click "Run...". Type "powershell" and click ok.
  3. type: cd \\tsclient\D
    This changes the directory to your USB drive. Note: if your home computer assigns a drive letter to your USB drive (I'll use drive X for example), then change this to type: cd x:
  4. type: mkdir cps125
    This creates a directory named cps125.
  5. type: cd cps125
  6. type: mkdir day1
  7. type: cd day1
  8. type: notepad hello.c
  9. In this new text file we will write our first program. Type the following exactly:
    #include <stdio.h>
    
    main()
    {
    	printf("hello, world\n");
    }
    
    Save the file and either close the notepad or change to the powershell window.
  10. type: \\tsclient\D\tcc\tcc hello.c
    This compiles your source file "hello.c" and creates an executable file "hello.exe". It may take a few seconds. Note: if your home computer assigns a drive letter to your USB drive (I'll use drive X for example), then change this to type: x:\tcc\tcc hello.c
  11. To run "hello.exe" (that is in the current directory), type: .\hello

some Windows shell commands

cdchanges the directory
copycopies one or more files to another location
deldeletes one or more files
dirdisplays the files and subdirectories in the current directory
helpdisplays help for various commands
mkdircreates a directory

A second program

Create a new file named "tempTable.c" and add the following to it. This is from Chapter 1 of the K&R text.
#include <stdio.h>

/* print Fahrenheit-Celsius table
    for fahr = 0, 20, ..., 300 */
main()
{
	int fahr, celsius;
	int lower, upper, step;

	lower = 0;	/* lower limit of temperature table */
	upper = 300;	/* upper limit */
	step = 20;	/* step size */

	fahr = lower;
	while (fahr <= upper ) {
		celsius = 5 * (fahr - 32) / 9;
		printf("%d\t%d\n",fahr,celsius);
		fahr = fahr + step;
	}
}

Input / Output examples

From section 1.5.1 of K&R text:
#include <stdio.h>

/* copy input to output; 1st version */
main()
{
	int c;

	c = getchar();
	while (c != EOF) {
		putchar(c);
		c = getchar();
	}
}


From section 1.5.4 of K&R text:
#include <stdio.h>

#define IN  1
#define OUT 0

/* count lines, words, and characters in input */
main()
{
	int c, nl, nw, nc, state;

	state = OUT;
	nl = nw = nc = 0;
	while ((c = getchar()) != EOF) {
		++nc;
		if (c == '\n')
			++nl;
		if (c == ' ' || c == '\n' || c == '\t')
			state = OUT;
		else if (state == OUT) {
			state = IN;
			++nw;
		}
	}
	printf("%d %d %d\n",nl,nw,nc);
}


From section 1.6 of K&R text:
#include <stdio.h>

/* count digits, white space, others */
main()
{
	int c, i, nwhite, nother;
	int ndigit[10];

	nwhite = nother = 0;
	for (i = 0; i < 10; ++i)
		ndigit[i] = 0;

	while ((c = getchar()) != EOF)
		if (c >= '0' && c <= '9')
			++ndigit[c-'0'];
		else if (c == ' ' || c == '\n' || c == '\t')
			++nwhite;
		else
			++nother;

	printf("digits =");
	for (i = 0; i < 10; ++i)
		printf(" %d", ndigit[i]);
	printf(", white space = %d, other = %d\n",
		nwhite, nother);
}

Function example

From section 1.7 of K&R text:
#include <stdio.h>

int power(int base, int n);

/* test power function */
main()
{
	int i;

	for (i = 0; i < 10; ++i)
		printf("%d %d %d\n",i,power(2,i),power(-3,i));
	return 0;
}
/* power: raise base to n-th power; n >= 0 */
int power(int base, int n)
{
	int i, p;

	p = 1;
	for (i = 1; i <= n; ++i)
		p = p * base;
	return p;
}

Character Arrays

This is from 1.9 of the K&R text.
#include <stdio.h>
#define MAXLINE 1000    /* maximum input line length */

int getline(char line[], int maxline);
void copy(char to[], char from[]);

/* print the longest input line */
main()
{
	int len;		/* current line length */
	int max;		/* maximum length seen so far */
	char line[MAXLINE];	/* current input line */
	char longest[MAXLINE];  /* longest line saved here */

	max = 0;
	while ((len = getline(line, MAXLINE)) > 0)
		if (len > max) {
			max = len;
			copy(longest, line);
		}
	if (max > 0)	 /* there was a line */
		printf("%s", longest);
	return 0;
}

/* getline: read a line into s, return length */
int getline(char s[],int lim)
{
	int c, i;

	for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
		s[i] = c;
	if (c == '\n') {
		s[i] = c;
		++i;
	}
	s[i] = '\0';
	return i;
}

/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
	int i;

	i = 0;
	while ((to[i] = from[i]) != '\0')
		++i;
}