General Discussions

Literate Programming

If you really want to get the story of the code you write, follow literate programming for the initial few days. There is a high probability that you might continue once you are used to it which is obviously a very good programming practice.  Literate programming was introduced by Donald Knuth. His idea was to write a program as the thoughts flow.  However the syntax of the program hinders that thought process.

Knuth suggested writing the story of the code as the thoughts flow and embedding suitable code snippets in it. You get the whole program if you pull out only the source code from the story.  Let us see a simple example of how to do literate programming for adding two numbers in C programming language.

#+Title: Program to Add Two Integer Numbers
#+Author: Prakash B H
#+Date: 31 May 2016

* Introduction
This document explains how to write a program to add two numbers using C programming language. The program is written using the literate programming technique. The documentation is carried out using the =emacs= editor in =org= mode. The code can be extracted from the document using the =tangle= commands.

* Program to Add Two Numbers
We begin the program by including the necessary headers which will bring together all the required libraries.

#include <stdio.h>
#include <stdlib.h>

We next write a module to add two numbers. We pass the two numbers as parameters to the method. We perform the addition operation on the supplied two numbers and return the result.

int add_numbers(int num1, int num2)
int result = 0;
result  = num1 + num2;
return result;

The next task is to write the main function which initializes the required variables and calls the function to add the two numbers. Let us put down the initialization part.

int main()
int num1 = 10;
int num2 = 10;
int result = 0;

Next we call the function where the variable =result= captures the result.
   result = add_numbers(num1, num2);

We then finally print the =result= to check if the operation was performed correct.
   printf(“The result of addition is %d\n” result);

Closing of the main function
   return 0;

* Conclusion
The story above presented a C program to add two integer numbers.

When we tangle this story, what we get is everything and only those which are embedded between



The ‘C’ tells that it is a C program. We then have our C program ready to compile and run. By writing the story we actually capture all the details of the program. Following this method, needless to say the readability and maintainability of the code increases. You can give this method a try!

Learn More! Check out:


Design of a Programmer


One thought on “Literate Programming

Let me Know What you Think!

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s