Chapter 1
Source Code

1 Files

Basically, a program in C is a sequence of functions (possibly just the special function called main(), if it's a small program), and each function in C is made up of an ordered sequence of commands, or statements, that tell the computer wich actions to perform in turn. These functions can be spread over several source files, and this is true mainly for large programs. Files with the “.c” extension are where we implement the functions, and files with the “.h” extension, also called header files, are where we say which implemented functions (along with other constructs of the language) we want to export to other parts of our program or to other programs. A large program is simply a set of “.c” and “.h” files. Usually, for each “.c” file there is a corresponding “.h” file, but this is not required by the language. We can, for instance, have a single header file, with its exported functions implemented across several “.c” files.

Now, let us begin with a program that fits into a single “hello.c” file. Type it with your favourite text editor, save it to your favourite folder, and compile it with your favourite C compiler. For instructions on how to do this last step, please refer to your compiler's instruction manual. So, after all this, here is everyone's typical first C program:

#include <stdio.h>

main()
{
  printf("Hello, World!\n");
  return 0;
}

The first line tells our program that we are going to use functions declared in a file called stdio.h. This file contains the elements of the language that the standard input and output library exports, which are essentially declarations of functions (among other things) that perform file operations like opening and writing, but also functions that write to the screen and read from the keyboard. Note that the file stdio.h is a text file that contains just declarations of functions and symbols — the implementations are elsewhere, usually in a library file of pre-compiled functions.

Next comes the implementation of the main() function. This is the function that gets called first when we execute our program, after compilation. The parentheses indicate that we are defining a function, not other items of the language, and inside the parentheses we place the arguments the function receives — in this particular case, none. Before the name of the function we say the type of the value returned. If omitted, as is the case here, it is assumed to be an int, that is, an integer. Next, between the braces, comes the body of the function. This is formed by the statements that the function executes, in the order they appear, when the function is called. In our example, there are two: a printf() function call, and a return statement. Statements end in semicolons. The printf() function is declared in stdio.h, and prints to the screen a string, which is a sequence of characters placed between quotes. The “\n” at the end of the string is the newline character, and forces text that is printed after its appearance to be displayed at the beginning of the next line on the screen. Notice that to call a function we also use the parentheses, and inside those parentheses we place the arguments the function is expecting to receive. We must declare functions somewhere in our program before we can call them, and that is why we included stdio.h before calling the function printf(). Finally, our main() function must return an int, because we omitted the return type before its name, and return 0; does just that.

2 Characters

Source files are written using the characters in the following list.

a b c d e f g h i j k l m
n o p q r s t u v w x y z
A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z
 
0 1 2 3 4 5 6 7 8 9
 
! " # % & ' ( ) * + , - .
/ : ; < = > ? [ \ ] ^ _ {
| } ~

There are also present the space, the horizontal tab, the newline character, and the form feed character. These are used to separate the symbols, or tokens, from each other, and are known as whitespace. Also, inside strings there may appear the so-called escape sequences that follow.

\0The null character, at the end of strings
\aAlert or beep sound
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\'The character '
\"The character "
\?The character ?
\\The character \
\oThe character with octal code o
\ooThe character with octal code oo
\oooThe character with octal code ooo
\xxxThe character with hexadecimal code xx

Most compilers these days support other characters inside strings and character literals, but if we're writing portable code, we should stick to these characters.

3 Identifiers

An identifier in C is just a sequence of uppercase or lowercase letters (from a to z or from A to Z), digits (from 0 to 9), and underscores (_). Identifiers cannot start with a digit and they're case sensitive. Identifiers are used as names of variables, functions, types, or other constructs of the language. Here follow some examples: banana, _5th_kong, g3D, and HoardStruct.

Some words cannot be used as identifiers. These are called reserved words or keywords, because they're being used for something else. The following is a list of all the keywords of C.

auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
inline
int
long
register
restrict
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
_Bool
_Complex
_Imaginary