1.7: Understanding the Code of the First C++ Program

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    1.7: Understanding the Code of the First C++ Program

    Following is the code of our first C++ program:

    Screenshot 2025-10-17 154040.png

    Let's discuss this simple program. Please note, some of the concepts you may not fully understand at this point, but you will be getting a better understanding as we move along with the course.

    Line 1: #include<iostream>

    is a compiler preprocessor directive that tells the compiler to link the "iostream library" to this program, which is needed to support console input and output. Console input means to receive input from the keyboard and console output means to display output on the monitor screen.

    C++ library contains pre-defined code for developing C++ programs. iostream is one of C++ library which is called a header file in C++ because it is usually included at the head of a program.

    Line 2: using namespace std;

    tells the compiler to use the standard namespace. Again, you may not understand what it means, it is ok, just place this line at the beginning of every program you are going to write. The reason we put this line here is we can use "cout" for displaying symbols or messages instead of using "sdt::cout"

    We will need those two lines of the code at the beginning of every C++ program.

    In C++, all the statements, instructions, and commands in a program will need to be placed inside a main() function. A main() function starts with a word "int" and followed by blank space and "main()". After "main()", there is pair of braces, all the code, including statements, instructions, and commends, of your program should be placed between those braces:

    int main()
    {
    cout<<"Good Morning!"<<endl;
    return 0;
    }

    Upon the completion of the program, the message "Good Morning!" will be displayed on the output window as following:

    Screenshot 2025-10-17 205758.png

    If we change the code between the braces in above program as following

    P2-1.png

    then upon the completion of the program, the display in the output window will be shown as following:

    preview

    This program consists of six statements. The first five statements begin with "cout<<" , where cout (read as see-out) stands for "console output" and << is called "stream insertion operator". We call cout<< as a "see-out statements". At the end of each of those statements, we place "<<endl;" which we refer it as "end of line character" It tells the computer "This is the end of this line, if you want to continue to display something, please start at the beginning of the next line"

    Anything that we would like to display on the output window, we will need to place them within a pair of quotation marks.

    For example, the line 7 displays a string of characters: +=================+ and the line 8 displays a string of blank spaces beginning with '|' and ending with a '|'. The line 9 displays a string beginning with '|' followed by a sequence of blank spaces and a phrase "Hollo World!" followed by another sequence of blank spaces, and ending with '|', and so on.

    The last statement

    return 0;

    is required at end of every program. This statement tells the computer that we have reached the end of the program.

    After we finish writing our program, the first thing we need to do is to "Build" the program. Building a program means

    • Doing the error checking
    • Translating the program to machine language so the computer can understand directly.
    • Linking all the resources required to run the program

    To build the program, click on "Build" and choose "Build Solution":P2-2.png

    After successfully build the program:

    P2-3.png

    we will need to send the program to computer CPU to have it executed (run).

    To execute the program, click on "Debug" and choose "Start without debugging"

    P2-4.png

    Upon the execution of the program, following output window opens and the output of the program shows on the screen:

    preview_mg1j

    We can press any key to close this window and go back to the source file.

    Congratulation! You are on your way to write more C++ programs.


    This page titled 1.7: Understanding the Code of the First C++ Program was last modified on Wed, 24 Dec 2025 20:37:22 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.