C program compilation process

C development on Linux – Introduction – I.

Introduction

What you’re just reading is the beginning of series of articles dedicated to development on Linux systems. However, with minor modifications (if any), you will be able to use this knowledge you will get by reading our series on any other system that uses the same tools (OpenIndiana, BSD…). This first article will deal gradually with the intricacies of writing C code on Linux. You are expected to have basic programming knowledge, either on Linux/Unix systems or on other platforms. The idea is that you should know the basics of programming, like what a variable is or how to define a structure. Even though, you will get this information from this article, we won’t insist very much on beginner-level concepts. A word of warning: we won’t include everything there is to tell about C, because that would take lots of space and of course, we don’t actually know everything about C.

Why C?

Some of you might argue that C is not the best beginner-level language ever. Agreed, but again, you’re expected to have some basic programming knowledge, for one. Second, C and Unix, and Linux afterwards, are intimately tied together that it only seemed natural to start our development series with C. From the kernel, of which a substantial part of it is written in C, to lots of everyday user-land applications, C is used massively on your Linux system. For example, GTK is based on C, so if you’re using Gnome or XFCE applications, you’re using C-based applications. C is an old, well-established programming language, vital tool in many parts in the IT world, from embedded systems to mainframes. Therefore, it is only fair to assume that C skills will not only enrich your CV, but they will also help you to solve many issues on your Linux system, that is only if you take this seriously and practice a lot by reading and writing C code.

Read more

C development on Linux – Types, variables, operators – III.

Introduction

As promised, starting with this part of our C development article, we will get started with learning, without further introduction. I couldn’t find no better way to start other than this, because types, operators and variables are an essential part of C and you will use them all the time when writing your own programs. For example, you can write a simple C program without defining your own functions, but it’s harder to do that without some variables, unless you wanna stick to “Hello, world!”. A variable is nothing more than a location in memory holding a value that can be altered (hence the name). But before you declare a variable you must know what kind of value you want it to hold, and here you will use types. And in order to operate on those variables, you’ll need…operators, of course. I intend to make this course as concise as possible, so I recommend attention and as usual, practice.

Read more

A quick GNU R tutorial to basic operations, functions and data structures

Introduction

In the last two articles we have learned how to install and run GNU R on the Linux operating system. The purpose of this article is to provide a quick reference tutorial to GNU R that contains introduction to the main objects of the R programming language . We will learn about basic operations in R, functions and variables. Moreover, we will introduce R data structures, objects and classes.

Basic Operations in R

Let us start with a simple mathematical example. Enter, for instance, addition of seven and three into your R console and press enter, as a result we obtain:

> 7+3
[1] 10

To explain in more detail what just happened and what is the terminology we use when running R, we say that the R interpreter printed an object returned by an expression entered into the R console. We should also mention that R interprets any number as a vector. Therefore, “[1]” near our result means that the index of the first value displayed in the given row is one. This can be further clarified by defining a longer vector using the c() function. For example:

Read more

while loop interactive C

C development on Linux – Flow Control – IV.

Introduction

You have already been exposed to a small part of what flow control is in our previous part, namely the section about relational operators. As you start writing more complex programs, you will feel the need to control the order in which your program executes various parts.
Flow control is present in most programming languages in one form or another, and what you’re about to read here is essential to writing C programs.

if/else/else if

This part of flow control is probably the most intuitive and simpler, although you can easily fall to the dark side and start writing unintelligible code with ifs. The idea is simple: if (condition_is_true) do_something; else do_something_else; . So it’s all about logic, binary logic that is, in which an expression can have two values: true or false. If you used C or Java, you’re used with the bool datatype. A bool variable can be only true or only false at a given moment. But C, although it doesn’t have the bool datatype, makes it easy to deal with binary logic, as you will see.

Read more

example scatter plot GNU R

A quick GNU R tutorial to statistical models and graphics

Introduction

In this quick GNU R tutorial to statistical models and graphics we will provide a simple linear regression example and learn how to perform such basic statistical analysis of data. This analysis will be accompanied by graphical examples, which will take us closer to producing plots and charts with GNU R. If you are not familiar with using R at all please have a look at the prerequisite tutorial: A quick GNU R tutorial to basic operations, functions and data structures.

Models and Formulas in R

We understand a model in statistics as a concise description of data. Such presentation of data is usually exhibited with a mathematical formula. R has its own way to represent relationships between variables. For instance, the following relationship y=c0+c1x1+c2x2+…+cnxn+r is in R written as

y~x1+x2+...+xn,

which is a formula object.

Linear regression example

Let us now provide a linear regression example for GNU R, which consists of two parts. In the first part of this example we will study a relationship between the financial index returns denominated in the US dollar and such returns denominated in the Canadian dollar. Additionally in the second part of the example we add one more variable to our analysis, which are returns of the index denominated in Euro.

Read more

C function prototype, definition,call

C development on Linux – Functions – V.

Introduction

The C standard library offers a plethora of functions for many usual tasks. Also there are lots of libraries for extra functionality, like GUI design (GTK+) or database interfacing (libpq). However, as you advance in the C programming world, you will soon find yourself repeating the same instructions in the same order over and over again and that will become time-consuming and inefficient. So you can just wrap all those instructions in a function and just call said function when you need it. Here’s what you’re gonna learn by reading this article, plus some useful tips that will make your life easier.

Creating your functions

