The Daily Build

Icon

Software Development, version 3.0

Getting Started in C

Some may argue that C is an outdated language to learn in 2009. I disagree. Learning C requires some degree of understanding how computers really work. And if you can understand pointers, you’re ten steps ahead of the game.

This is the first in a series of articles designed to help the reader learn the C language and understand how to use it to solve practical problems. The tutorial will be based on gcc in a Cygwin or Linux environment.

If you are already running Linux or have Cygwin and GCC installed, you’re halfway done with the first lesson.

Lesson 1

Part 1, Get GCC

If you’re using linux, open a terminal window (bash prompt). Type “gcc –version”. If you get back something like:

$ gcc --version
gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Then you’re all set. If you don’t have gcc installed, use your distribution’s package installation tool to install it. (For example, Debian-based systems will use sudo apt-get install gcc.)

If you’re on Windows, install Cygwin. When the selection box pops up to choose packages, expand the “Devel” category and select gcc:

Select GCC from the list of packages.

There’s a great step-by-step tutorial available if you want more detail.

Part 2, “Hello World”

Now you’re ready to write your first program. If you’re on Windows, I strongly recommend using a decent text editor instead of Windows Notepad or WordPad. If you have no editor on your system, try <a href=”http://www.brixoft.net/dload.asp?file=srcedit.zip”>Source Edit</a>. I haven’t used it much, but it seems to work well enough. If you’re on Linux, you have a choice of whatever editors are installed on your system: vi, emacs, pico, etc.

Now, create a directory to store your source files. On Windows, place this directory under your cygwin installation directory. I’m going to assume you’re using c:\cygwin\tutorials\, so adjust accordingly if you choose a different path.

Finally, we can write a program to print the string “Hello World” to the output when you run it. Open your text editor and type in the following program. (Do not cut and paste. The act of retyping the code presented in this tutorial will help you learn better.)

#include <stdio.h>

int main(int argc, char** argv)
{
    printf("Hello World\n");
    return 0;
}

Now, get to a bash prompt. (On cygwin, this is the cygwin icon that should be on your desktop from part 1 above. On linux, this is a terminal window or whatever your system happens to call it.) Change to the directory where your files are stored.

bash$ cd /tutorial

On cygwin, this will put you in the c:\cygwin\tutorial directory. Now, compile the file.

bash$ gcc hello.c

GCC does not give output for a successful compile, it just produces the executable. If you do a directory listing (”ls” at the bash prompt) you will see a file called a.out (linux) or a.exe (cygwin). For historical reasons, this is what the compiler produces by default. You can run this and see the output. Type “./a.exe” or (./a.out) at the bash prompt and you should get the greeting.

We can change the output file by telling the compiler what we want it to name the file.

bash$ gcc -o hello hello.c

The -o flag (think of “output”) tells the compiler to produce a file called hello.exe (cygwin) or hello (linux). Try this, and run “./hello.exe”.

Part 3, Breakdown

Here’s a rundown of what the pieces of this program mean.

#include <stdio.h>

This tells the compiler to pull the text of the file stdio.h into the program. This is called a “header” file, because it is typically included at the top (head) of your program. This header file contains the standard input/output definitions and declarations, including the declaration of the function “printf” which is used below.

int main(int argc, char** argv)

This is the start of a function definition. “main” is the name of the function that the operating system will call when you run the program. Main is always declared this way: it accepts two “arguments” called argc and argv, and it returns an integer (int). We’ll come back to the argc and argv in another lesson.

{

A left brace is used to indicate the start of a block. The line that describes the type and arguments of the function is followed by a block that contains the programming statements that actually do something useful.

printf("Hello World\n");

This calls the printf function, which sends text to the output device (in this case, your terminal window). A function call is built by following the function name with parentheses and including arguments inside the parentheses. The printf function is passed an argument: the string “Hello World\n”.

In C, strings are surrounded by quotation marks. The sequence \n means “newline”. strong>Exercise: modify the program to remove the \n, compile it, and see what happens.

return 0;

This ends the function, returning control to the function that called it (in this case, back to the operating system). It gives the value 0 to the calling function. When we return 0 to the operating system, this signifies a normal exit. Returning 1 or other values generally signifies that an error occurred.

}

A right brace is used to mark the end of the function.

Exercise: Experiment with this. Change the string. Add another call to printf to display a different greeting.

Next lesson: the debugger.

Share and Enjoy:
  • del.icio.us
  • Digg
  • Sphinn
  • Facebook
  • Mixx
  • Google Bookmarks
  • Twitter
  • FriendFeed
  • Posterous
  • email

Like this post? Get updates from my feed.

Related posts:

  1. Using Python’s ctypes to Call Into C Libraries The ctypes module makes loading and calling into a dynamic...
  2. Five Days to a Django Web App: Day Four, Deployment Thanks for your patience, and for coming back for...
  3. Python Exception Handling: Cleanup and Reraise I’ve had this code around for a while and...
  4. Set Your zsh Prompt Since the beginning of time, all the cool kids have...
  5. Five Days to a Django Web App: Day Three, Coding Thanks for coming back for Day Three! [Note: Sorry...

Category: c-programming

Tagged: , ,

Leave a Reply

Related Posts

Related posts:

  1. Using Python’s ctypes to Call Into C Libraries The ctypes module makes loading and calling into a dynamic...
  2. Five Days to a Django Web App: Day Four, Deployment Thanks for your patience, and for coming back for...
  3. Python Exception Handling: Cleanup and Reraise I’ve had this code around for a while and...
  4. Set Your zsh Prompt Since the beginning of time, all the cool kids have...
  5. Five Days to a Django Web App: Day Three, Coding Thanks for coming back for Day Three! [Note: Sorry...