Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learn C Programming
Learn C Programming

Learn C Programming: A beginner's guide to learning the most powerful and general-purpose programming language with ease , Second Edition

eBook
£19.99 £28.99
Paperback
£35.99
Subscription
Free Trial
Renews at £16.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. £16.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Learn C Programming

Running Hello, World!

Computer programming is about learning how to solve problems with a computer – it's about how to get a computer to do all the tedious work for you. 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 begin exploring this process, is to learn how to write, build, run, and verify a minimal C program.

In this chapter, we will cover the following topics:

  • 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

The knowledge gained with these topics will be essential for further progress. So, 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 can save unformatted plain text
  • A Terminal window where commands can be entered 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-Second-Edition. 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 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, which was 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 several 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 on your computer system. Finally, it gives us a 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. Although we would be able to build and run them, we would have very 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 has been used in various settings. You can find variations of this program in Java, C++, Objective-C, Python, Ruby, and more. GitHub, an online source code repository, even introduces its website and its functions with a Hello World beginner's guide.

Hello, world!

Without further ado, here is the Hello, world! C program. It performs 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 presented here will build and run with all C compilers that have been created in the last 30 years.

Before we get into the details of what each part of this program does, see whether you can identify which line of the program prints our greeting. You might find the punctuation peculiar; we will explain this in the next chapter. Additionally, notice how some punctuation marks come in pairs, while others do not. There are five paired and five unpaired punctuation marks in total. Can you identify them? (Note that we are not counting the punctuation, that is, the comma and exclamation point, in the Hello, world! message.)

There is another pairing in this simple program that is not so obvious at this time but one that we will explore further in the next chapter. As a hint, this pairing involves the int main() and return 0; lines.

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.

Tip

If you are eager to begin creating your first program, you can jump ahead to the next section. If you do, please come back to the Understanding the program development cycle section to complete your understanding.

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 that 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. A .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 also treats each of them differently. 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, and font style. 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: This allows you to open a file, edit a file, save the file and any changes that have been made to it, and, finally, save the file with another name.
  • The ability to navigate the file: This allows you to move up, down, left, or right to the beginning of the line, end of the line, beginning of the file, end of the file, and more.
  • Text manipulation: This allows you to insert text, delete text, insert a line, delete a line, selection, cut, copy, paste, undo/redo, and more.
  • Search and replace: This allows you to find text, replace text, and more.

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 might 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 that are worth installing on your computer and trying out:

  • Everywhere: Nano, which runs in a Terminal window; it has a moderate learning curve.
  • Linux/Unix: This consists of the following:

a. Vim or vi: This runs in a Terminal window; it has a moderate learning curve. It is on every Linux/Unix system, so it's worth learning how to use its basic features.

b. gedit: This is a powerful general-purpose editor.

c. Emacs: This is an everything and the kitchen sink editor; it has a very large learning curve.

  • Windows: This consists of the following:

a. Notepad: This is very simple – sometimes too simple for programming – but included in every Windows system.

b. Notepad++: This is a better version of Notepad with many features for programming.

  • macOS only: This runs BBEdit (the free version), which is a full-featured GUI programming text editor.

There are 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, the .c and .h files – translates the textual source code found there into machine language, and links together all the predefined parts required to enable the program to run on our specific computer hardware and OS. It generates an executable file that consists of machine language.

Machine language is a series of instructions and data that a specific Central Processing Unit (CPU) knows how to fetch from the program execution stream and execute on the computer one by one. Each CPU has its own machine language or instruction set. By programming in a common language, such as C, the programmer is shielded from the details of machine language; that knowledge is embodied in the compiler.

Sometimes, assembler language is called machine language, but that is not quite accurate since assembler language still contains text and symbols, whereas machine language only consists of binary numbers. Today, very few people have the skills to read machine language directly; at one time, many more programmers were able to do it. Times have changed!

When we compile our programs, we invoke the compiler to process one or more source files. The result of this invocation is either a success and an executable file is generated, or it will identify the programming errors it found during compilation. Programming errors can range from a simple misspelling of names or omitted punctuation to more complex syntax errors. Typically, the compiler tries to make sense of any errors it finds; it attempts to provide useful information for the problem it has found. Note that try and attempts are merely goals; in reality, the compiler might spew many lines of error messages that originate from a single error. Furthermore, the compiler will process the entire source code when invoked. You might find many different errors in different parts of the program for each compiler invocation.

A complete, runnable program consists of our compiled source code – the code we write – and predefined compiled routines that come with the OS, that is, code written by the authors of the OS. The predefined program code is sometimes called the runtime library. It consists of a set of callable routines that know how to interact, in detail, with the various parts of the computer. For example, in Hello, world!, we don't have to know the detailed instructions to send characters to the computer's screen – we can simply call a predefined function, printf();, to do it for us. printf() is part of the C runtime library, along with many other routines, as we will discover later. In the next chapter, we will explore the role of functions. How text is sent to the console is likely to be different for each OS, even if those OSes run on the same hardware. So, the programmers are shielded not only from the minutiae of the machine language, but they are also shielded from the varying implementation details of the computer itself.

