Loading...

C++ First Program

This page is where you write pine.cpp, one line at a time. The install page already built that file and the run showed pine. You are not installing a compiler again. If g++ or clang++ cannot be found, that gap belongs to the install page. Here the compiler is already on the machine, and the job is the text of the program. Open pine.cpp. If the install run left the file in place, read it in the same order you would type it into an empty file. Getting Started keeps this file and the lowercase word pine until a later page says the example changed.
The screen you are aiming at is the one the install run already produced: one line, pine. A matching file, rebuilt, shows that same line. A file that does not match shows something else, or it does not compile. The next page adds comments. This file has no comment, no second writing line, and no keyboard read. You stop before those.

Type the lines in the order below. The first line asks for the library that holds std::cout. The blank line under it is only a gap for your eyes. Then the function main opens, the writing line sits inside the braces, and return 0 closes the run. main is still a free function. There is still no class named Pine. Do not invent a second file. The name on disk stays pine.cpp, which is the name the install page compiled.
When the six lines match the block, save the file. The rebuild is the same command the install page used, from the folder that contains pine.cpp: g++ -std=c++17 pine.cpp -o pine and then ./pine. If that machine's version check was clang++ rather than g++, swap only the tool name: clang++ -std=c++17 pine.cpp -o pine. The screen is pine either way. The number 17 is a language level, not a screen character.

the first program, pine.cpp

cpp
#include <iostream>

int main()
{
    std::cout << "pine" << std::endl;
    return 0;
}

Read the file as a list of things you typed. #include is the request for the standard input and output library. Without it, the name std::cout is unknown, the build fails, and the screen from the install run disappears. You get an error instead of pine. Put the include back at the top and build again.
The empty line under the include prints nothing. It is not a blank line on the screen. Deleting it does not change the word pine. Leaving it in does not add a word. int main() is the start. The parentheses are empty because this program takes no arguments. The opening brace starts the body. The closing brace ends it. None of those tokens are printed. The indent in front of the two statements is for you. The compiler does not turn those spaces into screen characters.
If you type Int or Main with a capital letter, the build fails. The checked spelling is int main, all lowercase for those two words. The program name on disk can stay pine.cpp while the function stays main. Those are different names, and only the quoted word reaches the screen.

The writing line is the one that creates the screen you already saw. std::cout is the stream. The prefix std:: stays on the name because this file has no using directive. The operator << sends the next piece. The piece is "pine", four letters inside quotes. The quotes are typed. They are not shown. std::endl ends the line and is not a second word. The semicolon ends the statement and is not a character on the screen.
So the install run's line and this page's line are the same word. Rebuild and ./pine prints:
pine
Count the letters you typed inside the quotes. They are p, then i, then n, then e. Four letters, no space, no capital. The install screen used those same four letters. If the rebuild shows a space, a comma, or a fifth letter, the quotes do not match this page. Fix the quotes, rebuild, and run ./pine again until the line is pine and nothing else.
Change the quoted word to "Pine" and the screen becomes Pine. That is a different program. Put the lowercase word back. Remove the quotes and the compiler treats pine as a name that was never declared, so there is no screen. Remove std:: from cout and the bare name is unknown, so again there is no screen. Each of those edits throws away the install result. The checked line restores it.

return 0; is the last statement you type. It tells the system the program finished in the normal way. The 0 is a status. It is not printed under pine. Leave it under the writing line.
Move that return so it sits above the writing line, save, and rebuild. The program reaches return before it sends any characters, so the screen is empty. The install word is gone. That empty screen is how you see that order matters: statements run from top to bottom inside main. Move return back under the writing line, rebuild, and ./pine shows pine again. Do not leave the empty-screen experiment in the file.
A second std::cout line would add a second word. This page does not have one. A line that reads the keyboard would wait for you to type. This page does not have one. The whole visible result is still the single word from the install run.

The next page adds comments. A comment is a note the compiler skips, written with // or with a block between / and /. pine.cpp has neither. You do not type a note beside the writing line on this page. If a // is in the file, delete it before you judge the screen. The comments page is where that mark belongs. This page stops before it.
Last pass: the include is at the top, main is a function, the writing line quotes pine, endl ends the line, return 0 sits under that line and prints nothing, and ./pine shows pine. Getting Started still uses pine.cpp. The comments page is next, and this page does not start it.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top