Search

0.2 — Introduction to programming languages

Modern computers are incredibly fast, and getting faster all the time. Yet with this speed comes some significant constraints. Computers only natively understand a very limited set of instructions, and must be told exactly what to do. A program (also commonly called an application or software) is a set of instructions that tells the computer what to do. The physical computer machinery that executes the instructions is the hardware.

Machine Language

A computer’s CPU is incapable of speaking C++. The very limited set of instructions that a CPU natively understands is called machine code (or machine language or an instruction set). How these instructions are organized is beyond the scope of this introduction, but it is interesting to note two things. First, each instruction is composed of a number of binary digits, each of which can only be a 0 or a 1. These binary numbers are often called bits (short for binary digit). For example, the MIPS architecture instruction set always has instructions that are 32 bits long. Other architectures (such as the x86, which you are likely using) have instructions that can be a variable length.

Here is an example x86 machine language instruction: 10110000 01100001

Second, each set of binary digits is translated by the CPU into an instruction that tells it to do a very specific job, such as compare these two numbers, or put this number in that memory location. Different types of CPUs will typically have different instruction sets, so instructions that would run on a Pentium 4 would not run on a Macintosh PowerPC based computer. Back when computers were first invented, programmers had to write programs directly in machine language, which was a very difficult and time consuming thing to do.

Assembly Language

Because machine language is so hard to program with, assembly language was invented. In an assembly language, each instruction is identified by a short name (rather than a set of bits), and variables can be identified by names rather than numbers. This makes them much easier to read and write. However, the CPU can not understand assembly language directly. Instead, it must be translated into machine language by using an assembler. Assembly languages tend to be very fast, and assembly is still used today when speed is critical. However, the reason assembly language is so fast is because assembly language is tailored to a particular CPU. Assembly programs written for one CPU will not run on another CPU. Furthermore, assembly languages still require a lot of instructions to do even simple tasks, and are not very human readable.

Here is the same instruction as above in assembly language: mov al, 061h

High-level Languages

To address these concerns, high-level programming languages were developed. C, C++, Pascal, Java, Javascript, and Perl, are all high level languages. High level languages allow the programmer to write programs without having to be as concerned about what kind of computer the program is being run on. Programs written in high level languages must be translated into a form that the CPU can understand before they can be executed. There are two primary ways this is done: compiling and interpreting.

A compiler is a program that reads code and produces a stand-alone executable program that the CPU can understand directly. Once your code has been turned into an executable, you do not need the compiler to run the program. Although it may intuitively seem like high-level languages would be significantly less efficient than assembly languages, modern compilers do an excellent job of converting high-level languages into fast executables. Sometimes, they even do a better job than human coders can do in assembly language!

Here is a simplified representation of the compiling process:
Example of compiling

An interpreter is a program that directly executes your code without compiling it into machine code first. Interpreters tend to be more flexible, but are less efficient when running programs because the interpreting process needs to be done every time the program is run. This means the interpreter is needed every time the program is run.

Here is a simplified representation of the interpretation process:
Example of interpreting

Any language can be compiled or interpreted, however, traditionally languages like C, C++, and Pascal are typically compiled, whereas “scripting” languages like Perl and Javascript are interpreted. Some languages, like Java, use a mix of the two.

High level languages have several desirable properties.

First, high level languages are much easier to read and write.

Here is the same instruction as above in C/C++: a = 97;

Second, they require less instructions to perform the same task as lower level languages. In C++ you can do something like a = b * 2 + 5; in one line. In assembly language, this would take 5 or 6 different instructions.

Third, you don’t have to concern yourself with details such as loading variables into CPU registers. The compiler or interpreter takes care of all those details for you.

And fourth, they are portable to different architectures, with one major exception, which we will discuss in a moment.

Example of portability

The exception to portability is that many platforms, such as Microsoft Windows, contain platform-specific functions that you can use in your code. These can make it much easier to write a program for a specific platform, but at the expense of portability. In these tutorials, we will explicitly point out whenever we show you anything that is platform specific.

0.3 -- Introduction to C/C++
Index
0.1 -- Introduction to these tutorials