It follows from this that for each OS, there is a compiler and a runtime library specific to it. A compiler designed for one OS will most likely not work on a different OS. If, by chance, a compiler from one OS just happens to or even appears to run on a different OS, the resulting programs and their executions would be highly unpredictable. Mayhem is likely.

Many C compilers for every OS

You can learn C on many computer platforms. Common compilers in use on Unix and Linux OS are either the GNU Compiler Collection (GCC) or the LLVM compiler project, Clang. For Windows, GCC is available via the Cygwin project or the MinGW project. You could even learn C using a Raspberry Pi or Arduino, but this is not ideal because of the special considerations required for these minimal computer systems. It is recommended that you use a desktop computer since many more computer resources (such as memory, hard drive space, CPU capability, and more) are available on any such computer that can run a web browser.

Important Note

You should be aware that there are many variants of C. Typically, these are created by hardware vendors for the specific needs of their customers or by other groups who wish to retain extended features not approved in the standard specification.

Also, be aware that there are areas of the C specification that are undefined; that is, the standards committees have left those implementation details up to the creators of a given compiler for a given hardware system.

In this book, we will describe and focus strictly upon what the C standard provides. However, our approach will be such that we will emphasize how to verify program behavior so that we will always know when our program behaves differently. Because compilers are written by humans and not autogenerated from a specification, we subscribe to the guidance: trust but verify.

A note about Integrated Development Environments

On many OSes, the compiler is installed as a part of an Integrated Development Environment (IDE) for that OS. An IDE consists of a set of programs needed to create, build, and test programs for that OS. It manages one or more files associated with a program, has its own integrated text editor, can invoke the compiler and present its results, and can execute the compiled program. Typically, the programmer never leaves this environment while developing. Often, the IDE streamlines the production of a standalone working program.

There are many such IDEs – Microsoft's Windows-only Visual Studio, Microsoft's multiplatform Visual Studio Code, Apple's Xcode for macOS and other Apple hardware platforms, Eclipse Foundation's Eclipse, and Oracle's Netbeans, to name a few. Each of these IDEs can develop programs in a variety of languages. Nearly all of the programs used in this book were developed using a simple IDE named CodeRunner for macOS.

We will not use an IDE to learn C. In fact, at this stage of your learning, it is not advised for several reasons. To begin, learning and using an IDE can be a daunting learning task in and of itself. This task can and should be put off until you have more experience with each of the individual parts of the program development cycle. IDEs, while they have common functions, are sometimes implemented in vastly different ways with far too many different features to explore. Learn C first; then, you can learn an IDE for your desired environment later.

Installing a compiler on Linux, macOS, or Windows

Here are the steps to follow to install a C compiler on the major desktop computer environments – Linux, macOS, and Windows. For other platforms, you'll have to do some investigation to find the compiler you need. However, since those platforms want you to use them, they'll likely make those instructions easy to find and follow:

  • For Linux, perform the following steps:

1. If you are running a Red Hat Package Manager (RPM)-based Linux, such as Red Hat, Fedora, or CentOS, enter the following command on the command line:

$ sudo yum group install development-tools

2. If you are running Debian Linux, open a Terminal window and enter the following command from the command line:

$ sudo apt-get install build-essential

3. Verify your installation by entering the following command on the command line:

$ cc --version

From the preceding command, you will observe that you likely have GCC or Clang. Either one is fine. You are now ready to compile C programs on your version of Linux.

  • For macOS, perform the following steps:

1. Open Terminal.app and enter the following on the command line:

$ cc --version

2. If the development tools have not been installed yet, simply invoking the preceding command will guide you through their installation.

3. Once the installation is complete, close the Terminal window, open a new one, and enter the following:

$ cc --version

You are now ready to compile C programs on your version of macOS.

  • For Windows, perform the following steps:

