Home

Awesome

Little C <!-- omit from toc -->

The Little C Interpreter from Herbert Schildt's C: The Complete Reference (4th Ed.)

Table of Contents <!-- omit from toc -->

Project Structure

The files for this project are in the following directories:

.
├── bcb3                  # Borland C++ Builder 3.0 Project Files
├── bcc55                 # Borland C++ Compiler 5.5 Makefile
├── clang                 # Clang C Compiler Makefile
├── dev-cpp               # Dev-C++ 4.9 Project Files
├── gcc                   # Gnu C Compiler Makefile
├── qc25                  # Microsoft Quick C 2.5 Makefile
├── src                   # Project Source Code
├── vc152                 # Visual C++ 1.52 Project File
├── vc2                   # Visual C++ 2.0 Project File
├── vc42                  # Visual C++ 4.2 Project File
├── vc5                   # Visual C++ 5.0 Project File
├── vc6                   # Visual C++ 6.0 Project File
├── vs2002                # Visual Studio .NET Project File
├── vs2003                # Visual Studio .NET 2003 Project File
├── vs2005                # Visual Studio 2005 Project File
├── vs2008                # Visual Studio 2008 Project File
├── vs2010                # Visual Studio 2010 Project File
├── vs2012                # Visual Studio 2012 Project File
├── vs2013                # Visual Studio 2013 Project File
├── vs2015                # Visual Studio 2015 Project File
├── vs2017                # Visual Studio 2017 Project File
├── vs2019                # Visual Studio 2019 Project File
├── vs2022                # Visual Studio 2022 Project File
├── watcom                # Open Watcom 1.9 Project File
├── xcode                 # Apple Xcode Project File
├── LICENSE.md
└── README.md

Specifications

The following features are implemented:

Restrictions

The targets of if, while, do, and for must be blocks of code surrounded by beginning and ending curly braces. You cannot use a single statement. For example, code such as this will not correctly interpreted:

for(a=0; a < 10; a=a+1)
  for(b=0; b < 10; b=b+1)
    for(c=0; c < 10; c=c+1)
      puts ("hi");

if(a=c)
  if(a=b) x = 10;

Instead, you must write the code like this:

for(a=0; a < 10; a=a+1) {
  for(b=0; b < 10; b=b+1) {
    for(c=0; c < 10; c=c+1) {
      puts("hi");
    }
  }
}
if(a=c) {
  if(a=b) {
    x = 10;
  }
}

Prototypes are not supported. All functions are assumed to return an integer type (char return types are allowed, but elevated to int), and no parameter type checking is performed.

All local variables must be declared at the start of a function, immediately after the opening brace. Local variables cannot be declared within any other block. The following function is invalid:

int myfunc()
{
  int i; /* this is valid */
  if(1) {
    int i; /* this is not valid */
    /*...*/
  }
}

All functions must be preceded by either an int or char type specifier. For example, this declaration is valid:

int main()
{
  /*...*/
}

However, this one is not:

main ()
{
  /*...*/
}

Library Functions

The following library functions are implemented:

int getche(void);    /* Read a character from keyboard and return its value */
int putch(char ch);  /* Write a character to the screen */
int puts(char *s);   /* Write a string to the screen */
int getnum(void);    /* Read an integer from the keyboard and return its value */
int print(char *s);  /* Write a string to the screen */
int print(int i);    /* Write an integer to the screen */

Example Programs

A program demonstrating all of the features of Little C:

/* Little C Demonstration Program #1.

   This program demonstrates all features
   of C that are recognized by Little C.
*/

int i, j;   /* global vars */
char ch;

int main()
{
  int i, j;  /* local vars */

  puts("Little C Demo Program.");

  print_alpha();

  do {
    puts("enter a number (0 to quit): ");
    i = getnum();
    if(i < 0 ) {
      puts("numbers must be positive, try again");
    }
    else {
      for(j = 0; j < i; j=j+1) {
        print(j);
        print("summed is");
        print(sum(j));
        puts("");
      }
    }
  } while(i!=0);

  return 0;
}

/* Sum the values between 0 and num. */
int sum(int num)
{
  int running_sum;

  running_sum = 0;

  while(num) {
    running_sum = running_sum + num;
    num = num - 1;
  }
  return running_sum;
}

/* Print the alphabet. */
int print_alpha()
{
  for(ch = 'A'; ch<='Z'; ch = ch + 1) {
    putch(ch);
  }
  puts("");

  return 0;
}

A program demonstrating nested loops:

/* Nested loop example. */
int main()
{
  int i, j, k;

  for(i = 0; i < 5; i = i + 1) {
    for(j = 0; j < 3; j = j + 1) {
      for(k = 3; k ; k = k - 1) {
        print(i);
        print(j);
        print(k);
        puts("");
      }
    }
  }
  puts("done");

  return 0;
}

A program demonstrating the assignment operator:

/* Assigments as operations. */
int main()
{
  int a, b;

  a = b = 10;

  print(a); print(b);

  while(a=a-1) {
    print(a);
    do {
       print(b);
    } while((b=b-1) > -10);
  }

  return 0;
}

A program demonstrating recursive functions:

/* This program demonstrates recursive functions. */

/* return the factorial of i */
int factr(int i)
{
  if(i<2) {
    return 1;
  }
  else {
     return i * factr(i-1);
  }
}

int main()
{
  print("Factorial of 4 is: ");
  print(factr(4));

  return 0;
}

A program demonstrating function arguments:

/* A more rigorous example of function arguments. */

int f1(int a, int b)
{
  int count;

  print("in f1");

  count = a;
  do {
    print(count);
  } while(count=count-1);

  print(a); print(b);
  print(a*b);
  return a*b;
}

int f2(int a, int x, int y)
{
  print(a); print(x);
  print(x / a);
  print(y*x);

  return 0;
}

int main()
{
  f2(10, f1(10, 20), 99);

  return 0;
}

A program demonstrating loop statements:

/* The loop statements. */
int main()
{
  int a;
  char ch;

  /* the while */
  puts("\aEnter a number: ");
  a = getnum();
  while(a) {
    print(a);
    print(a*a);
    puts("");
    a = a - 1;
  }

  /* the do-while */
  puts("enter characters, 'q' to quit");
  do {
     ch = getche();
  } while(ch !='q');

  /* the for */
  for(a=0; a<10; a = a + 1) {
     print(a);
  }

  return 0;
}