C++ Variables
This page gives the two words from the output page a name. The output page ended with pine.cpp writing pine on one line and cone on the next, and ./pine showed pine, then cone. That two-line result is the step this page builds on. You keep the comment // the word stays pine above the writes, and you keep both std::endl calls. Getting Started still keeps pine.cpp and the lowercase word pine, so the cast and the file stay the same.
The compiler is already on the machine from the install page, and the build command does not change. What changes is the middle of the file. Each word moves into a box with a name, and the writing lines call the name instead of typing the word again inside quotes.
A variable is a name that holds a value. Think of it as a labeled box. The line std::string first = "pine"; does three things. It creates a box named first. It says the box holds text, and C++ calls that text type std::string. It puts the value pine into the box. The semicolon ends the statement, as it did on every writing line before. std::string is a class from the standard library, and you did not write it, so this page does not add a class of your own. The type's methods come later, on the types and strings pages. Read the declaration as a sentence: the type comes first, then the name, then an equals sign, then the value. The equals sign here does not ask a question. It means the box takes the value on the right. Later pages use the same shape for numbers, where the type changes and the layout stays the same.
Because std::string is a library name, the file needs one more include. Add #include
Type the file in this order, or change only the lines that differ if the output file is already on disk. Keep #include
#include <iostream>
#include <string>
int main()
{
// the word stays pine
std::string first = "pine";
std::string second = "cone";
std::cout << first << std::endl;
std::cout << second << std::endl;
return 0;
}
Rebuild from the folder that holds pine.cpp. The command is g++ -std=c++17 pine.cpp -o pine and then ./pine. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 pine.cpp -o pine. The screen is pine on the first line and cone on the second. Nothing on the screen changed from the output page, but the file now works through names. If your screen differs, compare the declaration line first, then the writing line below it, before you change anything else. The writing line std::cout << first << std::endl; prints the value inside the box, not the letters of the name.
The table lists what each piece holds. Read it once, then check your file against it. Each name holds one value. The quoted version is the one row that prints letters from a name instead of a value.
📊 Names and what they hold
| Piece | What it holds |
|---|---|
| First | pine |
| Second | cone |
| Quoted | the letters first, printed as text |
| Screen | pine then cone |
Watch what the name does. Change the first writing line to std::cout << "first" << std::endl; and rebuild. The screen shows the letters first, not pine. Quotes turn the name into plain text, so the compiler never looks up the box. Put the quotes back out, leave only first, and pine returns. Next, change the value. Edit the declaration to std::string first = "cone"; and rebuild. The screen shows cone, then cone again. Put pine back into the declaration, and the screen is pine, then cone.
Now remove the first declaration line completely and rebuild. g++ stops with an error that says first was not declared in this scope. The name exists only where a declaration makes it. Put the line back. Misspell the name in one writing line, for example Frist, and the same kind of error appears, this time for Frist. The fix is to match the spelling in the declaration and in the writing line. The error message points at the line where the name is used, not at the place where the name should have been declared. Read the name in the message, find it in your file, and check whether a declaration line exists above the writing line. A missing declaration and a misspelled name look alike to a beginner, so check the spelling first, then check that the declaration sits above the writing line that uses it.
Two rules keep the names honest. The name in the declaration and the name in each writing line must match letter for letter, uppercase included. first and First are different names, and the compiler treats them as different. A name can use letters, digits, and underscores, and it cannot begin with a digit. first and second are short on purpose. Use short names only when the reader can see what the box holds. A name like x would also compile, but it would tell the reader nothing about the word pine. The checked file keeps first and second because the names and the screen say the same thing. Do not change the comment. It still sits above the declarations and the compiler still skips it, so the note does not print. The two std::endl calls still end the two lines, and no third name and no third writing line appear.
The next page is input. Its job is to read a word that you type on the keyboard into a name, using std::cin. This page stops before that read. You leave with first holding pine, second holding cone, the comment still hidden, and ./pine showing two lines. Nothing reads from the keyboard yet, so count the lines on the screen before you leave. There are two, and each one comes from a name you can point to in the file.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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