In this section, we're going to jump into writing simple programs as we explore the fundamentals of not only C but general programming.
Computer programming is about learning how to solve problems with a computer โ about how to get a computer to do all the tedious work for us. The basic development cycle, or process of writing a computer program, is to determine the steps that are necessary to solve the problem at hand and then tell the computer to perform those steps. Our first problem, as we learn this process, is to learn how to write, build, run, and verify a minimal C program.
The following topics will be covered in this chapter:
- Writing your first C program
- Understanding the program development cycle
- Creating, typing into a text editor, and saving your C program
- Compiling your first C program
- Running your program, verifying its result, and, if necessary, fixing it
- Exploring different commenting styles and using them
- Employing guided chaos, followed by careful observation for deeper learning
Let's get started!
Technical requirements
To complete this chapter and the rest of this book, you will need a running computer that has the following capabilities:
- A basic text editor that is able to save unformatted plain text
- A Terminal window that commands can be entered into via the command line
- A compiler to build your C programs with
Each of these will be explained in more detail as we encounter them in this chapter.
The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming. However, please make every effort to type the source code in yourself. Even if you find this frustrating at first, you will learn far more and learn far more quickly if you do all the code entry for yourself.
Writing your first C program
We will begin with one of the simplest, most useful programs that can be created in C. This program was first used to introduce C by its creators, Brian W. Kernighan and Dennis M. Ritchie, in their now-classic work, The C Programming Language, published in 1978. The program prints a single line of output โ the greeting Hello, world! โ on the computer screen.
This simple program is important for a number of reasons. First, it gives us a flavor of what a C program is like, but more importantly, it proves that the necessary pieces of the development environment โ the Operating System (OS), text editor, command-line interface, and compiler โ are installed and working correctly. Finally, it gives us the first taste of the basic programming development cycle. In the process of learning to program and, later, actually solving real problems with programming, you will repeat this cycle often. It is essential that you become both familiar and comfortable with this cycle.
This program is useful because it prints something out to the Terminal, also known as the console, telling us that it actually did something โ it displays a message to us. We could write shorter programs in C but they would not be of much use. We would be able to build and run them but would have little evidence that anything actually happened. So, here is your first C program. Throughout this book, and during the entirety of your programming experience, obtaining evidence of what actually happened is essential.
Since Kernighan and Ritchie introduced the Hello, world! program over 40 years ago, this simple program has been reused to introduce many programming languages and used in various settings. You can find variations of this program in Java, C++, Objective-C, Python, Ruby, and many others. GitHub, an online source code repository, even introduces their website and its functions with a Hello World beginner's guide.
Hello, world!
Without further ado, here is the Hello, world! C program. It does no calculations, nor does it accept any input. It only displays a short greeting and then ends, as follows:
#include <stdio.h>
int main()
{
printf( "Hello, world!\n" );
return 0;
}
Some minor details of this program have changed since it was first introduced. What is here will build and run with all C compilers that have been created in the last 20 years.
Before we get into the details of what each part of this program does, see if you can identify which line of the program prints our greeting. You may find the punctuation peculiar; we will explain this in the next chapter. Notice how some punctuation marks come in pairs, while others do not. There are five paired and five unpaired punctuation marks in all. Can you identify them? (We are not counting the punctuation in the message "Hello, world!".)
There is another pairing in this simple program that is not obvious at this time, but one that we will explore further in the next chapter. As a hint, this pairing involves the lines int main() and return 0;.
Before we jump into creating, compiling, and running this program, we need to get an overview of the whole development process and the tools we'll be using.
Understanding the program development cycle
There are two main types of development environments:
- Interpreted: In an interpreted environment such as Python or Ruby, the program can be entered line by line and run at any point. Each line is evaluated and executed as it's entered and the results are immediately returned to the console. Interpreted environments are dynamic because they provide immediate feedback and are useful for the rapid exploration of algorithms and program features. Programs entered here tend to require the interpreting environment to be running as well.
- Compiled: In a compiled environment such as C, C++, C#, or Objective-C, programs are entered into one or more files, then compiled all at once, and if no errors are found, the program can be run as a whole. Each of these phases is distinct, with separate programs used for each phase. Compiled programs tend to execute faster since there is a separate, full compilation phase, and can be run independently of the interpreting environment.
As with shampoo, where we are accustomed to wet hair, lather, rinse, and repeat, we will do the same with C โ we will become familiar with the edit, compile, run, verify, and repeat cycle.
Edit
Programs are generated from text files whose filenames use predefined file extensions. These are known as source files, or source code files. For C, the .c file extension indicates a C source code file. An .h extension (which is present in our Hello, world! program) indicates a C header file. The compiler looks for .c and .h files as it encounters them and because each has a different purpose, it treats each differently as well. Other languages have their own file extensions; the contents of a source code file should match the language that the compiler expects.
To create and modify C files, you will need a plain text editor. This is a program that allows you to open, modify, and save plain text without any formatting such as font size, font family, font style, and much more. For instance, on Windows, Notepad is a plain text editor while Word is not. The plain text editor should have the following capabilities:
- File manipulation: Open a file, edit a file, save the file and any changes that have been made to it, and save the file with another name.
- The ability to navigate the file: Move up, down, left, right, to the beginning of the line, end of the line, beginning of the file, end of the file, and so on.
- Text manipulation: Insert text, delete text, insert line, delete line, selection, cut, copy, paste, undo/redo, and so on.
- Search and replace: Find text, replace text, and so on.
The following capabilities are handy but not essential:
- Automatic indentation
- Syntax coloring for the specific programming language
- Automatic periodic saving
Almost any plain text editor will do. Do not get too caught up in the features of any given text editor. Some are better than others; some are free, while others are costly, and may not immediately be worth the expense (perhaps later, one or more might be worthwhile but not at this time), and none will do 100% of what you might want them to do.
Here are some free plain text editors worth installing on your computer and trying out:
- Everywhere:Nano, which runs in a Terminal; a moderate learning curve.
- Linux/Unix:
- Vim, or vi: Runs in a Terminal; a moderate learning curve. It is on every Linux/Unix system, so it's worth learning how to use its basic features.
- gedit: A powerful general-purpose editor.
- Emacs: An everything and the kitchen sink editor; a very large learning curve.
- Windows:
- Notepad: Very simple โ sometimes too simple for programming โ but included in every Windows system.
- Notepad++: A better version of Notepad with many features for programming.
- macOS only: BBEdit (free version), which is a full-featured GUI programming text editor.
There are many, many text editors, each with its own strengths and weaknesses. Pick a text editor and get used to it. Over time, as you use it more and more, it will become second nature.
Compile
The compiler is a program that takes input source code files โ in our case, .c and .h files โ translates the textural source code found there into machine language, and links together all the predefined parts needed to enable the program to run o...