167 comments to 0.2 — Introduction to programming languages

  • Suraj

    Here is an example x86 machine language instruction: 10110000 01100001 and Here is the same instruction as above in assembly language: mov al, 061h
    Can you please explain me how they are same.

    • Alex

      An assembler “compiles” the assembly instruction “mov, al, 061h” into the machine language instruction 10110000 01100001, which can be executed directly by the CPU.

  • this tutorial is the best…. like always:)
    by the way I want to learn c++ because my wish it became a game development.
    Sorry if my writing less can be understood:)

  • Dakota Cookenmaster

    This tutorial site is almost an answer to prayers. I am a Web Developer by trade (knowing HTML5 and CSS3) but I never had the chance to get into C++ or C# or JavaScript. I just got a job offer to design a program and I freaked because I had no idea how to program a program. This is a glimmer of light. I hope that it will help me with what I need to do.

  • Marko

    I do not see adds but I would like to, because I wish you earn for your effort! Why is that?

  • Saulo

    I loved this. Thanks!

  • Apollo Justice

    So simple!
    Even a child could learn from this. [One actually is] Me. I’m a sixth-grade student.

  • sho_k

    hey alex,
             pizza or mac&cheese ?

    • Alex

      A true programmer would never choose mac & cheese over pizza.

      But now I’m wondering what mac and cheese pizza would taste like.

  • RandomGuy

    i am still on elememtary, and i am already here, lol

  • thank you
    i do not know English

  • Joe Lai Lim

    i am currently preparing for my university diploma in information technology course, i have completely no background or knowledge regarding programming languages and its roots, development and this websites are giving such an easy and detailed information!!! i want a textbooks which can be in this kind of format hahahah, and most importantly, it drives my thirst for knowledge in learncpp.com, thank you!!

  • Raza

    Can anybody explain me what this statement means in the second line of first paragraph "Computers only natively understand". I want to know what exactly author when he/she is using the word ‘natively’. I try google it, but did not found any solid answer.

  • Vidya Moger

    Alex,

    Compiler reads entire source code at once and loads the object code (binary data) in memory for execution.
    If my understandng is correct, then the interpreter will read each line of the code and loads the translated machine code into memory one by one.

    I am not completely understanding how compilers and interpreters loads the machine code into memory for execution. I hope you understand my queries. I am beginner.

    Thanks,
    Vidya

    • Alex

      Both compilers and interpreters work through the source code one line at a time. The difference is that compilers convert all of the code into an executable ahead of time. Interpreters do it “just in time”.

  • Vidya Moger

    Wow…simple and easy to understand. First few paragraphs hooked me up to this tutorial. Hope I become a good and effiecient C++ programmer 🙂 🙂
    Thanks Alex for your efforts.

  • Cirius

    Thank you for your help! I appreciate it!

  • Thanks .. nice tutorial for beginners.

  • Paul

    Thanks Alex for your hard work.
    For an old feller like me this is ideal, I have had jobs on the periphery of all this for years but never got around to getting my hands dirty.

  • hello Alex,great site and keep it up .i have a question that i want to become an expert in c++ and want to develop a game within 4 month ,can i do this . you tell me that what shall i do for this ??????please give me suggestion.

  • Alex please make the ads open in a new page…lol..Just messin

  • Hafiz Furqan

    sir,

    I HOPE you are fine it is very informative but i want to ask you i am little bit knowledge of computer my education is Master of education. is it possible to learn the c plus plus ?

  • Christopher Schrock

    This is awesome! I am new to all this and preparing for a college course in the next year or so. I’m hoping to make it through all of this to fully understand! 🙂

    I really appreciate all the hard WORK!

  • Nathan Choi

    Hey Alex,

    As I’m new to programming C++, I found this website quite helpful. I really appreciate all your work and effort. Thank you!

  • Ran

    What I’ve learned from this page.
        

            * machine language: A code that can be understand by CPU

        1. Instructions are composed of a number of binary digits.
        2. Each set of digits is translated by CPU into a specific instruction to do some task.
        3. Example: 10110000 01100001

        * Assemble language:

        1. The instructions given to CPU is a set of name (rather than a set of bits)
        2. Easy to understand by construct with machine language, but it is still hard for human to understand
        3. Fast but only to a specific CPU.
        4. Example: mov al, 061h

        * High level language

        1. Easy for human to write code.
        2. Must be translated into machine language before execute.
        3. Two ways to translate high level language into machine code: compiling or interpreting.

        * The difference between compiler and interpreter

        1. Compiler translates high-level language into executable programs that CPU can directly understand.
        2. Interpreter do not translate high-level language. It directly execute your program.

  • atul

    I read more to c++

  • toby

    Amazing tutorial….Would recommend this site to everyone

  • Amaan

    Thanks a million , in fact splendid tutorials. Great job

    please familiarise me about how to retain screen.

    i used
           std::cin.get;
    before return 0;
    it doesn’t work, but when i use it twice ,like
       std::cin.get;
       std::cin.get;
        return 0;

    then screen retains and waits for enter key to press.

    i am usind Bloodshed dev-C++ compiler

  • Cosry

    C++ works for Roblox right? I heard it does along side LUA

    • Alex

      I’m not familiar with Roblox, but it looks like the scripting is done in LUA. However, I do see some people doing C++ stuff, so maybe there’s a way to do it.

  • adoga

    this site is the juice…very well Exemplified..better than most textbooks, and it’s free..

  • heidi

    amazing how simple and practical can be,thx so much guys for this lesson.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter