ProgramGuru

What Is C++?

What Is C++?

A school needs to display a student’s mark. A person can type it manually, but a program can store the mark, calculate a result and display it by following instructions.

C++ is a programming language: a set of rules for writing those instructions as source code. A compiler translates that source into a program your computer can run. C++ is used in applications such as games, tools and embedded devices, where controlling data and resources can matter.

You do not need earlier C++ knowledge. This course starts with small school examples and builds toward a student report. We use C++17 for the examples; newer C++ standards also exist.

What you’ll learn

  • Distinguish source code, a compiler and a running program.
  • Trace a stored student mark to the displayed output.
  • Change the mark, rebuild the program and predict its new output.

Follow a complete C++ example

This is a complete C++17 program. #include <iostream> makes the standard input/output declarations available. main is where this program begins running, and braces group its instructions.

int marks = 72; stores the whole number 72 under the name marks. std::cout displays that stored value, and \n ends the line. return 0; signals successful completion to the environment. The semicolons end the statements.

C++17 · main.cpp · complete program

#include <iostream>

int main()  {
  int marks = 72;
  std::cout << marks << '\n';
  return 0;
}

Expected output

72
Run in C++ compiler →

Connect the code to its result

C++17 · source used in this explanation

#include <iostream>

int main()  {
  int marks = 72;
  std::cout << marks << '\n';
  return 0;
}

Expected output

72

First, the program creates marks with value 72. Next, it reads that value and sends it to standard output, producing the line shown below. Finally, it returns 0; this completion status is not printed as another line.

The compiler checks and translates the source before these runtime steps occur. The program cannot know that 72 is a school mark unless we write rules for that meaning; changing the stored number changes the displayed number.

Important mistakes and boundaries

C++ does not prevent every runtime mistake. Correct types, valid lifetimes and checked inputs are still your responsibility.

Change the school example

C++17 · edit this complete example

#include <iostream>

int main()  {
  int marks = 72;
  std::cout << marks << '\n';
  return 0;
}

Your task: Change the mark from 72 to 80. Before running your edited program, predict the new output and explain which stored value, condition or object changes. Keep the rest of the example intact.

Use the complete source repeated here; this task does not depend on remembering code from another section.

Worked answer and reasoning

Worked variation: Change the mark from 72 to 80. The printed value becomes 80.

The initializer now stores 80. The same output statement reads that new value, so it displays 80. The program’s completion status remains 0 and is still not printed.

C++ does not prevent every runtime mistake. Correct types, valid lifetimes and checked inputs are still your responsibility.

For a baseline run, save this source as main.cpp and use clang++ -std=c++17 -Wall -Wextra -pedantic main.cpp -o school, or equivalent g++ flags. Recompile after editing so that you run the updated executable.

The source and output below belong to the edited program. Compare them with the original example to see the consequence of this single change.

C++17 · worked variation · complete program

#include <iostream>

int main()  {
  int marks = 80;
  std::cout << marks << '\n';
  return 0;
}

Expected output

80
Run in C++ compiler →

Check your understanding

C++17 · original program for this question

#include <iostream>

int main()  {
  int marks = 72;
  std::cout << marks << '\n';
  return 0;
}
Before making the practice edit, what does the original C++17 program print?
Choose one answer. You can try again.

Answer and explanation

72

marks is initialized to 72, and the output statement reads that value. return 0 ends the program successfully without printing 0.

What to remember

C++ is a language for describing computations and organizing data. C++ does not prevent every runtime mistake. Correct types, valid lifetimes and checked inputs are still your responsibility.

Useful connections: Your First C++ Program, Give a Task a Name.

Language and design guidance: C++ Core Guidelines and C++ working draft. The draft describes an evolving language; these examples deliberately target C++17.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Set Up a C++ Compiler →