Now that you've been introduced to a simple programming language in Python, we need to spend a bit of time talking about a more complex but powerful language called C. C is the original language of Linux and has been around for many decades, but it is still widely used by open source developers. It is similar to Python, but is also a bit different; since you may need to understand and make changes to C code, you should be familiar with it and how it is used.
As with Python, you will need to have access to the language capabilities of C. These come in the form of a compiler and build system, which turns your text files that contain programs into machine code that the processor can actually execute. To do this, type sudo apt-get install build-essential
. This will install the programs you need to turn your code into executables for the system.
Now that the tools required for C/C++ are installed, let's walk through some simple examples. The following screenshot shows the first C/C++ code example:
The following is an explanation of the code shown in the previous screenshot:
#include <iostream>
– This is a library that is included so your program can input data using the keyboard and output information to the screen.int main()
– As with Python, we can place functions and classes in the file, but you will always want to start a program's execution at a known point; C defines this known point as the main function.int a;
– This defines a variable named a
, which is of the type int
. C is what we call a strongly typed language, which means we need to declare the type of the variable we are defining. The normal types are as follows:int
: This is used for numbers that have no decimal pointsfloat
: This is used for numbers that require decimal pointschar
: This is used for a character of textbool
: This is used for a true or false valueAlso note that every line in C ends with the ;
character.
int b;
– This defines a variable named b
, which is of the type int
.int c;
– This defines a variable named c
, which is of the type int
.std::cout << "Input value: ";
– This will display the string "Input value: "
on the screen.std::cin >> a;
– The input that the user types will be stored in the variable a
.std::cout << "Input second value: ";
– This will display the string Input second value:
on the screen.std::cin >> b;
– The input that the user types will go into the variable a
.c = a + b;
– The statement is the simple addition of two values.std::cout << c << std::endl;
– The cout
command prints out the value of c
. The endl
command at the end of this line prints out a carriage return so that the next character appears on the next line.return 0;
– The main function ends and returns 0
.To run this program, you'll need to run a compile process to turn it into an executable program that you can run. To do this, after you have created the program, type g++ example2.cpp –o example2
. This will then process your program, turning it into a file that the computer can execute. The name of the executable program will be example2
(specified as the name after the –o
option).
If you run an ls
command on your directory after you have compiled this program, you should see the example2
file in your directory as shown in the following screenshot:
By the way, if you run into a problem, the compiler will try to help you figure it out. If, for example, you were to forget to include the int
type before the int a;
declaration, you would get the error shown in the following screenshot when you try to compile the previous code:
The error message indicates a problem in the int main()
function and tells you that the variable a
was not successfully declared. Once you have the file compiled, to run the executable, type ./example2
and you should be able to obtain the following result:
We will not repeat the Python tutorial for C in this book; there are several good tutorials on the Internet that can help; for example, http://www.cprogramming.com/tutorial/c-tutorial.html and http://thenewboston.org/list.php?cat=14. There is one more aspect of C you will need to know about. The compile process that you just encountered seems fairly straightforward. However, if you have your functionality distributed among a lot of files, or need lots of libraries for your programs, the command-line approach to executing a compile can get unwieldy.
The C development environment provides a way to automate the compile process; this is called the make process. When performing this process, you create a text program named makefile
that defines the files you want to include and compile. Instead of typing a long command or set of commands, you simply type make
and the system will execute a compile based on the definitions in the
makefile
program. There are several good tutorials that discuss this system more; try visiting http://www.cs.colby.edu/maxwell/courses/tutorials/maketutor/ or http://mrbook.org/tutorials/make/.
Now you are equipped to edit and create your own programming files. The next chapters will provide you with lots of opportunities to practice your skills as you translate lines of code into cool robotic capabilities.
If you are going to do a significant amount of coding, you'll want to install an IDE (Integrated Development Environment). These environments make it much easier to see, edit, compile, and debug your programs. The most popular of these programs in the Linux world is called Eclipse. If you'd like to know more, start with a Google search or go to http://www.eclipse.org/.
18.188.38.142