Mike’s Web Site & Blog

No Subtitle Exists

Programming in C: 101

This article is part of the Learning to Program in C series.

When starting down a road, it helps to have a place to point at and say, “there is the start.” Since we’re not speaking of a literal road here, it can often be difficult to find “the” start, if indeed there is only a single one. Here, I offer a starting point on the road to learning to program, using the C programming language.

This is the first article in the Learning to Program in C series of posts.

The point of this article is for you to procure a working C compiler, which will allow you to take C source files and compile them into object files.

You will confirm that it all works by compiling and executing the following example C program:

#include <stdio.h>

main() {
    puts("Hello, world!\n");
    return 0;

Once you’ve accomplished that, it will be time to move on to the next post in the series. When the entire series is finished, you’ll have learned how to perform all of the major tasks required to create functional and useful computer software—for multiple platforms, too.

Knowledge Required

You need to know:

  • How to open a command-line window (on Windows) or a terminal window (on POSIX® systems, including Linux and macOS).
  • How to use programs from the command-line/terminal.
  • If using a remote system, how to connect to that system and operate it remotely (typically using SSH, VNC, or some other remote access protocol).

Most importantly, no previous experience programming or coding is required.

Resources Required

You need a system on which you can install a compiler. This could be any one of the following:

  • A PC or workstation-class system with at least 1 GB of RAM, running Windows, Linux, or macOS.
  • A virtual machine, such as a Linode. Such a system will allow you to run a C compiler without installing anything locally. It is a viable solution if you do not have a traditional computer, e.g., you are working on an iPad with a keyboard.
  • A shell account, such as one from sdf.org. The shell account provider must make a C compiler available, which might require a donation or subscription.

You also need:

  • Basic “computer” experience. You should know how to use a keyboard, mouse, touchscreen, and possibly a stylus. You should know what a “label,” a “button,” and a “scrollbar” are, and how to interact with those things.
  • A decent programmer’s editor. I personally use Visual Studio Code (yes, it is a Microsoft product—and it’s damn good). Another good option is Atom, which is what I used before I found VSC. Both VSC and Atom are available for the Linux, macOS, and Windows operating systems.
  • A little bit of experience in formal logic is useful, but it is not necessary; the required concepts will be picked up along the way.
  • Dedication and patience. While a person might be able to learn to code in a new programming language in a few days once they already know how to program, learning to do so initially means learning to program and learning to code, at the same time. And that is a lot of work.

Install a Compiler

In the really old days, when computers actually looked like machines, you would have to write machine language (usually by flipping switches or punching cards) to create an assembler. The assembler would then take Assembly language as input, and the programmer would write a compiler in assembly. Finally, the compiler would be rewritten in its own language, finishing the process of bootstrapping the compiler. Once this step is reached, the programmer is abile to maintain the compiler and any software written to be compiled with the compiler, hopefully never looking at the machine language again…

Thankfully, these days, we can just install the compiler—yay for that! It means that you can compile your first program today, and not in 6 to 12 weeks.

There are two major compilers worth discussing: the GNU Compiler Collection (also known as GCC) and clang (also known as “the new kid on the block”). Both include support for C and C++, although GCC also provides compilers for the Ada and Go programming languages. It is safe to use either compiler for now. In the past, GCC was the “safe” option; these days, they are both mostly equivalent in terms of capability and feature set.

If you’re on Windows, it truly doesn’t matter which one you choose: both GCC and clang are ABI-incompatible with Microsoft Visual C++, but they are ABI compatible with each other (because they both use the Intel Itanium C++ ABI). Either way: pick one and install it.

If you use a different operating system or distribution, you’re on your own for the installation part.

Confirming it Works

Remember that C program you saw at the top of the post? Copy and paste it into a text file, and save it as hello.c, somewhere accessible.

To confirm that your newly-installed compiler works, open a command-line or terminal window. Then, try the command for the compiler you installed.

For GCC:

gcc -o hello hello.c

For clang:

clang -o hello hello.c

If it worked, the compiler will have created a file named hello (or, on Windows, hello.exe). If you run this file, it should look similar to below:

$ ./hello
Hello, world!
$ _

Or, on Windows:

Hello, world!

If you’ve managed to get the expected output, then you’ve accomplished your goal, yay!

If you get an error message when trying to run the gcc or clang compiler programs, it is possible that you’ve either not installed the compiler correctly, or (particularly on Windows systems) you need to open a special command prompt window that was installed with your compiler, which would be in the Start menu.


Thank you for stopping by!

If you liked this article you can give a tip or become a patron. It helps pay my bills and keep me writing.