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.



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.

	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.

	while(n<10) {
		printf("%d\n", n);

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

	do {
		printf("%d\n", n);
	} while(n<10);
void loops()
	int n;

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>>=

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.

	while((ch=getchar())!=EOF) {

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

	if((fp=fopen("input.txt", "r"))==NULL) {
		fprintf(stderr, "Cannot read \"input.txt\"\n");
	fgets(line, 80, fp);
	if((fp=fopen("output.txt", "w"))==NULL) {
		fprintf(stderr, "Cannot write \"output.txt\"\n");
	fputs(line, fp);

void io()
	FILE *fp;
	char line[80];
	int ch;

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.

include stdio.h
void usage(const char *cmd)
	fprintf(stderr, "Usage %s loops|io\n", cmd);
int main(int argc, char *argv[])
	if(argc<2) {
		fprintf(stderr, "ERROR: Wrong number of arguments\n");
		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]);
	return 0;

See also