Do nothing, successfully

C is the lingua franca of systems programming. It is a general-purpose language that can be used for any kind of programming, and it is. However, as a systems programming language, its strength lies in writing programs at a level of abstraction that closely models the capabilities of most hardware. This is not only by design but also by coevolution.

A program in the C language is composed of two kinds of things: objects, which represent the data used in the computation, and functions, which represent the behavior of the computation. You do not have to define any objects, but every C program must at least have a function named main, which represents the behavior of the program overall. Thus, this is the smallest possible C program.

int main(){}

The environment provides this function with arguments, but we can leave the parameter list empty () and ignore them; the environment also expects main to return a number (an integer, abbreviated int), but at this point we don’t have to care what number is returned or what the environment does with it.

The only other syntax seen here is the pair of curly brackets {} that C and its descendents are so famous for, which describe what should happen when the environment calls this main function. Since there is nothing between the curly brackets in this example, nothing happens.

This program, despite being trivial, is installed as a system utility on all unix systems. It is named true, since it does nothing, but it does it quickly and is considered to do it successfully. It is used in shell scripting to represent the idea of success or Boolean truth.

Use your text editor to create a file named true.c and put the smallest possible C program text into it. Now you can build your program, i.e. translate it from the C language into the machine language your CPU understands so you will be able to run it. Use the system utility make.

make true

The argument to make is the name of the program to be built; by convention, the source code for program will be in program.c. Now run your program!

./true

Two obvious questions about this invocation answer each other: Why the ./? And how does the system know to run this program, and not the system program true? As a filename, ./true (using the abbreviation . for the current directory) is equivalent to just true, since relative paths start from the current directory anyway. However, the redundant ./ results in a path with a / in it, which forces the shell to run the program at that path rather than looking for a program installed in a system directory.

Now you have written, built, and run a program in the C language. It does nothing, and it does it successfully.

Even with such a short program, it is already possible to discuss style. In order to feel honest calling it the shortest possible program, I have presented the source code with no unnecessary whitespace. In general, the compiler doesn’t care a whit about whitespace; it is only required when necessary to distinguish alphanumeric sequences (i.e. ‘int main’, two words, versus ‘intmain’, one word). Wherever whitespace is allowed, any kind or amount of it can appear and is equivalent to a single space character.

The compiler may not care, but humans do. A program isn’t written in C for explaining what to do to the computer—no computer has ever been made that directly understands C, which is why we have to compile it. Machine code is for communicating with the machine. The C language is for communicating with other humans about what you want the computer to do.

Different people have different ideas of style, and that is okay. You will develop your own style, and by and large what matters most is legibility and consistency. I normally put the opening bracket for a function body on the next line, and I put the corresponding closing bracket on its own line. Any code within the brackets, which we will soon be adding, is indented to make it easy to scan the structure of the code.

int main()
{
    /* Your code could go here. But this is a comment.
       Comments, marked by slash-star and star-slash like
       this one, count as whitespace as far as the compiler
       is concerned. */
}

This is equivalent to the first way of writing it, but given the style I am used to reading and writing, this is much easier for me to read.

(Incidentally, in historical versions of the C language, the shortest program was just main(){}, because programmers weren’t required to say what types their functions returned. This hasn’t been true since the 1999 standard, however, so the version I gave above is indeed the shortest version allowed by the modern language.)

You have attempted of activities on this page