Basics of a Typical Program in C - dummies

By Dan Gookin

All C programs feature a basic structure, which is easily shown by looking at the C source code skeleton that Code::Blocks uses to start a new project.


#include <stdio.h>
#include <stdlib.h>
int main()
  printf("Hello world!n");
  return 0;

This listing isn’t the bare minimum, but it gives a rough idea of the basic C program.

Just as you read text on a page, C source code flows from the top down. The program starts execution at the first line, and then the next line, and so on until the end of the source code. Exceptions to this order include decision-making structures and loops, but mostly the code runs from the top down.

Basics of the C program structure

To better understand how C programs come into being, you can create the simplest, most useless type of C program.

Here are the specific steps:

  1. Start a new Code::Blocks project: ex0301.

  2. Erase the skeleton that Code::Blocks provided.

    Your source code should be empty.

  3. Save the project.

  4. Build and run.

    Code::Blocks complains that the project hasn’t been built yet. Tough!

  5. Click the Yes button to proceed with building the project.

    Nothing happens.

Because the source code is empty, no object code is generated. Further, the program that’s created (if a program was created) is empty. It does nothing. That’s what you told the compiler to do, and the resulting program did it well.

You may see a Code::Blocks error message after Step 4. That’s because the IDE was directing the operating system to run a program in a command prompt window. The error you see is the reference to a program file that either doesn’t exist or doesn’t do anything.

How to set the main() function

All C programs have a main() function. It’s the first function that’s run when a program starts. As a function, it requires parentheses but also curly brackets to hold the function’s statements.

Continue with previous exercise: Rebuild the source code for project ex0301. Save the project. Build and run.


main() {}

This time, you see the command prompt window, but nothing is output. That’s great! You didn’t direct the code to do anything, and it did it well. What you see is the minimum C program. It’s also known as the dummy program.

  • main isn’t a keyword; it’s a function. It’s the required first function in all C language source code.

  • Unlike other functions, main() doesn’t need to be declared. It does, however, use specific arguments.

How to return something to the operating system

Proper protocol requires that when a program quits, it provides a value to the operating system. Call it a sign of respect. That value is an integer (a whole number), usually zero, but sometimes other values are used, depending on what the program does and what the operating system expects.

Continue with previous exercise: Update the source code for project ex0301 to reflect the changes shown here.


int main()

First, you’re declaring the main() function to be an integer function. The int tells the compiler that main() returns, or generates, an integer value.

The return statement passes the value 1 back to the operating system, effectively ending the main() function and, therefore, the program.

As you type return, Code::Blocks may display Auto Complete text. These hints are useful to help you code, though at this point in your programming career, you can freely ignore them.


Continue with previous exercise: Save, build, and run the project.

The results are similar to the previous run, but you’ll notice the return value of 1 specified in the Code::Blocks summary in the command prompt window:

Process returned 1 (0x1)

If you like, edit the code again and change the return value to something else — say, 5. That value appears in the Code::Blocks output when you run the project.

  • Traditionally, a return value of 0 is used to indicate that a program has completed its job successfully.

  • Return values of 1 or greater often indicate some type of error, or perhaps they indicate the results of an operation.

  • The keyword return can be used in a statement with or without parentheses. Here it is without them:

    return 1;

    Above, return is used with parentheses. The result is the same.

How to add a function

C programs should do something. Though you can use keywords and operators to have a program do marvelous things, the way to make those things useful is output.

Continue with previous exercise: Modify the project’s source code one final time to match this listing:


#include <stdio.h>
int main()
  printf("4 times 5 is %dn",4*5);

You’re adding three lines. First, add the #include line, which brings in the printf() function’s prototype. Second, type a blank line to separate the processor directive from the main() function. Third, add the line with the printf() function. All functions must be declared before use, and the stdio.h file contains the declaration for printf().

When you type the first ″ for printf(), you see the second quote appear automatically. Again, that’s Code::Blocks helping you out. Remain calm.


Before proceeding, please note these two important items in your source code:

  • Ensure that you typed the #include line exactly as written:

    #include <stdio.h>

    The #include directive tells the compiler to fetch the header file, stdio.h. The header file is required in order to use the printf() function.

  • Ensure that you type the printf() statement exactly as written:

    The printf() function sends formatted text to the standard output device: the display. It also contains a math problem, 4*5. The result of that problem is calculated by the computer and then displayed in the formatted text:

    printf("4 times 5 is %dn",4*5);

    You’ll find lots of important items in the printf() statement, each of which is required: quotes, comma, and semicolon. Don’t forget anything!

Finally, the return value is changed from 1 to 0, the traditional value that’s passed back to the operating system.

Continue with previous exercise: Save the project’s source code. Build and run.

If you get an error, double-check the source code. Otherwise, the result appears in the terminal window, looking something like this:

4 times 5 is 20

The basic C program is what you’ve seen presented. The functions you use will change, and you’ll learn how things work and become more comfortable as you explore the C language.