Interpreted vs. Compiled Program Execution - Practices
This article will give you a good understanding of the basic concepts and practices of the C++ language, so that you will have the foundation to eventually learn these ideas in detail as you continue working with the language. It is excerpted from Ivor Horton's Beginning ANSI C++ The Complete Language (Apress, 2004; ISBN 1590592271).
Whatever the programming language, the programs that you write are made up of separate instructions or source statements that describe the actions that you want the computer to carry out. These are referred to collectively as source code and are stored on disk in a source file.A single C++ program of any size will consist of several source files.
Programming languages are designed to make it relatively easy for you to describe the actions you want a computer to carry out compared with the form of program that a computer can actually execute. Your computer can only execute programs that consist of machine instructions (also called machine code), so it can’t execute your program’s source code directly. There are basically two ways in which a program written in one of the languages I mentioned previously can get executed and, for the most part, a particular language will use one or the other. Programs written in BASIC, for example, are often interpreted—that is, another program called an interpreter inspects the BASIC source code, figures out what it’s supposed to do, and then causes that to be done. This is illustrated on the left side of Figure 1-1.
Figure 1-1.Interpreted and compiled program execution
C++, on the other hand, is usually a compiled language. Before you can execute your C++ program, it must be converted to machine language by another program called a compiler. The compiler inspects and analyzes the C++ program and generates the machine instructions that will produce the actions specified by the source code. Of course, in reality neither interpreting nor compiling is quite as simple as I’ve described them here, but in principle that’s how they work.
With an interpreted language, execution is “indirect,” by which I mean the intent of the source code needs to be determined each time a program is executed. For this reason, execution is much slower—sometimes of the order of 100 times slower—than the equivalent program in a compiled language. The upside is that you don’t have to wait and compile a program before you run it. With an interpreted language, as soon as you’ve entered the code, you can execute the program immediately. A given language is usually either compiled or interpreted, and it’s typically the design and intended use of the language that determines which. Having described BASIC as an interpreted language, I should point out that it isn’t exclusively so; there are compilers for the BASIC language.
The question of which language is the “best” language sometimes comes up. The short answer is that there is no such thing as the “best” language—it depends on the context. Writing a program in BASIC is typically very rapid compared with most other languages, for instance, so if speed of development is important to you and obtaining the maximum execution performance isn’t, then BASIC is an excellent choice. On the other hand, if your program requires the execution performance that C++ provides, or you need the capabilities in your application that are available in C++ but not in BASIC, then C++ is obviously what you would use. If your application really must execute on a wide range of different computers and you aren’t concerned about achieving the ultimate in execution performance, then Java may be the best option.
Of course, the length and steepness of the learning curve will vary among different languages. In terms of the amount of time needed to learn a language, C++ is probably at the higher end of the scale, but this shouldn’t put you off. It doesn’t mean that C++ is necessarily more difficult. It does mean that there’s a great deal more to C++ than most other languages, so it takes a little longer to learn.
As a final thought on which language you should learn, any professional programmer worth his or her salt needs to be comfortable in several programming languages. If you’re a beginner, this may sound a little daunting, but once you’ve grappled with and conquered your first two programming languages, you’ll find it gets a lot easier to pick up others. Your first programming language is almost always the hardest.
If you had to create everything from scratch every time you wrote a program, it would be tedious indeed. The same kind of functionality is often required in many programs— for example, reading data from the keyboard, or displaying information on the screen, or sorting data records into a particular sequence. To address this, programming languages usually come supplied with considerable quantities of prewritten code that provides standard facilities such as these, so you don’t have to write the code for them yourself every time.
Standard code that’s available for you to use in any of your programs is kept in a library. The library that comes with a particular programming language is as important as the language itself, as the quality and scope of the library can have a significant effect on how long it takes you to complete a given programming task.
Why Is C++ Such a Great Language?
C++ enjoys extraordinary popularity across virtually all computing environments: personal computers, Unix workstations, and mainframe computers. This is all the more remarkable when you consider the degree to which history weighs against a new programming language, no matter how good it is. The inertia implicit in the number of programs written in previous languages inevitably slows the acceptance of a new language. Added to this, there’s always a tendency among most professional programmers to stick with what they know and are expert and productive in, rather than jump in at the deep end with something new and unfamiliar, in which it will take time to develop fluency. Of course, the fact that C++ was built on C (which itself was the language of choice in many environments before the advent of C++) helped tremendously, but there’s a great deal more to it than that. C++ provides you with a unique combination of advantages:
C++ is effective across an incredible range of applications. You can apply C++ to just about anything, from word processing to scientific applications, and from operating system components to computer games.
C++ can be used for programming down at the hardware level—for implementing device drivers, for instance.
C++ combines the facility for efficient procedural programming that it inherits from C with a powerful object-oriented programming capability.
C++ provides extensive facilities in its standard library.
There are many commercial libraries supporting a wide range of operating system environments and specialized applications for C++.
You’ll also find that just about any computer can be programmed in C++, so the language is pervasive across almost all computer platforms. This means that it is possible to transfer a program written in C++ from one machine to another with relatively limited effort. Of course, if this is truly going to be a straightforward process, you need to have in mind when you write the program that you intend to run it on a different machine.
The ANSI/ISO Standard for C++
The international standard for C++ is defined by the document ISO/IEC 14882, which is published by the American National Standards Institute (ANSI). You can get a copy of this standard if you wish, but remember, the standard is intended for use by compiler writers, not by students of the language. If that hasn’t discouraged you, you can download a copy for a relatively reasonable fee from http://webstore.ansi.org/ansidocstore/default.asp.
Standardization of a language is fundamental when you want to transfer a program written for one type of computer to another. The establishment of a standard makes possible a consistent implementation of the language across a variety of machines. A full set of standard facilities across all conforming programming systems means that you’ll always know exactly what you’re going to get. The ANSI standard for C++ defines not only the language, but also the standard library. Using the ANSI standard for C++ makes the migration of applications between different machines easier and eases the problems of maintaining applications that run in more than one environment.
Another benefit of the ANSI standard for C++ is that it standardizes what you need to learn in order to program in C++ in any environment. The existence of the standard itself forces conformance over time, because it provides the only definitive reference for what a C++ compiler and library should provide. It removes the license to be “flexible” that compiler writers have had in the absence of an agreed standard, so when you buy a C++ compiler that conforms to the ANSI standard, you know what language and standard library capabilities you’re going to get.
This article is excerpted from Beginning ANSI C++ The Complete Language by Ivor Horton (Apress, 2004; ISBN 1590592271). Check it out at your favorite bookstore today. Buy this book now.