chapter 1

up exercises solutions

next

 

chapter 1 chapter 2 chapter 3

Home keywords c bibliography

 

Chapter 1

first steps

    first program

In this chapter we will write down, compile and run our first programs. We will make use of the for-loop as an important control structure of most C/C++ programs. A more elaborated discussion of loops will be given in Chapter 2. Enter the following program and save it as c1_1.cc

#include <iostream>

void main()
{
	cout << "my first C++ output!\n";
}

The suffix .cc is the one of the standard file-endings for C++-files. Other commonly used are .c++ or .cpp. The choice depends mainly on your own flavor. Here we will always use .cc as a convention. Go to the command line and compile the program via

g++ c1_1.cc -o c1_1

This executes g++ which is a special version of GNU´c gcc with special options for C++. We will later try using gcc directly and observe some differences. g++ interprets the preprocessor options, compiles your program, links optional libraries and creates an executable. Arguments are the filename of the file to be processed (c1_1.cc) and via the -o switch the output filename (c1_1). The interested reader may have a look at the manpage (man gcc, man g++), the info files (info gcc, info g++) or gcc-webpage.

Now, simply execute your program and enjoy the output of your first self-written program:

c1_c
my first C++ output!

Well, this was very easy! Now let's have a look at the program.

If you compare with the list of keywords you will see that already the first command of the example program is NOT a keyword of the C-language. Indicated by # are compiler options, which for example tell the compiler to include other files in our program. Here we include the header filer of the iostream-library. This header file does not contain all the routines but just the declarations of them. The routines themselves are already compiled (this saves time!) to a library which is linked automatically to our program by the g++ compiler (because iostream is part of the standard C++ library, these elements are with g++ always linked automatically. Another example of an automatically linked part of the standard library is math, which we have to include, but not to link explicitly). gcc on the contrary does not automatically link this library, so we have to do it by hand as we will see later. The iostream library is a pure C++ library making use of the object oriented parts. For the moment we are not going to discuss it in detail, but we need one of the parts, "cout" to printout things on screen. cout is an object and "<<" an operator working on this object. For the time being we can imagine this as putting things (from the right-hand side) to the output (on the left-hand side).

Please keep in mind that C/C++ are case-sensitive, so "cout" is different from "Cout" or "COUT".

After the preprocessor commands you can start to define functions, global variables and constants and finally the function main. Function main is executed when you start the program, here it is of type void. Void is not a real data type, it simply means that we are not going to return a value from this function. The brackets () always follow the name of a function and may contain parameters passed to the function. The program itself must be surrounded by {} brackets. Of course you can have just one main function.

Every command is finished by a semicolon ";".

 

    comments

In C comments are included in /* (beginning of a comment) and */ (end of a comment). The included comment may be several lines long, and it may be used almost everywhere - except inside keywords:

i = 42 /* 42 is THE answer to all open questions...*/ + 67;       // this one is OK
dou/* a comment inside the keyword double...*/ble alpha;      // this one is NOT OK

The second type of comment used here is started by // and marks the rest of the line as a comment. This one is only available in C++.

It is a good practice to comment your program with the // type - this allows to exclude parts of your program using the /* */ comment for tests. Note that a */ always ends this type of comment - even when you opened more than one /* comment beginnings.

 

    variables

Now we want to define a variable. But before we can define one we have to decide of which kind it should be. Commonly used types are integer like numbers and floating point numbers (see chapter 3) as well as characters and strings. Also more complicated types like structures, composed of several (maybe different) types, and classes exist. In this chapter we will start examining the integer like numbers.

Names of variables may be of a size of 1024 (significant) characters in C++. In C you have as a minimum a guaranteed length of 31 characters. Both is fairly enough to give your variables meaningful names. Such a name must start with a letter (not a number) or an underline. The rest of the characters may be composed of alphanumeric characters (including numbers) and underlines. Not allowed are any special symbols like ", !, $, %, & ... (including the dots and colons). Commonly used variable names for the counting variable (of type integer) in loops are "i", "j", "n" and "k".

First remember that all numbers inside your computer are represented in binary format. Thus what defines the range (i.e. largest and smallest reachable numbers) of a certain type is primarily the length of the data type in byte. All data types have a length of an integer multiple of one byte, where one byte  consists of 8 bit's. Since you are usually dealing with positive and negative numbers the first bit of all signed numbers is  a sign-bit thus reducing the maximum value by a factor of 2.

Let´s see an example of a one byte number (also called a character) which is unsigned. It contains 8 Bit, so the highest number we get is

Bit 7 6 5 4 3 2 1 0
2n 128 64 32 16 8 4 2 1

1111 1111 = 128+64+32+16+8+4+2+1=255. To calculate this number more easily remember that the next highest number is 1 0000 0000 = 2^8 = 256. So 1111 1111 is simply 28-1=255, where 8 actually was the number of bit we used.

Copy the following program, compile and execute.

#include <iostream>

void main()
{
const int number_bits=16;

    ui=1;

    for (i=0;i<number_bits;i++) ui=ui*2;

    ui_max=ui-1;
    ui_plus_one=ui_max+1;

    cout << ui_max << "\t" << ui_plus_one << "\n";
}

Hopefully you have seen that it did not compile! What happened? The compiler usually gives us a detailed description if any problem occurs, including the number of the line where it first appeared. Here we simply have forgotten to define the variable i before we use it in the for-loop. Solve it inserting "int i" where you define the other variables.

As you might guess, in the for-loop we calculate the 16th power of 2, display that number minus one (which should give the maximum value) and also that maximum value plus 1. "unsigned" and "short" are 2 modifiers acting upon int. While the meaning of "unsigned" is clear, "short" simply means a 16 bit integer. If we use an integer without a modifier, the length is system dependent: on 16 bit operating systems it will usually be 16 bit long, on OS with 32 bit you usually get an 32 bit integer. If you want to be sure, use a modifier!

In the output command one sees two strings which control the output:

"\t"    produces a tab

"\n"    produces a newline

 

email me: Daniel Schürmann