C++ Tutorial

C++ Output

cout sends text to the console. endl and \n start a new line. This is how every first program talks back.

cout writes to the console

cout is the standard output stream. You send values to it with the insertion operator<<. Text goes in double quotes. After the program runs, you see those characters in the console — on StudyGrid, that is the output pane under the editor.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Hello from cout";
  return 0;
}

Open examples with Try it in C++. That is /cpp/try: a g++ compile-and-run editor. It.

Chain several pieces with <<

One cout can take many insertions. Each << adds the next value to the same line unless you insert a newline. Mixing strings and numbers is normal.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Score: " << 12 << " points" << endl;
  cout << 3 << " plus " << 4 << " is " << 3 + 4 << endl;
  return 0;
}

The order is left to right. First the label, then the number, then more text. You do not need a separatecout for each value.

endl versus \n

Both start a new line. endl writes a newline and flushes the output buffer. \n is just the newline character inside a string. For these lessons either is fine. This tutorial uses endl in most complete programs so each line is easy to see.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "first line" << endl;
  cout << "second line\n";
  cout << "third line\nfourth line" << endl;
  return 0;
}

Without a newline, the next cout continues on the same line. Forget that and two messages glue together: HelloWorld instead of two words.

Numbers, characters, and strings

cout prints integers, floating-point values, single characters, and quoted text. Adouble may show without trailing zeros. A char prints as a glyph, not as a number, when you store a character literal.

Example

#include <iostream>
using namespace std;

int main() {
  int count = 7;
  double price = 2.5;
  char grade = 'A';
  cout << count << endl;
  cout << price << endl;
  cout << grade << endl;
  cout << "done" << endl;
  return 0;
}

Spaces are your job

C++ does not insert spaces between chained values. If you write cout << "Hi" << "there", you get Hithere. Put a space in a string, or insert " ", wherever a gap should appear.

CodeWhat you see
cout << 2 << 5;25
cout << 2 << " " << 5;2 5
cout << "n=" << 3;n=3

You need iostream

cout lives in <iostream>. Include that header at the top. Withusing namespace std; you write cout; without it you write std::cout. This track keeps the shorter form.

Example

#include <iostream>

int main() {
  std::cout << "same stream, longer name" << std::endl;
  return 0;
}

Next: comments, so you can leave notes next to the lines that print.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Physics

Cells in series

Identical cells in series add their emfs. Four 1.5 V cells give 6.0 V, ignoring internal resistance. The count of cells is an int. The voltage is not.

cout prints both on one line. Setprecision belongs later; here the default already shows 6.

V_total = n × V_cell

Real batteries sag under load. This is the open-circuit sum, not what a motor would actually see.

Example

#include <iostream>
using namespace std;

int main() {
  int cells = 4;
  double volts = 1.5 * cells;
  cout << cells << " cells -> " << volts << " V" << endl;
  return 0;
}

Astronomy

A Mars year

Mars takes about 687 Earth days to go once around the Sun. That is a sidereal year, measured against the stars, not a calendar date on Mars.

cout can mix a name and an integer on one line. Mission logs look like this: an object, then a number, then a unit implied by the sentence.

Example

#include <iostream>
using namespace std;

int main() {
  cout << "Mars year ~ " << 687 << " Earth days" << endl;
  return 0;
}

FAQ: C++ Output

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ cout examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ cout in this C++ C++ lesson (C++ Output).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.