For a simple start, let’s say you wanna write a calculator. We won’t focus on the interface (GUI vs curses vs slang vs CLI) as we’re interested on the internals. It would be clunky to not create a function for every operation you decide to support, unless there is one already, like pow() , defined in math.h, which returns the result of a base raised to a power. So, for example, for addition you will have a function named add() that takes two arguments, at least for now, and returns the result. So when the user chooses to add the number (s)he introduced, you just call the function with the numbers the user entered and you needn’t worry about anything else. These three terms that I wrote in italics are essential in understanding functions. A function usually (but not always) takes something, does a number of operations on that something and spits out the result. “Not always” because main(), as you could see before, can be called with no arguments, and there are other examples as well. But for now, let’s focus on our examples. The numbers that need to be added together are the arguments, that “something” you give the function for processing. The processing part is in the body of the function, when you tell it to add the numbers together. After that, the “spitting out” part is called returning a value, which is, in our case, the result of the addition.

Read more

How to install and use packages in GNU R

Introduction

GNU R offers a wide variety of packages for its users. There are all kinds of packages for R, which allow to display graphics or perform statistical tests. Some packages are designed for applications specific to a given industry. Many packages are already a part of the basic R installation, however, some of them need to be additionally installed into GNU R. This article will describe how to install and use packages under R.

What is a Package

A package is a set of functions, help files and data files that have been linked together. In order to use a package in R you need to first make sure that it is installed in the local library. In general, the one system-level library is used for storing the default R packages. You can, however, add additional libraries. You also need to remember about loading packages into your current R session. This is very important when using R. It is recommended that you do not load too many packages at the time. Loading a large number of packages may result in errors due to clashes of function names coming from two different packages.

Read more

Pointer to array

C development on Linux – Pointers and Arrays – VI.

Introduction

We have come to a crucial point in our series of articles regarding C development. It’s also, not coincidentally, that part of C that gives lots of headaches to beginners. This is where we come in, and this article’s purpose (one of them, anyway), is to debunk the myths about pointers and about C as a language hard/impossible to learn and read. Nonetheless, we recommend increased attention and a wee bit of patience and you’ll see that pointers are not as mind-boggling as the legends say.

Definitions and warnings

It seems natural and common sense that we should start with the warnings, and we heartily recommend you remember them: while pointers make your life as a C developer easier, they also can introduce hard-to-find bugs and incomprehensible code. You will see, if you continue reading, what we’re talking about and the seriousness of said bugs, but the bottom line is, as said before, be extra careful.

A simple definition of a pointer would be “a variable whose value is the address of another variable”. You probably know that operating systems deal with addresses when storing values, just as you would label things inside a warehouse so you have an easy way of finding them when needed. On the other hand, an array can be defined as a collection of items identified by indexes. You will see later why pointers and arrays are usually presented together, and how to become efficient in C using them. If you have a background in other, higher-level languages, you are familiar with the string datatype. In C, arrays are the equivalent of string-typed variables, and it is argued that this approach is more efficient.

Read more

Building basic packages in GNU R

Introduction

Whether you would like to share your code and data with other people or simply pack up your code in a concise way, the ability of building a custom package in GNU R may come useful to you. In this article we will outline as clearly as possible the process of building a basic package in R. This does not include more advanced knowledge on building R packages. This tutorial, however, will get you started. You may also find How to install and use packages in GNU R of help if you are not familiar with using R packages at all.

Creating a package structure

Every package consists of a set of functions that are programmed to apply with a common aim. Additionally, a sample data is often provided with the package in R. Let us now propose a simple example. Below we defined four R objects: two functions div() and pow() and two data sets in a form of two vectors data1 and data2.

Read more

C development on Linux – Structures – VII.

Introduction

We will continue in this part of our tutorial with the complex data types in C, and we will talk about structures. Many modern programming languages offer them, one shape or another, and so does C. As you will see later, structures allow you to manipulate data easier, by allowing you to store different variables of (possibly) different types under one single “roof”.

Beginning structures

Although I wanted to postpone the definition part for this sub-chapter, it seems like I couldn’t wait and included it in the introduction. Yes, folks, that’s what a structure is, and you will see in a whim how useful it is when I will show you some examples. One interesting parallel is the one referring to a database table: if you have a table called users (the unique name), then you will put in that table the exact data which pertains directly to the users: age, gender, name, address, and so on. But these are different types! No problem, you can do that with a table, just as you can do it with a struct: age will be an integer, gender will be a char, name will be a string and so on. Then you will be able to access the members of the table easily, by referring to the name of the table/member. But this is not a database course, so let’s move on. But before that, let’s take a short look at a logical aspect: you are invited to create structs with members that have something in common from a logical point of view, like the example above. Make it easier for you and the people that will later look at your code. So, let’s see how our users database table would translate in a C struct:

Read more

C development on Linux – Basic I/O – VIII.

Introduction

With this part of our C development on Linux article we are getting ready to get out of the theoretical zone and enter the real life one. If you followed the series until this point and tried to solve all the exercises, you will now have some idea about what C is about, so you need to get out in the wild and do some practical stuff, without which theory doesn’t have much value. Some of the concepts you’ll see below are already known, but they are extremely important for any C program on any Unix-like OS. Yes, the information is valid regardless of the OS, as long as it’s some kind of Unix, but if you’ll stumble onto something Linux-specific, you will know. We will treat concepts like standard input, output and error, in-depth printf() and file access, among others.

Read more

vim-logo

Vim editor basics in Linux

Vim needs no presentations: it is probably the most loved text editor by system administrators, also due to the fact that it is a clone and an improvement of the original Vi, which is included by default in practically all Linux and Unix based operating systems. Vim can be quite intimidating at first, and it has a steep learning curve; learning how to use it, however, can really increase our productivity. In this article we will learn the Vim basics.

In this tutorial you will learn:

  • How to install Vim
  • Why Vim is a “modal” text editor and what are the Vim modes
  • How to tweak Vim look and feel using the ~/.vimrc configuration file

Read more