How to Write and Test Source Code in C

By Dan Gookin

Source code represents the part of the process that contains the programming language itself. You use a text editor to write a source code file.

How to write C source code

Source code is shown in program listings.

STANDARD “HELLO WORLD” PROGRAM

#include <stdio.h>
int main()
{
  puts("Greetings, human.");
  return 0;
}

Line numbers are not shown in the listings because they can be confusing. Besides, line numbers are referenced in the editor, such as in Code::Blocks, as you type.

You’re directed to type the source code from a listing as part of an exercise.

  1. Create a new Code::Blocks console application, a C language project named ex0201.

  2. Type the code from Standard “Hello World” Program into the editor.

    You can erase the skeleton given by Code::Blocks or just edit it so that the result matches.

  3. Save the source code file by choosing the File→Save File command.

There. You’ve just completed the first step in the programming process — writing source code.

  • All C source code files end with the .c filename extension.

  • If you’re using Windows, you shouldset the folder options so that filename extensions are displayed.

  • C++ source code files have the extension .cpp.

  • In Code::Blocks, the final program name is the same as the project name, so changing the source code filename isn’t vital.

How to compile to object code inC

A compiler is a program that reads text from a source code file and translates that text into something called object code. In C, the compiler also deals with special instructions called preprocessor directives.

For example, Standard “Hello World” Program shows the following precompiler directive:

#include <stdio.h>

The include directive instructs the compiler to locate the header file stdio.h. The contents of that file are added to the source code, and then both are converted by the compiler into object code. The object code is then saved into an object code file. The object file has the same name as the source code file, but with the .o filename extension.

As the compiler translates your C code into object code, it checks for common mistakes, missing items, and other issues. If anything is awry, the compiler displays a list of errors. To fix the errors, you reedit the source code and attempt to compile once again.

  1. Choose Build→Compile Current File.

    The Build Log window displays the results, which shows zero errors and zero warnings. Well, unless you mistyped something, in which case, check your source code.

You would normally choose the Build command at this step. But when you need only to compile, you use the Compile Current File command.

Upon success, the compiler produces an object code file. Because the source code file is named main.c, the object code file is named main.o.

In Code::Blocks, the object code file is found in the project’s folder, inside either the obj/Release or obj/Debug subfolder.

How to link in the C library

The linker is the tool that creates the final program. It does so by linking the object code file with C language libraries. The libraries contain the actual instructions that tell the computer (or another device) what to do. Those instructions are selected and executed based on the shorthand directions found in the object code.

For example, in Standard “Hello World” Program, you see the word puts. This word is a C language function, which is written as puts() in this text. It stands for put string.

Oh, and puts rhymes with foots, not shuts.

The compiler translates puts() into a token and saves that token in the object code file, main.o.

The linker combines the object file with the C language standard library file, creating the final program. As with the compiler, if any errors are detected (primarily, unknown tokens at this point), the process stops and you’re alerted to the potential troublemaker. Otherwise, a fully functional program is generated.

In Code::Blocks, the Build command is used to compile and link; the IDE lacks a separate Link command.

  1. Choose the Build→Build command.

    Code::Blocks links the object file with C’s standard library file to create a program file.

The next and final step in the process is to run the program.

  • The text a program manipulates is referred to as a string, which is any text longer than a single character. In C, a string is enclosed in double quotes:

    "Hello! I am a string."
  • The final program includes the C language library, bundling it in with the object code. This combination explains why a program file is larger than the source code file that created it.

  • Some C programs link in several libraries, depending on what the program does. In addition to the standard C libraries, you can link libraries for working with graphics, networking, sound, and so on. As you learn more about programming, you’ll discover how to choose and link in various libraries.

How to run and test C source code

Creating a program is the whole point of programming, so the first thing to do after linking is to run the result. Running is necessary, primarily to demonstrate that the program does what you intend and in the manner you desire.

When the program doesn’t work, you have to go back and fix the code. Yes, it’s entirely possible to build a program and see no errors and then find that the thing doesn’t work. It happens all the time.

  1. Choose Build→Run.

    The program runs. As a Text mode program, it appears in a terminal window, where you can peruse the results.

  2. Close the terminal window by pressing the Enter key on the keyboard.

Running a simple program like ex0201 merely shows the results. For complex projects, you test the program. To do so, run the program and try to break it. If the program survives, you’ve done your job. Otherwise, you have to reedit the source code to fix the problem and then rebuild the program.

  • Running a program is a job for the device’s processor and operating system: The operating system loads the program into memory, where the processor runs the code.

  • In Code::Blocks, the program file is named after the project. In Windows, the name is ex0201.exe. In Mac OS X, Linux, and Unix, the program name is ex0201 with no extension. Further, the file’s permissions are set so that the file becomes an executable.