Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
To print in C++, include the iostream library, then use std::cout followed by the output insertion syntax, <<. Each item is inserted into the same output stream in the order you write it. A semicolon ends the complete statement, so the compiler knows where this output statement stops.
#include <iostream>
int score = 42;
std::cout << "Mira";
std::cout << " scored ";
std::cout << score;
std::cout << " points.\n";
std::cout << "Language: C++\n";The first statement inserts Mira. The next statement inserts scored with one space before and after the word. The third inserts the value stored in score, which is 42. The fourth inserts points., then prints a line break. The final statement inserts Language: C++, then prints another line break. All five statements use the same output stream, so the console receives one continuous sequence of characters.
Which item from the running fragment reaches the console immediately after Mira?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The console shows the characters produced by the statements, not the instructions used to produce them. Quotation marks tell C++ where a literal begins and ends, but they are not printed. std::cout identifies the output stream, << inserts an item, and each semicolon ends a statement. None of those source tokens becomes visible. The name score also stays hidden, while its stored value, 42, appears.
Source items:
"Mira" " scored " score " points.\n" "Language: C++\n"
Console output:
Mira scored 42 points.
Language: C++The first output line is therefore Mira scored 42 points. It does not contain quotation marks, the word score, or any of the << symbols. The second line is Language: C++, because those are the characters inside its literal. The punctuation in the source and the value produced by score have different roles: source punctuation controls the program, while printed characters form the result.
A statement boundary tells C++ that one statement has ended and another is beginning. It does not insert a space. The spaces in the running output come from the literal " scored ", not from std::cout, from <<, or from the semicolon after each statement.
int score = 42;
std::cout << "Mira";
std::cout << "scored";
std::cout << score;
std::cout << " points.\n";
std::cout << "Language: C++\n";Mirascored42 points.
Language: C++In this version, the first statement prints Mira and the next prints scored immediately beside it, so the result begins Mirascored. The variable score then contributes 42 immediately after scored. The space before points. still appears because it remains inside the fourth literal. Every visible separator must be present in a literal or come from a value that itself contains a separator.
The fragment uses "scored" instead of " scored ". Type the corrected literal, including its quotation marks.
int score = 42;
std::cout << "Mira";
std::cout << "scored";
std::cout << score;
std::cout << " points.\n";
std::cout << "Language: C++\n";Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The running fragment stays on one console line until the characters \n in " points.\n" are processed. That line break ends the first line after the period. The final literal, "Language: C++\n", prints the second line and then ends it as well. The visible text before each \n remains on the current line; \n controls where the next printed characters begin.
int score = 42;
std::cout << "Mira";
std::cout << " scored ";
std::cout << score;
std::cout << " points.\n";
std::cout << "Language: C++\n";std::endl can also end the current line. For example, the statement that prints points. could insert the text " points." and then use std::endl, while the statement that prints Language: C++ could do the same. std::endl ends the line and flushes the output stream, which asks C++ to send buffered output to the console immediately. For this running output, \n is sufficient because you only need the two line breaks.
int score = 42;
std::cout << "Mira";
std::cout << " scored ";
std::cout << score;
std::cout << " points." << std::endl;
std::cout << "Language: C++" << std::endl;The two versions produce the same visible lines: Mira scored 42 points. and Language: C++. The difference is what happens to the stream after each line. \n contributes a line break, while std::endl contributes a line break and flushes. In both versions, the spaces still come from the printed text. Neither line-ending choice adds the missing spaces between Mira, scored, and 42.