1. Install either Cygwin (http://www.cygwin.com) or MinGW (http://mingw-w64.org/) from their respective websites. Either one will work well. If you choose to install Cygwin, be sure to also install the extra package for the GCC. This will install a number of other necessary compilers and debugging programs with GCC.

2. Once the installation is complete, open Command Prompt and enter the following:

$ cc --version

You are now ready to compile C programs on your version of Windows.

Note that compilation is a two-part process – compiling and linking. Compiling involves syntax checking and converting source code into nearly complete executable code. In the linking phase, the nearly complete machine code is merged with the runtime library and becomes complete. Typically, when we invoke the compiler, the linker is also invoked. If the compiler phase succeeds (that is, with no errors), the linking phase is automatically invoked. Later, we will discover that we can get error messages from the compiler either at compile time – the compiling phase – or at link time – the linking phase – when all of the program's pieces are linked together.

We will learn how to invoke the compiler later when we compile our first program.

Throughout this book, once you have a working program, you will be directed to purposely break it – causing the compilation of your program to fail. This is so that you can start learning about the correlation of various program errors with compiler errors, so you will not be afraid of breaking your program. You will simply undo the change and success will be yours once more.

Run

Once compilation has been completed successfully, an executable file will be generated. This executable file, unless we provide an explicit name for it, will be named a.out. Typically, the executable file will be created in the same directory the compiler was invoked from. For the most part, we will make sure our current working directory has the same location as the source files.

Running an executable file is performed by invoking it from the command line. When invoked, the executable is loaded into the computer's memory and then becomes the CPU's program execution stream. Once loaded into memory, the CPU begins at the special reserved word, known as main(), and continues until either return; or a closing } character is encountered. The program stops and the executable is then unloaded from memory.

To run an executable, open Command Prompt (on Windows) or a Terminal window (on Linux and Mac), navigate with cd to the directory of the executable file, and simply enter the executable's name (for example, a.out or whatever you've specified).

Note

If you successfully navigate to the same location as the executable and you have verified it exists, but you get an error message from the command interpreter, you likely have a problem with your command interpreter's built-in PATH variable. To quickly work around this, enter the $ ./a.out command to run it. This instructs the command interpreter to look in the current directory for the file named a.out.

As the program runs, any output will be directed to the Terminal or console window. When the program has ended, the command interpreter will present you with a new command prompt.

Verify

At this point in the cycle, you might feel that simply getting your program to compile without errors and running it without crashing your computer means you are done. However, you are not. You must verify that what you think your program was supposed to do is what it actually did do. Did your program solve the problem it was intended to? Is the result correct?

So, you have to return to writing your original program and then compare that to the output your program gives. If your intended result matches, your program is correct; only then are you done.

As we get further into writing more complex programs, we will discover that a proper or good program exhibits each of the following qualities:

  • Correct: The program does what it's supposed to do.
  • Complete: The program does everything it's supposed to do.
  • Concise: The program does no more than it's supposed to do and it does so as efficiently as possible.
  • Clear: The program is easily understandable to those who use it and to those who must maintain it.

For most of this book, we will concern ourselves largely with correctness, completeness, and clarity. Currently, hello1.c is not complete, nor clear, and we will understand why shortly.

Repeat

Unlike our shampoo metaphor that we mentioned earlier, which was wet hair, lather, rinse, and repeat, instead of repeating the instructions just once, you will repeat this cycle multiple times.

Rarely will you be able to go through the program development cycle with only one iteration. Most likely, you have to will repeat parts of it many more times. For example, you might edit the source code, compile it, and find that the compiler failed. In this scenario, you will have to go back to edit the source code and compile it, each time figuring out what went wrong and then fixing it. Once it compiles successfully, you will move on to running and verifying it. If the output is not correct or the program crashes, you will have to figure out what went wrong and start editing the source code again.

Does this sound frustrating? It can be – especially when you don't know why something went wrong or you can't figure out what the compiler or computer is saying is wrong.

