Файл: Interfacing with C plus plus-programing communication with microcontrolers (K. Bentley, 2006).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 3542

Скачиваний: 1

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

1

Getting Started

Inside this Chapter

ξ

ξ

ξ

ξ

ξ

Developing programs – what is involved?

Writing and running your first C++ program.

Program syntax.

Functions.

Fundamental data types.

1.1 Introduction

The aim of this chapter is to get you started in writing C++ programs. We will develop a number of simple C++ programs and learn the syntax and typography associated with writing a program. One of the basic building blocks of any C++ program is the so-called function. This chapter will explain the basic concepts behind C++ functions and their use. The C++ language has built-in fundamental data types that can be used to develop complex user-written data types. Some of the fundamental data types will be explained in this chapter.

Towards the end of the chapter we will step through the complete program development process; starting from planning a small program down to using the elements of program development software needed to generate a program that can be run on your computer. We will commence with the use of non-object-oriented programming methods because these programs are simpler to understand at this early stage. Object-oriented programming concepts will be explained in Chapter 4 and then used extensively through the remainder of the text.

1.2 Program Development Software

The process of program development includes a number of subtasks. To be able to develop a program you must have an editor, a compiler and a linker. In modern program development platforms, these subtasks are seamlessly integrated and the entire process is very transparent. Such platforms are known as Integrated Development Environments (IDEs). Most modern C++ packages (the software that you will use to develop C++ programs) provide some sort of an IDE. Some of the commercially available packages include Turbo C++, Borland C++, C++ Builder and Visual C++. There are also packages referred to as command line versions. The command line versions require you to type a command (say at the DOS prompt) to invoke the editor. Then you must use another command line to invoke the compiler and so forth.

Along with the editor, compiler and linker, these packages also provide extensive library support. Sometimes these libraries are referred to as run-time libraries (RTLs). They contain a wide variety of routines or functions we can use within our programs. Regardless of what package we use, it is worthwhile to understand what happens during each subtask. The following sections will describe editing, preprocessing, compiling, and linking.

1.2.1 Editing

The first step in preparing your program is to use some kind of editor to type your program. Not every editor is suitable for this purpose. The edit program of DOS and the Notepad editor of Windows are two suitable editors. Integrated Development Environments (IDE) that are part of C++ packages provide built-in editors known as text editors. At the end of the editing session you must store the

1 GETTING STARTED 3

contents of the editor into a file. The two editors mentioned above will only store what you type. They will not add extra characters to your file (unlike some editors). What we normally type includes digits, letters, punctuation marks, the space, tab, carriage return and line-feed characters. The line-feed character is used by the editor to position the cursor on a new line. The carriage return character is used by the editor to position the cursor at the start of the next line. A program file must not contain characters apart from those listed above. The file that contains all programming instructions, is known as the source file. The source file is said to contain the source code, which is nothing more than the programming instructions you typed.

1.2.2 Compiling

