Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



C

From Juneday education
Revision as of 09:47, 13 September 2018 by Henrik Sandklef (Talk | contribs) (Development environment for C programming)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


C (/ˈsiː/, as in the letter c) is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. - https://en.wikipedia.org/wiki/C_(programming_language)

Development environment for C programming

To setup a development environment for C programming, check out the page: Setting up your C development environment

C source code

source code written in the C Programming Language.

Object code

The compiler produces Object code, which is in a format that the computer understands. Object files can be linked to an executable form, e g a program.

C compiler

In most cases, especially in this course we don't need a deeper understanding of the concepts below. We do, however, believe it is good to have been introduced to the various steps in C compilation.

Let's assume we have a file called hello.c with the following content:

#include <stdio.h>

int main() {
  printf("Hello Cleveland\n");

  return 0;
}

We can compile this file easily:

$ gcc hello.c -o hello

and execute the program equally easy:

$ ./hello

Now it's time to look a bit more at what goes on when you invoke the compiler like above. Four steps are done automatically for you.

Preprocessor

With the preprocessor we can include header files, expand macros and perform conditional compilation. We can also use it for line control.

We can halt the compilation process (above) after preprocessing and look at the result by using the GCC command line option -E

$ gcc hello.c -E

or if we want to store the output in a file (hello.E):

$ gcc hello.c -E -o hello.E

Compiler

After preprocessing is performed the compiler continues with something we call compilation. Unfortunately this sub process is also the name of the entire process to go from C source code to an executable form of the source code.

The compiler reads the preprocessed file and generates assembly code (usually for one specific machine).

We can halt the entire compilation process after compilation and look at the result by using the GCC command line option -S

$ gcc hello.c -S

which stores the output in a file called hello.s.

Assembler

The assembler takes the generated assembly code and converted into executable machine code.

We can halt the entire compilation process after assembly and look at the result by using the GCC command line option -c

$ gcc hello.c -c

which stores the output in a file called hello.o.

Linker

The linker takes the generated machine code (usually referred to as object code) and turns it into an executable file, library file or an object file.

The linker is automatically invoked with the gcc command, so simply typing

$ gcc hello.c -o hello

will create our program. We can also go from an object file (produced by the assembler) like this:

$ gcc hello.o -o hello

Running your program

Let's assume you're located in /home/user/C-course and that you have a file called hello.c.

To compile this program you type: gcc hello.c -o hello.

To execute the program you need to tell bash where the program is located. We can do this in many different ways.

Absolute path

You can specify the complete path by typing:

/home/user/C-course/hello

By telling the shell exactly where the program is located it is quite easy to understand that the shell can find the program and then execute it.

Relative path using ./

Using an absolute/complete path all the time is a bit cumbersome so we need an easier way. And the good news is that there is an easier way. Before we proceed we need to do some recap.

Every directory has two rather special directories, . and ... . means the current directory and .. means the directory above. Using ., which is equivalent to /home/user/C-course since we're located in that directory.

Ok, recap done. Let's continue. If we look at the commnad line from the previous section: /home/user/C-course/hello and use that . is equivalent to /home/user/C-course we can replace /home/user/C-course/hello with . in the command line above. This means we replace

/home/user/C-course/hello

with

./hello

We can now execute the program easily. Fact is we can execute the program in many different ways using a relative path but we believe that the above is both easy to understand and easy to remember so let's stick to this.

Since we're telling bash where the program is located relative to . we say that we're using a relative path.

GCC

Cross compilation

Conditional compilation