Contents |
Chapter 0 - A very quick introduction to the C Programming Language
Prerequisites
To start learning the C Programming Language, you will first need to get the necessary tools. The first one is a simple text editor (you can find a few in C resources:IDEs) and the second is a compiler (many are listed in C resources:Compilers).
DID YOU KNOW THAT if you have a GNU/Linux system, you probably have a pretty good compiler installed already:gcc. Open up a terminal and typegcc --version. This will return gcc's version number if it is installed. If it isn't, consult your Linux distribution's help files to find out how to add it.
"Hello world" program
When learning a new programming language, it is traditional to start off by writing a program that displays "Hello World!" on a suitable display device (normally a computer monitor, although it might be something else, such as a Braille display or a line printer).
In order to get a C program to work, you have to be able to write it, save it, compile it, link it, and run it in such a way that you can see the output. A simple "Hello World" program demonstrates what at first can seem like quite a complicated process, without the need to worry about the complication of a more sophisticated C program too. Here's its source code:
#include <stdio.h> int main(void) { puts("Hello World!"); return 0; }
Copy this program into your text editor, save it as a file with a name like "hello.c", and compile it using the compiler of your choice. If you get everything right, you will be able to run the program, and it will (or at least should!) display
Hello World!
on your standard output device.
"Hello world" program explanation
The first line, #include <stdio.h>, is a preprocessing directive, which tells the compiler to incorporate a standard header into the program. This header carries information about certain standard library functions - in this case the one we're interested in is the puts function.
The second line declares the function int main(void), which is the program's starting point. When you compile and run the program, it will start from this function.
In the third line we make a call to the function puts(), the purpose of which is to put a string onto the standard output stream. The string is defined between the two double quotation marks ("). A string embedded in source code like that is known as a string literal. Several rules exist to allow for expansion of special characters within string literals, which we'll cover later - to start with, it's enough to know that any combination of purely alphanumeric characters (numbers and letters) will be interpreted as-is.
Finally, the fourth line returns control to the operating system with a status of success - more on return status later. The techno-legal arguments and qualifications as to whether and when this line is necessary can be subtle, but for now and for portability's sake, it's safe to say that it's required.










