|
|
Chapter 1first steps
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 Well, this was very easy! Now let's have a look at the program.
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 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.
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
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> 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 |