Beginning Programming All-in-One For Dummies. Wallace Wang
Чтение книги онлайн.
Читать онлайн книгу Beginning Programming All-in-One For Dummies - Wallace Wang страница 20
A thousand lines of assembly language commands might do nothing more than multiply two numbers together. A thousand lines of high-level language commands might create a video game, a music player, or a stock market analysis program. By using high-level languages, programmers can spend more time being productive and less time worrying about the technical details of the computer.
Combining the best of both worlds with the C programming language
High-level languages isolate you from the technical details of programming, but by isolating you from these details, high-level languages also limit what you can do. So, as a compromise between assembly language (which can manipulate the processor) and high-level languages (which isolate you from the details of manipulating the processor), computer scientists created an intermediate language dubbed C.
The idea behind the C programming language is to give programmers the ability to manipulate the processor directly like assembly language, but also give them the chance to ignore these technical details, if they want, like a high-level language.
As a result, a C program doesn’t look as cryptic as assembly language, but it also isn’t as easy to read as a high-level language, as the following C program demonstrates:
#include <stdio.h>int main(void){ printf("Hello World!\n"); exit(0);}
Just by looking at this C program, you can probably figure out that it prints
Hello World!
on the screen. However, you may see a bunch of cryptic curly brackets, back slashes, and other odd symbols and characters that may make no sense whatsoever. Don’t worry. Just notice how confusing C programs can look while at the same time being somewhat understandable.
Because C lets you directly control the processor like assembly language does, but still write programs that look somewhat understandable and easy to read and write, many operating systems — such as Linux, macOS, and Microsoft Windows — are written all or partially in C.
Weighing the pros and cons of programming languages
The whole purpose of machine language, assembly language, high-level language, and the C language is to give you different ways to give instructions to the processor (computer). Ultimately, it doesn’t matter which type of programming language you use because it’s possible to write the exact same program in machine language, assembly language, a high-level language (like BASIC or Fortran), and C.
The only difference is that writing a program in machine language takes a really long time and is very difficult to write, fix, and understand. A similar program written in assembly language is smaller and simpler than an equivalent machine language program.
Writing the same program in the C language makes the program even smaller and much easier to write and understand. If you use a high-level language, the program would most likely require writing less code and be easiest to understand out of them all.
So, given these advantages of C or high-level languages, why would anyone ever use machine language or assembly language? The answer is simple: speed and efficiency.
If you want to write the fastest program possible that uses the least amount of memory, use machine language because machine language is the native language of all computers. Unfortunately, machine language is so hard to understand, write, and modify that writing anything but small programs in machine language is nearly impossible.
Instead of using machine language, most programmers use assembly language when they need speed and efficiency. Assembly language creates small and fast programs, but they’ll never be as small or fast as machine language programs. That’s because processors understand only machine language, so when you write an assembly language program, you have to translate that assembly language program into machine language.
Translating assembly language into machine language by hand would be slow and error-prone, so computer scientists have created special programs that can do this automatically. These programs are assemblers.
An assembler takes an assembly language program and converts it into machine language, but this conversion process isn’t perfect. That’s why assembly language tends to create bigger and slower programs than equivalent handcrafted machine language programs. However, assembly language programs are much easier to write and modify later than machine language programs are, so assembly language is used much more often than machine language.
High-level languages are much easier to write and understand than machine language or assembly language. The problem is that processors don’t understand high-level languages either, so you have to translate a high-level language program into equivalent machine language commands.
Doing this by hand is nearly impossible, so computer scientists have created special programs — compilers — to do this for them. A compiler takes a program written in a high-level language and translates it into equivalent commands written in machine language.
This translation process isn’t perfect, which is why programs written in high-level languages tend to be much bigger and slower than equivalent programs written in machine language or assembly language. So, when programmers want to create large, complicated programs that still run fast and take up as little space as possible, they tend to rely on the C programming language. That’s why so many programs are written in C — C creates programs nearly as small and fast as assembly language programs, while also being nearly as easy to write and understand as high-level languages. (Note the emphasis on the word nearly.)
As a general rule, if you want to make programming easy where speed and efficiency aren’t that crucial, use a high-level programming language. If you want to make a small and fast program and don’t care how inconvenient it may be to write it, use machine language or assembly language.
What if you want to write a big and fast program (like an operating system or word processor) and also make it convenient for you to write? You’d use the C programming language.
Although C is a fast and powerful language, it’s not the safest. C lets you access all parts of a computer, which means if you’re not careful, your programs can corrupt the computer’s memory (known as memory leaks) and cause all types of unintended havoc. For that reason, computer scientists have used C as a reference to create “safer” versions of the language. Many of today’s popular programming languages (such as C#, Java, Objective-C, Python, and Swift) have been directly or indirectly inspired by C. Because C uses curly brackets to define the beginning and end of code blocks, C-based languages are often called curly-bracket languages.
Ultimately, no one cares what language you use as long as your program works. A program that works is far better than a small, fast, and efficient program that doesn’t work. Think of a programming language as a tool. A good programmer can use any tool well, but a bad programmer can screw up using the best tool in the world. The programmer’s skill always determines the quality of any program; the type of programming language used is always secondary. So the goal isn’t to become a “C programmer” or a “Fortran programmer.” The goal is to become a good programmer, regardless of the language