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.
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.
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.
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.
Since in our first part of this article we said we expected you, the reader, to have some programming knowledge, in this part we want to help you get a on where C stands in comparison to other programming languages you might know. The choice of those languages was pretty tough because of various criteria, but in the end we stopped at C++, Perl and Python. Since programming languages can be classified in lots of ways (depending on paradigm, syntax or style, for example), we didn't try to find languages that are in the same category as C. Instead, since the aforementioned languages are pretty popular in the Linux world, we chose them, mentioning what every language's place is in the great scheme, what it's generally used for and of course, the differences between them and C. This article will be structured as follows: we'll start with important elements of every language, like variable declaration, typing or structure and compare that with how it's done with C. Thusly we hope to give you an idea about the language before we start. The parts this article is made of will be exactly like the announced structure of this article, to make comprehension easier.
Types, operators, variables
C++ was initially named "C with classes", which says a lot about its' relation with C. It is widely seen as a superset of C ( thus C++ the unary increment operator ++ ) , introducing object-oriented programming features. Types are essentially used the same, with bool being introduced in C++ for boolean operations. Basically, when talking about C and C++ differences, most of those come from the OOP concepts C++ has and C does not. For example C++ has operator overloading, an OOP-specific term, which means that an operator may have different implementations depending on the data it's operating on. For example, in C++ you can do this:
a << 2
Now, if a is an integer, this will do a bitwise operation on it (left shift by 2), but if a is an output stream, the above line will try to write a '2' to it. This kind of behavior is one of the reason C++ is criticized for allowing poor programming practices. Variables and constants are declared the same way as in C.
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.
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.
The export command is one of the bash shell BUILTINS commands, which means it is part of your shell. The export command is fairly simple to use as it has straightforward syntax with only three available command options. In general, the export command marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables. If you are unsure what this means read on, as this article will explain this process in more detail.
Frequently Used Options
-p List of all names that are exported in the current shell
Scala is a programming language that is starting to gain momentum in the last years. The popular TIOBE Index ranks it, as this article is written, as being more used than more popular languages like Haskell or Go. The TIOBE index, if you're not familliar with the name, is the somehow-authoritative source of information regarding programming language popularity. From the beginning we want to clearly state that this is not, by any means, an article on the language itself. It just aims to get the user up and running with an IDE for writing Scala code in the shortest time possible. That being said, let's get to it.
Installation and prerequisites
On Linux, the only prerequisite for installing Intellij IDEA, which is the IDE we'll be talking about, is the Oracle Java JDK. This is a specific requirement, as you can't use OpenJDK, so please note this before we start. So let's make sure we have the right JDK installed.
The automated build Docker image of The R Project for Statistical Computing “linuxconfig/cran-r” can be used to instantly deploy R programming software environment on any hosts given that you have docker already installed on your system.
The docker image with the R Project is compiled and runs on Debian GNU/Linux system.
Here we assume that docker is already installed on your system.First pull docker image:
The following is an example on how to pass and access command line arguments which a Python script. Save the following python script to a file eg. python-arguments.py
from sys import argv
name, first, second, third, fourth = argv
print"Script name is:", name
print"Your first argument is:", first
print"Your second argument is:", second
print"Your third argument is:", third
print"Your fourth argument is:", fourth
# Alternatively we can access "argv" argument list directly using range. For exmaple:# Print all arguments except script nameprint argv[1:]
# Print second argumentprint argv
# Print second and third argumentprint argv[2:4]
# Print last argumentprint argv[-1]
The Python raw_input() function is used to read a string from standard input such as keyboard. This way a programmer is able to include user inserted data into a program. Let's start with a simple example using python script to ask for an user name.
print"What is your name?"
print"Hello %s!"% name
First, we print string What is your name? telling the user what we expect him to input. Next, using the raw_input() function the standard input is assigned to a variable name. Lastly, we print the value of variable name to standard output.
$ python input.py
What is your name?
Hello Monty Python!
: My Python program produce a following error message upon execution:
SyntaxError: Non-ASCII character '\xc4' in file test.py on line 1, but no encoding declared;
Normally the above error message is displayed by python when other characters other then ASCII are used withing your code. The solution is to either remove all non-ASCII characters or include the bellow line into your code to enable UTF-8 encoding:
Author: Tobin Harding Here we briefly outline some of the major use cases for brackets, parenthesis, and braces in BASH scripting, see bottom of page for definition of these three terms.
Double parentheses (( )) are used for arithmetic:
((var = 3))
for ((i = 0; i < VAL; i++))
echo $((var + 2))