Session 02 - Road to GSOC 2018!
Our second session started at Universidad Nacional de Ingeniería and we were so exciting for our second class and the classification of Perú to the FIFA World Cup!!
… so much that we went with our T-shirts of the selection.
The class started with a exam about the last class. The exam was about some LINUX commands, file creation and editing with VI.
The topic of our session was about C, but first, I’m going to write about commands of GIT:
GIT is a version control software designed by Linus Torvalds, GIT allow us to create repositories and share our code with other member of our team.
The most common commands for the majority of projects are:
$ git init → That allow us to create a new repository
$ git add. → That allow us to load the data to a staging area.
$ git commit -m “comment” → That command allow us to load the files to the head and put a comment about the load.
$ git push → that allow us to send the data to the remote repository, in my case, mi GitHub Repository.
C is a programming language developed by Dennis Ritchie between 1969 and 1972. C is a predecesor of B language, and now is the most famous language for creating system software. C is known for its efficiency and versatility to work with other languages like assembler, C++, python, etc.
C is an important topic for our sessions because the kernel of Linux is programming on C, and there are a lot of apps written in C. C offers structures and pointers that are very useful either to optimize code or create new programs.
The last standard for C is the ISO / IEC 9899:2011. This change was basically by some updates to the C++ STL libraries, and the need for C to be able to run with these C++ standards.
In Linux, the compiler for C is gcc, we can install the compiler with:
$ sudo dnf install gcc.
We started with the classic Hello_World:
but, now we are a study the implications of a simple hello world.
The compilation process.
The compilation process has 3 steps: compile, link and execute.
For compile: Here, the compiler analyzes the syntax and semantics of the preprocessed source code and translates it, generating a file containing the object code.
$ gcc -c hello_world.c
For Link: The link is the process of references objects to other modules (libraries)
$ gcc hello_world_1.o -o hello_world_1.exe
For Execute: We can finally execute the program with the next command.
Analyzing the code
The declaration of the main menu is int main, it is declared int because it returns an integer, for them we place return 0 at the end.
Also we can put exit_success or exit_failure instead of return 0:
Now, check the follow code:
What is argc and *argv ?
argc is the number of arguments that our program receives when it is called from the command line (and from any other side).
*argv is the arrangement of arguments, the first argument is the name of the executable, so if we don’t pass arguments to the program, it will print 1.
String and Pointers
The strings are a way to arrays, and allow to put characters or pointers.
The strings have some particularities, for example, the end of the line is determined putting \0 into the last position. The jump of line is representing by \n.
There are many ways to initialize a string:
If we want to go through the string, we can go through it as if it were an array through a loop.
Now, that we have the basic of the string, we can use pointer for through the string.
The pointers are variables that save the address memory of other variables. There are 2 important characters:
& → the ampersand allow to have the address of memory of any variable
* → the asterisk have 2 functions. The firs function is for declare a pointer, and the other function is for get the value of the variable.
Here, we are a declare a pointer py1 into the line 16, and into the line 17 we assignment the address memory of y1.
Into the line 19, we are using the second function of the *, *z have the value of x.
The structs in C allow define a object and asigne properties, then we can have different types of object with the same structure, for example, we declare the structure of a complex number:
The struct complex have 2 attributes, the real part and the imaginary part. Then, we can define a generic struct complex a declare different complex numbers:
Here, also we have a function that add two complex numbers.
A MakeFile file, allow to reduce the process for compile a C program. Basically the makefile run a script for compile, link and execute a determinate program. That is so util when yo work on a big project and you can’t compile programs every time that you need a run some code.
This example allow to compile a file “hello_structs_1.c”, link the object and execute depending of the SO (Windows or Linux). Finally there are other function called clean.
Clean allow to compile again when you changed the source code.
For run the scrip, type:
$ mingw32-make run
if you changed your code, type:
$ mingw32-make clean
$ mingw32-make run
Here is the repository that I used:
And thanks for getting here!!
… And thanks to the teacher Julita and the teams of Fedora, Gnome and LinuxFundation!