C Basics and Compiling
Compiling and running a program takes the program through the following steps:
- Editor - You edit the source code on disk
.c
- Preprocessor - This program processes the code.
- Compiler - This creates object code and stores it on disk.
- Linker - This links object code with libraries and creates
.out
. - Loader - This puts the program in memory.
- Execution - Each instruction in memory is executed in the flow of the program.
There are four kinds of files that we work with:
- Source Code Files
*.c
- Contain function definitions.
- Header Files
*.h
- Contains various preprocessor statements.
- Allow source code files to access externally-defined functions.
- Object Files
*.o
or*.obj
.- Contain function definitions in binary form.
- Binary Executable
- They are the output of the linker.
- Made from a few object files.
Preprocessor
Preprocessor commands start with #
and look like so:
#include <stdio.h>
#include
This allows us to access function definitions defined outside of the source file.
#include <stdio.h>
The preprocessor pastes the contents of <stdio.h>
into the source code at the location of the #include
statement.
Functions must be declared in the code before they are called.
#define
This is used to include header files which are used to define constants.
For example, if we define the following:
#define MAXNUM 999
then the preprocessor will turn the following:
int i = MAXNUM
into this:
int i = 999
Using #define
makes your code more legible as the values are labelled in the code.
Other Preprocessor Commands
#define
#include
#undef
#ifdef
#ifundef
#error
#if
#else
#elif
#endif
#pragma
The Compiler
After the program has been preprocessed it is compiled by the compiler. It turns source code into object code.
Object code is a non-executable binary version of the source code.
The compiler can be invoked as:
$ gcc foo.c
if we just want to make object files then we can run it like so:
$ gcc -c foo.c
- -c - Just compile the source files to object files.
Generating object files can be useful in large projects where you don’t want to compile everything, or for proprietary code.
You can use the following to name the output of the compiler:
$ gcc foo.x -o foo
-o
- Name the output executable.
The Linker
This links together object files into a binary executable. It is a separate program called ld
.
The normal way to link together several files is like so:
$ gcc foo.o bar.o baz.o -o myprogram
C Language Basics
This simple example has the following components:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
return 0;
}
- The
main()
function:main
is a C keyword that is reserved.- The function is called automatically for us. Ideally we should not call it ourselves in the code.
- Statements:
- Each statement in C needs to be terminated with a semicolon
;
. - Functions should return a result, hence we
return 0;
.
- Each statement in C needs to be terminated with a semicolon
- Identifiers
- These are words used to represent certain program entities such as variables and functions.
- They have the following rules:
Rule Example Can contain a mix of characters and numbers W3c
Cannot start with a number 2assignments
Must start with a letter or underscore Number1
_area
Can be of mixed cases whoAmI
Cannot contain any arithmetic operators Sm*il
Cannot be any other punctuation marks (separators) !@#$%^&*(){}
Cannot be a C keyword/reserved word main
printf
Cannot contain a space Oh yay
Identifiers are case sensitive Happy
≠happy
The following keywords are reserved:
auto | else | long | switch |
---|---|---|---|
break | enum | register | typedef |
case | extern | return | union |
char | float | short | unsigned |
const | for | signed | void |
continue | goto | sizeof | volatile |
default | if | static | while |
do | int | struct | _Packed |
double |
Basic Data Variables and Types
All data in C has to have a specified type. There are the following types:
- Integers:
char
- This is ASCII text only.
- They are only quoted in single quotes
'
.
int
short
long
long long
- Unsigned Integers:
unsigned char
unsigned int
unsigned short
unsigned long
unsigned long long
- Floating Point Numbers
float
double
Constants
These are entities that appear in the program code as fixed values. There are four types on constant:
-
Integer:
const int FOO = 999;
-
Floating Point:
const double FOO = 1.23e4;
-
Character:
const char FOO = 'l';
-
Enumeration:
enum City {Manchester, Liverpool, Leeds};
Attempts to modify a constant will result in an error.