Many years ago, when compilers were simpler and not as forgiving as they are today (actually, compilers are still not very forgiving – they've just gotten better at figuring out what we humans might have done wrong with our programs and communicating it to us in better ways!), the very first time I attempted to compile my Hello, world! program on a Digital Equipment Virtual Address Extension (VAX) Virtual Memory System (VMS) C compiler, the compiler gave me 23 thousand error messages. It turns out that I had overlooked a single ; character somewhere. One character. Sheesh!

The point of that story is to reassure you that you will make mistakes, mostly missing or erroneous punctuation or misspelled variables, and you will get frustrated. Part of learning to program is learning how to deal with your frustration and how to become a sleuth to track down the picayune errors that will crop up.

When that happens, get up and take a break from the computer. Take a walk. Have a laugh. Then, get back to work. Don't omit the first parts (that is, laughing and walking around a bit).

A note about debugging

As you go through the program development cycle, and as you get more familiar with the development language, the development tools, and yourself (yes, you are learning about yourself while you program), this will all become second nature to you, as it should. When you make a typing error, or when you get an obviously incorrect result, these are not bugs – they are just mistakes. Bugs are far more subtle.

There is a deeper trap that is very difficult for most beginner programmers to see; that is, their assumptions about what should happen without any evidence of what did happen. Most of the most difficult bugs that I introduced in my code were those that I assumed the program would work on in a certain way, but I did not verify that. When I finally went back to my assumptions and proved them in code, I was able to get beyond my self-imposed bugs.

So, can you avoid this trap?

Yes. Throughout this book, we will attempt to mitigate this subtle problem with a method we will use to develop programs. As we proceed, we will use trial and error, guided discovery, and evidence through observation. Sometimes, we will purposefully break our programs to see what happens. Additionally, we will try to prove each concept so that the expected behavior matches the actual behavior.

This is not to say that even with such an approach, bugs won't creep in. They will. But with careful attention to your assumptions, observed behavior, and the collection of evidence that you have gathered to prove any assumption, most bugs can be avoided.

Now that we have introduced the program development cycles, you are now ready to write your first program. As you progress through this book, you will become very familiar with these cycles.

Creating, typing, and saving your first C program

Let's begin creating our Hello, world! program.

Before we begin creating files, create a directory on your computer where you will save all of the work for this book. Perhaps you can create it in your $HOME directory or your Documents folder. My advice is to put it somewhere inside a user directory of your choice. Let's go ahead with our program:

  1. Open Command Prompt, a Terminal window, or a console (depending on your OS).
  2. Navigate to $HOME or ./Documents, or wherever you chose to work from, and create a directory for the programs that you'll write in this book. Do this with the following command:
    $ mkdir PacktLearnC
  3. Make that directory your current working directory using the following command:
    $ cd PacktLearnC
  4. Make a new directory for this chapter using the following command:
    $ mkdir Chapter1_HelloWorld
  5. Make that directory your current working directory with the following command:
    $ cd Chapter1_HelloWorld
  6. Picking the text editor of your choice – any will do – open the text editor either from the command line or the GUI (depending on both your OS and your preference of which one you wish to use).

From the command line, you might enter $ myEditor hello1.c, or just $ myEditor, and later, you will have to save the file as hello1.c in the current working directory.

  1. Enter the following program text exactly, all while paying attention to spacing, {} versus () versus "" (these double quotation marks are the keys next to the ; and : keys) versus <>, with particular attention being paid to #, \, ., and ;:
    #include <stdio.h>
    int main()
    {
        printf( "Hello, world!\n" );
        return 0;
    }
  2. Save your work and exit the editor.
  3. Verify that hello1.c exists by listing the directory and verifying that its file size is not zero.

Congratulations! You have completed your first editing phase of the program development cycle.

Compiling your first C program

Having successfully entered and saved your hello1.c file, it is now time to compile it:

  1. In a Terminal, command-line, or console window (depending on your OS), with the current working directory the same as your hello1.c file, enter $ cc hello1.c.
  2. Once this is done and you have a new command-line prompt, verify that you have a file named a.out.

You have completed your first compiling phase of the program development cycle.

Running your first C program

Your hello1.c program has been successfully compiled, and you now have an a.out file in the same directory. It's time to run it! Let's get started:

  1. In a Terminal, command-line, or console window (depending on your OS), navigate to the directory that holds a.out.
  2. At the command prompt, usually indicated by a > character in the first column, enter ./a.out.
  3. You should see Hello, world!.
  4. If you see that, we can now verify the output of your program.
  5. Note that the command prompt, $, is not on the same line as Hello, world!. This means you correctly entered \n in the output stream. If not, you need to re-edit hello1.c and make sure \n occurs immediately preceding the second ", recompile it, and rerun a.out.
  6. If Hello, world! is on a line by itself with a command prompt before and after it – woo-hoo! You did it!

If everything works as it should, you should see the following output in your Terminal:

Figure 1.1 – A successful compilation and execution of hello1.c

Figure 1.1 – A successful compilation and execution of hello1.c

In the screenshot of our Terminal session, > is the prompt the Terminal gives to indicate that it is ready to take input from us. After the first prompt, we have entered cc hello1.c followed by <return>. The fact that the very next thing we see is another prompt means that the compilation was successful, and we can now run the compiled program. We then enter a.out followed by <return> to execute hello1.c. We should see our desired message, Hello, world!, followed by a prompt awaiting Figure 1.1 – further input.

If the compiler spews out any error messages, read what the compiler is telling you and try to understand what error it is telling you to fix. Always focus on the very first error message, to begin with; later error messages are usually the result of the very first error. Then, go back to the editing phase and examine where your entered program is different from what has been shown here. The two must match exactly. Then, come back to this phase; hopefully, your program will compile successfully (that is, there will be no error messages).

It's always important to remember to do a little dance, and make a little joy, get down tonight! when you've successfully completed something. Programming can be very frustrating, so remembering to celebrate even your small successes will make your life a little bit more joyful through all the frustration. Too many programmers forget this incremental and regular step of celebrating with joy!

As we progress through this book, we'll add more compiler options to the cc command to make our life easier.

Tip

If you skipped over the Understanding the program development cycle section of this chapter, now would be a good time to read it before moving on.

Writing comments to clarify the program later

A lot about writing good code is writing code in a consistent manner. Consistency makes it somewhat easier for the reader (or you) to comprehend at a later time. Consistency is very often a good thing. However, there might be times where we need to step out of that consistency, and for some good reason, when we write code, that code becomes particularly twisted or obtuse and difficult to understand. Or, we might write code a certain way that might not be obvious or might not be expected, again for good reason. It is in these circumstances that we should comment on our code – not for the compiler, but for ourselves and others who might be reading our code later, often much later, scratching our/their foreheads and thinking, What? What did I/they intend to do here?

Code comments are a way to explain why a particular piece of code is written in a certain way. Let's explore some of the different ways we can write code comments in C.

Comments in code, when done correctly, are ignored by the compiler. They are only for human edification. Consider the following code comments:

/* (1) A single-line C-style comment. */
/* (2) A multi-line
   C-style comment. */
/*
 * (3) A very common way to 
 * format a multi-line
 * C-Style comment.
 */
/* (4) C-style comments can appear almost anywhere. */
/*(5)*/ printf( /* Say hello. */ "Hello, world!\n" ); 
/*(6)*/ printf( "Hello, world!\n" ); /* Yay! */
// (7) A C++ style comment (terminated by End-of-Line).
   printf( "Hello, world!\n" ); // (8) Say hello; yay!
//
// (9) A more common way 
// of commenting with multi-line
// C++ style comments
// 
// (10) anything can appear after //, even /* ... */ and
// even more // after the first // but they will be 
// ignored because they are all in the comment.

The comments illustrated in the preceding code are not particularly useful comments, but they show various ways in which comments in C can be employed.

Comments with tags (1)(6) are old-style C comments. The rules for these are simple – when /* is encountered, it is a comment until */ is subsequently encountered, whether it appears on the same line or several lines later. / * (with a space between them) and * / (with a space between them) are not valid comment indicators.

C comments that have been adopted from C++ are shown from tags (7) to (10). When // is encountered, it is a comment until an End Of Line (EOL) is encountered. Therefore, these comments cannot appear anywhere like C comments can. Likewise, / / (with a space between them) is not a valid comment indicator.

C comments are more flexible, while C++-style comments are more obvious. Both styles are useful. We'll use both throughout this book.

Now that we know the various ways we can format comments, let's consider some effective ways for using them.

Some guidelines on commenting code

One of the best guidelines for commenting in code is the same guideline to follow in life. This is sometimes called the Goldilocks Principle, also known as the Three Bears Principle, named after the children's fairy tale, Goldilocks and the Three Bears. The essence of this guideline is not too much; not too little; just right. However, just right is subjective, it depends on several factors, and will be different for each situation. Your own judgment and experience must be your guide to your Goldilock's moment.

Here is a list of essential guidelines to follow when commenting on your code:

  • Assume the reader already knows the language: You are not teaching your reader how to code. Do not explain the obvious features of the language. You are explaining the non-obvious aspects of your code.
  • Write in full sentences with proper capitalization and punctuation: Comments are not code. They are the words you are writing to yourself or other readers of your code. Your comments will be much less cryptic and more easily understood if you follow this guideline.
  • Comment on unusual uses of the language: Every language has oddities and idiosyncrasies that might not be used often or might be used in unexpected ways. These should be clarified and highlighted.
  • Try to comment in a way that is resilient to code changes: Very often, as code changes, comments are not necessarily updated to match those changes. One way to mitigate this issue is to put comments in globs at the beginning of functions or to precede blocks of code rather than them being interspersed within code blocks. This is so that if those code blocks change, the comments are still valid. You will see examples of how to do this throughout this book.
  • Comment at a high level: Describe the intent of the code and the way it attempts to solve a problem. This guideline goes hand in hand with the first guideline we mentioned. The higher the level the comments are describing, the less likely they will need to be changed as the code changes.
  • Convey your intent: With your comments, strive to convey the intent of the code you are writing, why the code is needed, and what the code is trying to accomplish. What the code is actually doing should come from the code itself.

I am often surprised when I revisit a piece of code I wrote 6 months ago. Too often, I find that I am scratching my head asking why did I do this? or what was I thinking here? (both are cases of too little commenting). I also find that when I change code, I have to delete numerous comments that are no longer necessary (a case of too much commenting). I rarely find that I have commented too much when I have focused on the intent of the code (what was I trying to do here).

At one point in my career, I came across a programmer whose comments were completely divorced from the code that was there. I concluded that this programmer initially intended their algorithm to work one way, but then modified the code so significantly that the comments no longer matched the actual code at all. When I saw that programmer's name in subsequent code, after careful inspection, I simply deleted the code comments because I found them to be irrelevant. Please do not do this unless you are absolutely certain you understand the code and that the comments do not match the code.

Learning how to effectively comment on code is a lifelong challenge. I do not suppose you will learn this quickly. You will learn this after years of examining your code and making your code clearer to yourself, let alone making your code clearer to others. As we work through various C example programs, I intend to demonstrate a variety of useful and resilient commenting techniques.

Adding comments to the Hello, world! program

Now that we have explored the various ways we can comment on code and commenting styles, let's copy hello1.c to hello2.c and add appropriate comments.

You can either copy hello1.c to hello2.c with your command interpreter or, in your editor, open hello1.c and immediately save it as hello2.c. Regardless of how you do this, you should have both hello1.c and hello2.c in your Chapter1_HelloWorld directory.

In your editor, modify hello2.c so that it appears as follows:

/*
 * hello2.c
 * My first C program with comments.
 * by <your name>
 * created yyyy/mm/dd
 */
#include <stdio.h>
int main()
{
    printf( "Hello, world!\n" );
    return 0;
}
/* eof */

Note how the * character at the beginning of each line providing a comment makes it clear that there are several lines of comments in a group; the group begins with /* and eventually ends with */. Compile, run, and verify this program. Make sure you haven't introduced an accidental character here or there, which is always possible and should always be verified.

This is now a complete program. We know from the evidence from hello1.c that the program is correct – it displays our intended message in the way we desire. The first six lines of comments provide minimal information about the program's author and the date it was written. This program's heading information can be simple or it can be more comprehensive. For now, we will keep such heading information simple.

The program itself is so simple that anyone who understands C would know that a simple message is printed. No further commenting is required here.

Finally, we mark the end of the file with a comment; the only benefit to such a marking is when there are multiple editor windows open and/or programs get very long. This simple demarcation lets us know we're at the EOF. This final EOF indicator is entirely optional and becomes more of a stylistic preference than a practice with rigorous rationale.

I have found that in every programming language I have used, my commenting style has adapted to the simplicity or obtuseness of the given language. When I programmed in assembler language at university or later in an early version of Fortran 4, I commented on almost every line. However, for C++ or Objective-C, I found I commented only sparsely or in globs – large sections of comments that explain a concept or programming solution.

Furthermore, even within a given language, when the problem being solved is unusual or I am using a novel approach to its solution, more comments are required.

In the remainder of this book, depending on the code sample, we'll explore various useful commenting practices that are effective, even when the code is subject to change.

Learning to experiment with code

Now that we have gotten our basic program to work (woo-hoo!), we can turn our attention to learning how to intentionally break it (ouch!). This is so that we can learn more about what the compiler is trying to tell us. What it is telling us isn't always clear, especially while we are learning.

Once you have mastered the language, there would be little need to do this (yay!). However, while we are learning the language, becoming familiar with the various kinds of compiler error messages is essential and, ultimately, will save us many hours/weeks of debugging, which might have been prevented early on in the iterative program development cycle. Please do not skip this essential step as you learn C as it will save you many hours/weeks.

So, using the full program development cycle outlined earlier, inject the following errors into your source file. When you see the error messages, try to correlate them with what you just did to cause them. After each one, correct the error and recompile it to verify the fix:

  • Remove { from hello2.c. Save it and compile it. What errors does the compiler give?
  • Put { back in its appropriate place and remove }. What errors does the compiler give?
  • There are three other paired punctuation marks: <> and (), which occur twice, and "". What errors does the compiler give when you remove the opening of the pair and the closing of the pair? Put them back after each experiment.
  • Remove ; from either line. What error messages does the compiler give?
  • Comment out the return 0; line. What error messages does the compiler give?
  • Change int main() to int MAIN(). What does the compiler tell you?
  • Similarly, change printf( to printout(. With this error, you should be able to see what linker messages look like.
  • Now, comment out #include <stdio.h>. You should also see linker errors telling you they can't find the printf() function.
  • Return hello2.c to its original state. Compile, run, and verify the program is both correct and complete.

If you get more than 23,000 lines of error messages from the compiler, I would really like to know. Please email me with the details of your experiments.

Summary

Whew!

In this chapter, you've learned an enormous amount about the program development cycle and how to set up your C development environment. Getting Hello, world! to compile and run is a far larger accomplishment for beginners than they might imagine. You have a working text editor, a working compiler, and you've begun manipulating programs in a command-line interpreter. You have probably experienced frustration to an extent you've rarely experienced before. I, like many programmers before you, feel your pain. We have all lived that pain. And with luck, I'm here to help take some of that pain away. In the rest of this book, we'll explore various ways to optimize this whole experience.

You have begun to learn that programming is about solving problems. While we haven't solved many interesting programming problems yet, you are just beginning your journey from simple programs and problems to far more complex programs and problems. We'll encounter a few of these in later chapters.

Furthermore, you are now aware of ways to make your programs clearer to both yourself – especially months after you've written the code – and to others who might later be tasked with modifying your code for new demands.

To be sure, getting a C++, C#, Objective-C, or JavaScript environment up and running would be similar yet subtly different.

In the next chapter, we will go into much more detail about how Hello, world! works and then modify it in some interesting ways to learn about statements, blocks, and functions – the building blocks of larger and more interesting programs.

Woo-hoo! You did it! Isn't programming fun?

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn essential C concepts such as variables, data structures, functions, loops, and pointers
  • Grasp the core programming aspects that form the base of many modern programming languages
  • Work with updated code samples and cover array declaration and initialization in detail in this new edition

Description

The foundation for many modern programming languages such as C++, C#, JavaScript, and Go, C is widely used as a system programming language as well as for embedded systems and high-performance computing. With this book, you'll be able to get up to speed with C in no time. The book takes you through basic programming concepts and shows you how to implement them in the C programming language. Throughout the book, you’ll create and run programs that demonstrate essential C concepts, such as program structure with functions, control structures such as loops and conditional statements, and complex data structures. As you make progress, you’ll get to grips with in-code documentation, testing, and validation methods. This new edition expands upon the use of enumerations, arrays, and additional C features, and provides two working programs based on the code used in the book. What's more, this book uses the method of intentional failure, where you'll develop a working program and then purposely break it to see what happens, thereby learning how to recognize possible mistakes when they happen. By the end of this C programming book, you’ll have developed basic programming skills in C that can be easily applied to other programming languages and have gained a solid foundation for you to build on as a programmer.

Who is this book for?

If you're an absolute beginner who has basic familiarity with operating a computer, this book will help you learn the most fundamental concepts and practices that you need to know to become a successful C programmer. If you're an experienced programmer, you'll find the full range of C syntax as well as common C idioms covered in the book useful.

What you will learn

  • Implement fundamental programming concepts through C programs
  • Understand the importance of creating complex data types and the functions to manipulate them
  • Develop good coding practices and learn to write clean code
  • Validate your programs before developing them further
  • Use the C Standard Library functions and understand why it is advantageous
  • Build and run a multi-file program with Make
  • Get an overview of how C has changed since its introduction and where it is going

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 30, 2022
Length: 742 pages
Edition : 2nd
Language : English
ISBN-13 : 9781801078450
Vendor :
DeepMind
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. £16.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Aug 30, 2022
Length: 742 pages
Edition : 2nd
Language : English
ISBN-13 : 9781801078450
Vendor :
DeepMind
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
£16.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
£169.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts
£234.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just £5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total £ 107.97
Learn C Programming
£35.99
Learn Python Programming, 3rd edition
£35.99
Deciphering Object-Oriented Programming with C++ [WARNING: NOT FOR USE IN OTHER MATERIAL/SEE CONTRACT]
£35.99
Total £ 107.97 Stars icon

Table of Contents

36 Chapters
Part 1: C Fundamentals Chevron down icon Chevron up icon
Chapter 1: Running Hello, World! Chevron down icon Chevron up icon
Chapter 2: Understanding Program Structure Chevron down icon Chevron up icon
Chapter 3: Working with Basic Data Types Chevron down icon Chevron up icon
Chapter 4: Using Variables and Assignments Chevron down icon Chevron up icon
Chapter 5: Exploring Operators and Expressions Chevron down icon Chevron up icon
Chapter 6: Exploring Conditional Program Flow Chevron down icon Chevron up icon
Chapter 7: Exploring Loops and Iterations Chevron down icon Chevron up icon
Chapter 8: Creating and Using Enumerations Chevron down icon Chevron up icon
Part 2: Complex Data Types Chevron down icon Chevron up icon
Chapter 9: Creating and Using Structures Chevron down icon Chevron up icon
Chapter 10: Creating Custom Data Types with typedef Chevron down icon Chevron up icon
Chapter 11: Working with Arrays Chevron down icon Chevron up icon
Chapter 12: Working with Multi-Dimensional Arrays Chevron down icon Chevron up icon
Chapter 13: Using Pointers Chevron down icon Chevron up icon
Chapter 14: Understanding Arrays and Pointers Chevron down icon Chevron up icon
Chapter 15: Working with Strings Chevron down icon Chevron up icon
Chapter 16: Creating and Using More Complex Structures Chevron down icon Chevron up icon
Part 3: Memory Manipulation Chevron down icon Chevron up icon
Chapter 17: Understanding Memory Allocation and Lifetime Chevron down icon Chevron up icon
Chapter 18: Using Dynamic Memory Allocation Chevron down icon Chevron up icon
Part 4: Input and Output Chevron down icon Chevron up icon
Chapter 19: Exploring Formatted Output Chevron down icon Chevron up icon
Chapter 20: Getting Input from the Command Line Chevron down icon Chevron up icon
Chapter 21: Exploring Formatted Input Chevron down icon Chevron up icon
Chapter 22: Working with Files Chevron down icon Chevron up icon
Chapter 23: Using File Input and File Output Chevron down icon Chevron up icon
Part 5: Building Blocks for Larger Programs Chevron down icon Chevron up icon
Chapter 24: Working with Multi-File Programs Chevron down icon Chevron up icon
Chapter 25: Understanding Scope Chevron down icon Chevron up icon
Chapter 26: Building Multi-File Programs with Make Chevron down icon Chevron up icon
Chapter 27: Creating Two Card Programs Chevron down icon Chevron up icon
Epilogue Chevron down icon Chevron up icon
Assessments Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(6 Ratings)
5 star 83.3%
4 star 0%
3 star 16.7%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Rachel barber Oct 30, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
starts from the ground up avoids getting bogged down early in the book and teaches at a good pace for beginners.good idioms and some history here and there with well thought out examples for intermediate readers.
Amazon Verified review Amazon
Sarthak Nov 27, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Sometime It's worthwhile to understand past data to connect the dots for every "WHY ?" questionsarises out of curiosity and for sake of understanding that topic thoroughly.Thank you Jeff for keeping Historical Notes.I loved the used analogies and practical examples to make us understand topic thoroughly.
Amazon Verified review Amazon
kfay Nov 02, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I found this book to be very easy to read and follow. The advice given in the Preface was important not only for learning C from this book effectively but also to learn from any other. Don't skip the preface.The source code formatting is sometimes a bit wonky (line breaks in the wrong places cause the most compiler errors) which is sometimes frustrating. But I was thankful that all ofsource code in the book is also available for download from Github. That made figuringout where I went wrong with my own version of each program a bit easier.I noticed that the 2nd edition contains additional chapters, one of which includes two complete but simple card games. It seems as if the entire book leads up to being able to build and understand these two complete programs.Is this the only book you'll need to learn C? No, I don't think so, but after working through it, I think I am now well prepared to undertake the next level of learning C. There are also lots of example programs which may come in handy later.
Amazon Verified review Amazon
Tim Kellermann Dec 01, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
After reading this book, I can confidently say it contains all the information one would need to drastically improve their C programming knowledge. From beginners downloading and configuring a compiler to an intermediate level with pointer and memory allocation, this book has it all. I was pleasantly surprised by the explanation and examples of pointers. Pointers can be a serious sore spot for many new programmers, but this book has an eloquent explanation that I found extremely helpful. Another big takeaway for me was that this book contains information on good programming practices such as creating a multi-file program that is easily comprehensible for others. Learning about good programming practices will benefit every new programmer when entering the software development or embedded systems industries. Therefore, I will be recommending this book to all who are interested in learning C or embedded systems. Thank you and happy coding!
Amazon Verified review Amazon
Krishnan Raghavan Nov 14, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I am coming back to C Programming language after a very long time and this book helped me with getting up and running quite nicely. This book is not specific either for beginners or advanced programmers but kind of touches the sweet spot where both kinds of readers can benefit from the book. For beginners, this book handholds and teaches a new programming language by covering the theory part with many code examples that should be typed and practiced. Advanced programmers can skip the theory part and read and understand the code along with practicing the same for consolidating their knowledge. The questions at the end of each chapter ensure that the topic covered in the chapter is well understood by the reader. The author suggests that experienced programmers can skim through the theory part but I would advise against the same. If nothing else, it can work as a refresher to the concepts that we are aware of but over a period of time kind of forgotten.This book is divided into four parts with each part covering a specific aspect of C programming. The first part covers the fundamentals of C programming where the author starts with writing a simple Hello World program and then introduces more basic concepts like what is the structure of a program and data types in the subsequent chapters. Other topics that are covered as part of this block are a few important topics like loops, iterations and enumerations.Part 2 covers more advanced topics and complex data types where the author takes the reader through a journey of understanding what are structures and how to create and use them in your program. Arrays are also introduced in this part of the book covering both single as well as multi-dimensional arrays. Chapter 13 onward, the reader is introduced to very important C concepts of Pointers and how pointers work with arrays and strings. Pointers are one of the most difficult topics and the author has done a reasonably good job of explaining the same. In my personal opinion, Pointers should be a book by itself.Part 3 covers Memory Manipulations techniques where the author explains how memory allocation works.Part 4 covers the different Input and Output formats that are available in C including covering the topic of how to read and write from a file.Part 5 basically helps the reader understand how to create and manage programs consisting of multiple files. i.e. instead of writing a program that has a single source file, how relevant part of the program can be called from multiple source files making it easier to maintain an application.The last chapter helps reader consolidate their knowledge by writing a game using all the techniques learnt from different topics.At the end of each chapter, there are questions which help consolidate the lessons learnt in that particular chapter which ideally should not be skipped. There is a concise summary at the end of each chapter.I found the book to be very well written as it takes the user on a journey about learning C programming. Kudos to Jeff Szuhay for writing such a wonderful book.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.