Home > Articles > Programming > C/C++

The Absolute Beginner's Guide to Writing Your First C Program

  • Print
  • + Share This
This chapter introduces you to your first C program, your first function, and teaches you to identify different kinds of data used in programs.
This chapter is from the book

You get to see your first C program in this chapter! Please don’t try to understand every character of the C programs discussed here. Relax and just get familiar with the look and feel of C. After a while, you will begin to recognize elements common to all C programs.

A Down-and-Dirty Chunk of Code

This section shows you a short but complete C program and discusses another program that appears in Appendix B, “The Draw Poker Program.” Both programs contain common and different elements. The first program is extremely simple:

/* Prints a message on the screen */
#include <stdio.h>
      printf("Just one small step for coders. One giant leap for");
      printf(" programmers!\n");
      return 0;

Open your programming software and type in the program as listed. Simple, right? Probably not the first time you use your new compiler. When you open Code::Blocks for the first time, you will be greeted by a “Tip of the Day.” These tips will come in handy later, but right now you can just get rid of it by clicking Close.

To create your program, Click the File Menu and select New. Choose Empty File from the options that appear on the submenu. Now you’ve got a nice clean file to start writing your seven-line program.

After you type in your program, you will need to compile or build your program. To do this, click the little yellow gear icon in the upper-left corner. If you’ve typed the program in exactly and had no errors, you can then run the program by clicking the green right-facing arrow next to the gear. (The next icon in that row, with a gear and arrow, will do both the compiling and running of the program, simplifying your life by reducing the number of arduous clicks you must perform from two to one.)

When you compile (or build) the program and run it, you should see something like Figure 2.1.


FIGURE 2.1. The output of your first program.

To see a much longer program, glance at Appendix B. Although the Draw Poker game there spans several pages, it contains elements common to the shorter program you just saw.

Look through both the programs just discussed and notice any similarities. One of the first things you might notice is the use of braces ({}), parentheses (()), and backslashes (\). Be careful when typing C programs into your C compiler. C gets picky, for instance, if you accidentally type a square bracket ([) when you should type a brace.

C isn’t picky about everything. For instance, most of the spacing you see in C programs makes the programs clearer to people, not to C. As you program, add blank lines and indent sections of code that go together to help the appearance of the program and to make it easier for you to find what you are looking for.

C requires that you use lowercase letters for all commands and predefined functions. (You learn what a function is in the next section.) About the only time you use uppercase letters is on a line with #define and inside the printed messages you write.

  • + Share This
  • 🔖 Save To Your Account