Basic constructs (C)

From LiteratePrograms

Jump to: navigation, search
Other implementations: AmigaE | C | C++ | Java | OCaml | Unlambda

This program is under development.
Please help to debug it. When debugging
is complete, remove the {{develop}} tag.


This article describes some basic constructs in the C programming language. This is not a tutorial, just a collection of example code. This page at Google Directory provide links to some C tutorials.

Contents

Loops

There are 3 standard ways to create a loop in C.

The for-loop is the most commonly used.

The following example will print the numbers from 0 to 9 on separate lines.

<<forloop>>=
	for(n=0; n<10; ++n) {
		printf("%d\n", n);
	}

Here is an example of a while-loop. The output is exactly the same as in the for-loop example.

<<whileloop>>=
	n=0;
	while(n<10) {
		printf("%d\n", n);
		++n;
	}

The do-while-loop works like the while-loop, except the test expression is evaluated after the code block.

<<dowhileloop>>=
	n=0;
	do {
		printf("%d\n", n);
		++n;
	} while(n<10);
<<loops>>=
void loops()
{
	int n;
forloop
whileloop
dowhileloop
}

Note: Sometimes infinite (never ending) loops might be useful. You can write them as for(;;) , while(1) . For embedded systems where wasted CPU cycles are undesirable there might be a small speed advantage in using the for(;;) as it causes no evaluation. A (do{ }) while(1) will evaluate the expression depending on how smart the compiler is. It might be worth it to check the difference in the assembly code generated for the two different loops.

Simple I/O

Most programs dealing with input/output include the stdio.h header file.

<<include stdio.h>>=
#include<stdio.h>

Standard input/output

getchar() is used to read one character from the standard input stream (stdin). It returns EOF when the end of the file is reached. We use putchar() to write to the standard output stream (stdout). The following code will read characters from stdin, and write them to stdout.

<<stdio>>=
	while((ch=getchar())!=EOF) {
		putchar(ch);
	}

File reading writing

Before reading from or writing to a file, fopen() is used to open it. There are lots of functions able to read from and write to files. We choose fgets() and fputs(). This code will read the first line of the file input.txt and write the same line to output.txt

<<fileio>>=
	if((fp=fopen("input.txt", "r"))==NULL) {
		fprintf(stderr, "Cannot read \"input.txt\"\n");
		return;
	}
	line[79]='\0';
	fgets(line, 80, fp);
	fclose(fp);
	if((fp=fopen("output.txt", "w"))==NULL) {
		fprintf(stderr, "Cannot write \"output.txt\"\n");
		return;
	}
	fputs(line, fp);
	fclose(fp);


<<io>>=
void io()
{
	FILE *fp;
	char line[80];
	int ch;
stdio
fileio
}

Command line arguments

The argc argument to main() is the number of arguments specified when invoking the command. This number includes the command itself. argv is an array of the arguments itself. When wrong arguments are specified, programs normally display an error mesage, along with a usage message showing the allowed arguments.

<<basic_constructs.c>>=
#include<stdio.h>
include stdio.h
#include<string.h>
#include<stdlib.h>
loops
io
void usage(const char *cmd)
{
	fprintf(stderr, "Usage %s loops|io\n", cmd);
	exit(1);
}
int main(int argc, char *argv[])
{
	if(argc<2) {
		fprintf(stderr, "ERROR: Wrong number of arguments\n");
		usage(argv[0]);
		return 1;
	}
	if(!strcmp(argv[1], "loops")) loops();
	else if(!strcmp(argv[1], "io")) io();
	else {
		fprintf(stderr, "ERROR: Unknown command: \"%s\"\n", argv[1]);
		usage(argv[0]);
	}
	return 0;
}

See also

Views