Friday, June 24, 2016

C - Introduction

C is a programming language used in many fields. It's a low-level language, which means that you will handle some low-level computer features as the memory usage. It's used in many microcontrollers as it requires a minimal installation and runtime support. Other programming languages (as Java) will need a virtual machine which will run the application, with C, at compile time, the runnable application will be build in a machine language yet, so less memory consumption will be needed by the machine (as it don't need a sideway application as the virtual machine), and less stockage too (as no virtual machine must be installed).

How to build an application with C? When you write the commands you want to run in your application, you will do it in a text plain format file (ASCII encoding), with this file (or a set of files), you will call the compilation program which will build your application (gcc, https://gcc.gnu.org/). This will build your application in four steps:
  1. Preprocessing. Basically, it will put all your written program in a single file; this is, every time you wrote #include it will include the given file. It will also concatenate all the single lines you broke for readability.
  2. Compilation & Assembly. It will traduce your text plain format file into a machine language format. This is one of the most important steps, where you will know if you made some syntax errors the machine don't understand. Also, the result of these step will be machine specific; you can't build your application in a Linux platform and try to run it on a Windows. The compilation step will build an intermediate human readable result, and the assembly will build a machine code.
  3. Linking. This is the last step. It will include all the generated code into a single runnable file. It will include all your psot-compiled files, and additional required libraries. This will result in a single runnable file.
Let's see a simple example: a "Hello World".


For the current simple example, I've used no IDE, only a simple text editor (vim) and the compilation was made in a terminal with gcc in a Linux platform. The example shown, is composed by a standard library import at the first line (this import some basics commands as the printf). In the line 3 we declare the main entry point of the application. This is needed in all the application by the compiler a to know where the application must start. Without this declaration, your machine will not know at which instruction start. The main method declaration can be more configurable (with inputs) but for the current example, it's enough. At line 4, we use the instruction printf which will print in the output console the message given as the argument (in this case "hello world"). The final character \n means that a new line will be printed at the end of the text, otherwise, you will continue with the cursor at the end of the current line. And finally, the return 0; at line 5 indicates the return code of our application. The value of 0 is mainly used to indicate that all went good (it's only a convention, not a rule); and negative values indicate different kind of errors (each one will be defined by the user as it needs them).

After writing this application in a file named helloworld.c you can compile it with a terminal:


We can see that we start with our single file helloworld.c. Then we use gcc to compile and generate our runnable application. gcc accepts as first argument a list of file which you want to compile, and with the -o parameter, we specify the file of the output we want. The output will be the generated runnable application, which can be run as shown in the figure.

Compile your application with a terminal can be a big headache, as your application grown you will have more and more files. The best option (also for edition) is to use an IDE, in the next articles I'll use Geany (https://www.geany.org/) which is a lightweight IDE. The IDE use to help you for both compilation and edition of your applications, as the compilation will be automatic (it will detect the files you included in your project to add them at the compilation time) and you can have some useful autocomplete when editing.



No comments:

Post a Comment