Thursday, December 15, 2011

CWEB, Part 2: Hello World returns!

Continuing from our previous post on CWEB, we will take the time honored example:
lets consider a more complicated "Hello World!" Program.

\def\title{Hello World, Reloaded}

@*A Simple Example.
This is a trivial example of a \.{CWEB} program.
It is, of course, the classic "hello, world"
program we all know and love:

@c
@<Header files needed by the program@>@;
@#
main(void)
{
   @<Print the message |"hello, world"|@>@;
}

@ Naturally, we use |printf| to do the dirty work:

@<Print the message |"Hello, World!"|@>=
printf("Hello, World!\n");

@ The prototype for |printf| is in the standard
header, \.{<stdio.h>}.

@<Header files needed by the program@>=
#include <stdio.h>

@*Index.

This is perhaps the simplest example demonstrating how to use chunk identifiers (those @<...@> things) in CWEB.

Note that \.{CWEB} typesets CWEB using typewriter font.

Also note that we don't have to define a chunk before we use it. That's what we did in this example, all the chunks were defined after they were used.

What are those @; symbols used for? Well, they're for formatting, and they don't do anything other than prettyprint the TeX output.

No comments: