.. _program-what-is-a-programming-language: What is a Programming Language? ------------------------------- .. index:: single: high-level language The programming language you will be learning is C++. It is a **high-level language**. .. index:: single: low-level language As you might infer from the name “high-level language,” there are also **low-level languages**, sometimes referred to as machine language or assembly language. Loosely-speaking, computers can only execute programs written in low-level languages. Thus, programs written in a high-level language have to be translated before they can run. This translation takes some time, which is a small disadvantage of high-level languages. .. index:: single: portable But the advantages are enormous. First, it is *much* easier to program in a high-level language; by “easier” I mean that the program takes less time to write, it’s shorter and easier to read, and it’s more likely to be correct. Secondly, high-level languages are **portable**, meaning that they can run on different kinds of computers with few or no modifications. Low-level programs can only run on one kind of computer, and have to be rewritten to run on another. Due to these advantages, almost all programs are written in high-level languages. Low-level languages are only used for a few special applications. .. index:: single: interpreter single: interpret There are two ways to translate a program; interpreting or compiling. An **interpreter** is a program that reads a high-level program and does what it says. In effect, it translates the program line-by-line, alternately reading lines and carrying out commands. .. digraph:: interpreters :name: fig_interpreter_process :caption: Simplified interpreter process :alt: diagram showing source code interpreted into an executable program :align: center fontname = "Bitstream Vera Sans" node [ fontname = "Bitstream Vera Sans" fontsize = 11 style=filled fillcolor=lightblue ] edge [constraint=false] src [label="source \ncode", shape="box"]; exe [label="executable\nprogram", shape="box"]; src -> interpreter -> exe; node [ shape="plain" style=none ] edge [style=invis, constraint=true] t1 [label="The interpreter\lreads the\lsource code ...\l"]; t2 [label="... and the result\lappears on\lthe screen ...\l"]; src -> t1; interpreter -> t1; interpreter -> t2; exe -> t2; {rank=sink; t1 t2}; .. index:: single: compiler single: compile single: source code single: object code single: executable A **compiler** is a program that reads a high-level program and translates it all at once, before executing any of the commands. Often you compile the program as a separate step, and then execute the compiled code later. In this case, the high-level program is called the **source code**, and the translated program is called the **object code** or the **executable**. As an example, suppose you write a program in C++. You might use a text editor to write the program (a text editor is a simple word processor). When the program is finished, you might save it in a file named ``program.cpp``, where "program" is an arbitrary name you make up, and the suffix ``.cpp`` is a convention that indicates that the file contains C++ source code. The ``cpp`` suffix is not universal. Others you might encounter include ``cc``, ``cxx``, or others. In this text, I will use ``cpp``. Then, depending on what your programming environment is like, you might leave the text editor and run the compiler. The compiler would read your source code, translate it, and create a new file named program.o to contain the object code, or program.exe to contain the executable. .. digraph:: compilers :name: fig_compiler_process :caption: Simplified compilation process :alt: diagram showing source code compiled into object code and an executable program :align: center fontname = "Bitstream Vera Sans" node [ fontname = "Bitstream Vera Sans" fontsize = 11 style=filled fillcolor=lightblue ] edge [constraint=false] rankdir=LR; src [label="source\ncode", shape="box"]; obj [label="object\ncode", shape="box"]; exe [label="executable\nprogram", shape="box"]; src -> compiler -> obj -> linker -> exe; node [ shape="plain" style=none ] edge [style=invis, constraint=true] t1 [label="The compiler\lreads the\lsource code ...\l"]; t2 [label="... and generates\lobject code.\l"]; t3 [label="the linker combines\lall the object files\linto an executable."]; t4 [label="which can be run\land the result\lappears on\lthe screen ...\l"]; src -> t1; compiler -> t1; compiler -> t2; obj -> t2; obj -> t3; linker -> t3; linker -> t4; exe -> t4; {rank=sink; t1 t2 t3 t4}; The next step is to run the program, which requires some kind of executor. The role of the executor is to load the program (copy it from disk into memory) and make the computer start executing the program. Although this process may seem complicated, the good news is that in most programming environments (sometimes called development environments), these steps are automated for you. Usually you will only have to write a program and type a single command to compile and run it. On the other hand, it is useful to know what the steps are that are happening in the background, so that if something goes wrong you can figure out what it is. .. tb-group:: :name: tab_check .. tb-tab:: Q1 .. tb-blank:: :name: program_lang_1 A(n) {{blank:blank1}} translates a high-level program line by line, executing commands as they come. A(n) {{blank:blank2}} translates a high-level program all at once, before executing any of the commands! .. tb-answer:: blank1 :match: interpreter :feedback: Correct! :incorrect: Try again! .. tb-answer:: blank2 :match: compiler :incorrect: Try again! .. tb-tab:: Q2 .. tb-choice:: :name: program_lang_2 **Multiple Response** Which is true about a high-level programming language? - [x] Almost all programs are written in high-level languages. High-level languages are efficient and easy to understand, an obvious choice for writing a program! - [x] Programs written in a high-level language must be translated before they can be run. All programs in high-level language must be translated to a low-level language before the computer can execute them! - [ ] It's easier to program in a low-level language than a high-level language. Actually, its much the other way around! - [ ] Computers can only execute programs written in high-level languages. Computers actually can't understand high-level languages as they are written. - [ ] High-level programs can only run on one kind of computer (you'd have to rewrite the program if you wanted to use a different machine). High-level programs are portable, meaning they can run on different kinds of computers with little to no modification. .. tb-tab:: Q3 .. tb-choice:: :name: program_lang_3 What is the role of an executor? - [ ] To translate the program line by line. This is the role of an interpreter! - [x] To copy the program from disk to memory and make the computer run the program. The role of an executor is to carry out, or execute, the program! - [ ] To translate the program all at once. This is the role of a compiler! - [ ] To give an error message if something is preventing the code from being translated. This happens automatically when we try to compile/interpret our program. .. tb-tab:: Q4 .. tb-match:: :name: program_lang_4 Match each term to an example of it! source code our program written in C++ object code translated version of our program that the computer can understand and execute ----- .. admonition:: More to Explore - From cppreference.com - :lang:`Phases of translation ` - :wiki:`Compiler` and :wiki:`linker ` on Wikipedia