Since the release of K&R, every programmer has become familiar with the Hello, world! program. I find that learning to build this program from start to finish is an excellent introduction to the tools available in Linux.
First, we want to create a directory for our programs. I recommend ~/Documents/Programming, because this is a simple
Almost every useful program requires loops and tests, which determine how the program will proceed. Without this feature, a program might as well be a text file, which generates its contents on every execute.
There are three basic loops which we generally recognize.
The for loop sets an initial condition when we enter the loop, then
If we're being honest, we don't start programming because we want to print out clever one-liners to a terminal. We want to calculate something complicated or massive, and we want to do it quickly.
Variables, functions, and symbols
We all know what a number is, right? We may behind many countries in mathematics and science, but
We've looked at the basics of performing basic math in C, but there are a few more things we should discuss before we write any code.
We know that there are many different kinds of numbers. For example, we have signed numbers (which can be positive or negative) and unsigned numbers (which are assumed
It's time to delve into one of the enormously useful topics of programming: working with text.
Characters and Strings
In the past few programs, we've used the printf() function quite a bit. This magical little function generates strings and prints them to the terminal. But what exactly is a string?
First, we have to understand what a
Let's be honest, folks. We don't expect to work with a few clearly defined variables forever. Sometimes we need to handle a lot of things at once, or at least a group of related things.
The simplest method for lumping a group of things together is the basic data array.
An array is a set of
At this point, we've written and compiled a few C programs, and I'm sure you're already noticing the patterns. Still, it sometimes helps to have these patterns clearly defined.
When we talk about a line of code, we don't necessarily mean one physical line on the page. A line of code is one logical
It's finally time to look at those arguments we pass into main() - int argc and char *argv.
You might have noticed that vi and gcc are both programs. You might also have noticed that we always call these programs with arguments.
You might also notice that, up to this point, we haven't called any
We know how to send input from the terminal. Now we'll look at the right and wrong ways to do keyboard input within your program.
Reading Input from Standard Input
Have you ever seen a program which used a prompt to ask for something? For a simple case, it could look like this:
There are numerous functions
Ever wondered what the whole #include thing really means? Of course you have.
There are basically three stages that gcc goes through when it compiles your program: precompilation, translation, and linking. At this point, we're interested in that first stage.
The precompiler is the program which reads and organizes your C code for the translator to