The second step is to compile the source file. For this purpose, a special program known as a compiler is used. As part of the compiler, a program named the preprocessor is invoked. This takes place before the actual compilation of your source code. The preprocessor attends to your source code statements that start with the '#' sign. (See the program listings ahead for the lines starting with a ‘#’ sign). These statements are referred to as compiler directives. The preprocessor takes action as directed by these statements and will modify your original source file. At the end of preprocessing, all lines starting with the '#' sign will have been processed and eliminated. This process is shown in Figure 1-1. The preprocessor and the compiler are gradually becoming merged - most modern compilers have the preprocessor as a built-in part of the compiler itself.

#include <iostream.h>

.

.

void main()

.

{

cout << "...

void main()

}

{

PREPROCESSOR

cout << " ...

}

Figure 1-1 Preprocessor attends to all lines starting with '#' symbol.

The compiler in-turn processes the file produced by the preprocessor and produces a file known as an object file. The object file contains what is known as object code, which the Central Processing Unit (CPU) of your computer understands, also known as machine code. However, the PC cannot execute the object code since it


41 GETTING STARTED

still has a few parts missing. At this stage your program is in a similar state to an unfinished highway with some stretches complete and others not. As a result, the compiled program cannot yet be executed (i.e. run on your computer).

At this incomplete stage, the object code is said to contain undefined references. The undefined references refer to pieces of object code that need to be retrieved from elsewhere to complete the entire program. Just like the highway, the object file does not have a continuous execution path. The compiling process is shown in Figure 1-2.

.

01000101001001010010100

01010101011111001001001

.

01010010000011110101001

.

01111001001100110010010

void main()

01111100011100100100001

COMPILING

11001000011101010100010

{

00110100100010001001000

cout << " ....

10010100101000100100100

}

???undefined

references

???????????????????????

11100000101001001010100

00100101001010010100101

0010001001001001001010?

??undefined

references

??????????????????????0

Source Code

Object Code

Figure 1-2 The compiler converts the source code to object code.

The syntax used as part of the program statements is extremely important. As mentioned earlier, syntax refers to the use of punctuation marks within the source file. Most of the time these punctuation marks act as delimiters. A delimiter identifies the end of variables, keywords, numbers, statements etc. The space, the comma, the semicolon, the colon, the brace etc., act as delimiters for different contexts of usage. Compilers have limited in-built intelligence. If you miss a semicolon the compiler will detect it and report an error, but it cannot correct the error for you.

As mentioned earlier, the object code is incomplete with many unresolved areas and it cannot be executed. For example, the object code may contain calls to various routines. The object file includes function calls to be made. The actual instructions to be executed during the call are not yet in place. These instructions may be available elsewhere in the object file, or they may need to come from a library file or another object file. Note that finding the missing bits is not part of the compiler’s duties – the compiler can be viewed in basic terms as a translator that checks grammatical content!


1 GETTING STARTED 5

1.2.3 Linking

The program that bridges all the gaps and completes assembly of the program is known as the linker. It will search all the object files and the libraries to find the missing sets of instructions. Sometimes the linker must be told to search certain libraries and object files. These are either third party libraries you may have purchased or the libraries and object files you developed. The linker automatically searches the libraries and object files that come with the C++ software, one being the so-called Run-Time Library (RTL). The linker will insert the missing sets of instructions into appropriate places to form a file that has a ‘gaps free’ execution path. This process is known as linking. At the end of the linking process, we have a file the PC can execute, known as an executable file.

The program must be loaded into the computer's memory before execution can begin. This action is carried out by a piece of executable code known as a loader. Most linkers append a loader to the start of the executable file. Therefore, when we try to run the program, first the loader will run, loading the program into memory and then actual program execution will begin. Figure 1-3 shows the linking process.

01000101001001010010100

01000101001001010010100

01010101011111001001001

01010101011111001001001

01010100100100100100100

01010100100100100100100

01010010000011110101001

01010010000011110101001

01111001001100110010010

01111001001100110010010

01111100011100100100001

LINKING

01111100011100100100001

11001000011101010100010

11001000011101010100010

00110100100010001001000

00110100100010001001000

10010100101000100100100

10010100101000100100100

???undefined references

01010101000100101010100

???????????????????????

01001010010010010100101

1110000010100100101010

01110000010100100101010

00010010100101001010010

00010010100101001010010

10010001001001001001010

10010001001001001001010

???undefined references

10111001001001011010001

??????????????????????0

01001001100001010110010

Object code

Executable Code

1110001

1100101

1010001

1010011

0100001

1011010

1000101

1010111

0011011

1000010

0011010

1011010

0010001

0010011

1111010

1000100

1110100

1100110

1000101

0111000

Library Routines

1010110

1010001

1011110

0010111

0000101

Library Routines

Figure 1-3 Linking forms a gaps-free executable code.


61 GETTING STARTED

1.3 A C++ Program

A computer sees a program as a set of instructions to be executed. The programmer arranges these instructions in a certain order depending on the tasks the computer is expected to perform. To give you a simple example; if you want to write a program to add two numbers, the numbers must be entered first and then the addition must be carried out. Therefore, the instructions to read the numbers must come before the instructions to add the numbers.

Each programming language has its own unique syntax. Syntax is the typography and the use of punctuation marks. Here, we will learn the syntax that applies to the C++ programming language.

As mentioned earlier, the basic building block of a C++ program can be viewed as the function – a procedure that produces an end result. Therefore, every C++ program that contains a set of executable instructions must have a function. One of these functions is special, and is named main. To uniquely identify functions separately from other entities in our text, we use a pair of parentheses () after the function name. Simple programs can be written just with a main() function. When programs become more elaborate and complex, other functions may have to be written in addition to the main() function.

The aim of our first C++ program is to print a text message on the screen of your computer. The lines of this program are given in Listing 1-1.

Listing 1-1 Program to print a text message on the screen.

/* This program prints a text message on your screen. The program consists of just one function named main.*/

#include <iostream.h>

// The main function. void main()

{

cout << “Getting Started “ << endl;

}

If you run this program, you will see the message:

Getting Started

printed on your screen. The following sections explain the composition of this program.

1 GETTING STARTED 7

1.3.1 Comments in Programs

Comments are descriptions included in a program that are used so programmers can document their work. They often describe a program or specific parts of a program and do not form any part of the actual program’s instructions that will run on the computer. If you include comments, you must indicate to the compiler that they are not to be considered as actual code when the compiler prepares the final program prior to execution. There are two different ways to include comments:

(i)To include single line or multi-line comments you can use ‘/*’ at the start of the comment and ‘*/’ at the end of the comment.

(ii)If the comment is a single line comment you may use ‘//’ at the start of the comment.

In Listing 1-1, we have a multi-line comment and a single line comment. The multi-line comment is:

/* This program prints a text message on your screen The program consists of just one function named main().*/

The single line comment is:

// The main function.

The text contained within ‘/*’ and ‘*/’ will be ignored by the compiler. Likewise for the text after ‘//’ on that line.

1.3.2 Header Files

The first line after the multi-line comment of Listing 1-1 is an include statement:

#include <iostream.h>

It instructs the preprocessor to replace that statement with the entire contents of the file iostream.h. In our program this takes place just before the start of the main() function. The files with the file extension ‘.h’ are known as header files or as include files. A header file can already exist within the C++ development software, or it may be a file created by the programmer. If it is a file provided with the C++ development software, then it resides in a special sub-directory known as the Include Directory, as is the case for the iostream.h file. Programs can have more than one include statement, resulting in the inclusion of a number of header files.

The header files are text files that contain C++ programming statements, most of which do not form executable program statements. Not all statements in your program are executable. However, the statements in header files play a major role in the preparation of your program. The majority of the statements in a header file assist the compiler to carry out a thorough check of the program statements you write in your program. Once the header files are written and tested, we do not