Like HowStuffWorks on Facebook!

How C Programming Works


The Simplest C Program

Let's look at a simple C program and use it both to understand the basics of C and the C compilation process. If you have your own computer with a C compiler installed as described earlier, you can create a text file named sample.c and use it to follow along while we step through this example. Note that if you leave off the .c in the file name, or if your editor appends .txt to the name, you'll probably get some sort of error when you compile it.

Here's our sample program:

/* Sample program */

#include <stdio.h>

int main()

{

printf("This is output from my first program!\n");

return 0;

}

When compiled and executed, this program instructs the computer to print out the line "This is output from my first program!" and then stop. You can't get much simpler than that! Now let's take a look at what each line is doing:

Line 1 -- This is one way to write comments in C, between /* and */ on one or more lines.

Line 2 -- The #include command tells the compiler to look at other sources for existing C code, particularly libraries, which are files that include common reusable instructions. The references a standard C library with functions for getting input from a user and for writing output to the screen. We'll look at libraries a more closely later.

Line 3 -- This line the first line of a function definition. Every C program has at least one function, or a block of code representing something the computer should do when the program runs. The function performs its task and then produces a byproduct, called a return value, that can be used by other functions. At a minimum, the program has a function called main like the one shown here with a return value with the data type int, which means integer. When we examine functions more later, you'll see what the empty parentheses mean.

Lines 4 and 7 -- The instructions within a function are enclosed in braces. Some programmers start and end a brace-enclosed block on separate lines as shown here. Others will put the open-brace ({) at the end of the first line of the function definition. Though lines of code in the program don't have to be typed on separate lines, programmers typically put each instruction on a separate line, indented with spaces, to make the code easier to read and edit later.

Line 5 -- This is a function call to a function named printf. That function is coded in the stdio.h library included from Line 1, so you don't have to write it yourself. This call to printf tells it what to print to the screen. The \n at the end, within the quotes, isn't printed, though; it's an escape sequence which instructs printf to move the cursor to the next line on the screen. Also, as you can see, every line in the function must end with a semi-colon.

Line 6 -- Every function that returns a value must include a return statement like this one. In C, the main function must always have an integer return type, even though it's not used within the program. Note that when you're running a C program, though, you're essentially running its main function. So, when you're testing the program, you can tell the computer to show the return value from running the program. A return value of 0 is preferred since programmers typically look for that value in testing to confirm the program ran successfully.

When you're ready to test your program, save the file and compile and run the program. If you're using the gcc compiler at a command line, and the program is in a file called sample.c, you can compile it with the following command:

gcc -o sample.exe sample.c

If there are no errors in the code, you should have a file named sample.exe in the same directory as sample.c after running this command. The most common error is a syntax error, meaning that you've mistyped something, such as leaving off a semicolon at the end of a line or not closing quotes or parentheses. If you need to make changes, open the file in your text editor, fix it, save your changes and try your compile command again.

To run the sample.exe program, enter the following command. Note the ./ which forces the computer to look at the current directory to find the executable file:

./sample.exe

Those are the basics of coding and compiling for C, though there's a lot more you can learn about compiling from other C programming resources. Now, let's open the box and see what pieces C has for building programs.


More to Explore