Register

C++ for Beginners

0 of 24 notes read 0%

Reading progress is saved to your INFIRALS account. Sign in to keep this note marked as read.

The shape of a C++ program

Starting out9 min readLesson 2 of 6

2.1 One entry point, many statements

Every standard C++ program has a main function. Execution begins at its first statement. main returns an int, and return 0; reports success to the operating system. The return statement is optional for main, and writing it makes the ending explicit.

A statement is one complete instruction, usually ending in a semicolon. A block groups statements between { and }. Whitespace and line breaks do not change meaning, so indentation exists for human readers.

cpp
#include <iostream>

int main() {
    std::cout << "One step at a time.\n";
    return 0;
}

Output

One step at a time.

2.1.1 Headers and namespaces

The line #include <iostream> asks the preprocessor to copy declarations for input and output streams into the file. std::cout is the standard output stream. The std:: prefix says that cout belongs to the std namespace, which keeps standard names separate from names you write.

You can type the prefix each time, import one name with using std::cout;, or import all of std with using namespace std;. The prefix is the clearest choice in shared code. Keep a using declaration small in scope.

cpp
#include <iostream>

using std::cout;

int main() {
    cout << "One imported name.\n";
    cout << "The std prefix is gone.\n";
    return 0;
}

Output

One imported name.
The std prefix is gone.

2.1.2 Sending text to the screen

The << operator inserts a value into a stream. It chains, so std::cout << "Total: " << 42 << "\n"; sends text and a number in order. A string literal sits between double quotes. Escape sequences inside a literal start with a backslash:

  • \n newline
  • \t tab
  • \\ backslash
  • \" double quote

The stream adds no spaces or newlines by itself. Every piece appears exactly where the insertion operators put it.

cpp
#include <iostream>

int main() {
    std::cout << "Folder: C:\\cpp\\notes\n";
    std::cout << "\"Quoted text\"\n";
    std::cout << "A new line starts here.\n";
    return 0;
}

Output

Folder: C:\cpp\notes
"Quoted text"
A new line starts here.

2.1.3 Comments and layout

A comment explains why the code does something. The compiler ignores it.

  • // comments run to the end of the line.
  • /* ... */ comments can span lines.

Use comments for decisions a reader cannot infer from the code. A comment that repeats the next line adds noise. Indent each block by four spaces so nested statements line up, and keep braces on their own lines when that helps you scan a block.

cpp
#include <iostream>

int main() {
    // A single-line comment.
    /* A block comment
       can span lines. */
    std::cout << "Comments are ignored.\n";
    return 0;
}

Output

Comments are ignored.

2.1.4 Syntax errors beginners meet

  • Missing semicolon after a statement.
  • Missing closing brace, especially after an if or a loop.
  • Using = where == belongs.
  • Calling Main or Count when the declared name differs in case.
  • Forgetting #include <iostream> before using std::cout.
  • Adding a semicolon after if (condition) and expecting a block to follow.

The compiler points to a line, and that line is often where it noticed the problem. The cause can sit on the line above, so check the end of the previous statement too.

cpp
#include <iostream>

int main() {
    std::cout << "Part one "
                 "and part two.\n";
    int total = 3 +
                4;
    std::cout << "Total: " << total << "\n";
    return 0;
}

Output

Part one and part two.
Total: 7

2.1.5 A small formatting habit

Use \n for newlines unless the program needs the stream flushed right away. std::endl inserts a newline and flushes the stream. Either works in a beginner program, and \n is the simpler default.

Start each output line with a label so the result reads like a short report. A run that prints Total: 15 takes less interpretation than a bare 15.

2.1.6 Reading input

Programs that need typed data use std::cin >> value;. The stream reads one whitespace-separated token and stores it in value. For a whole line, including spaces, use std::getline(std::cin, line) after including <string>. Input is interactive, so the terminal waits for you to type and press Enter.

The >> operator skips leading whitespace for numbers and words. getline reads up to the newline and keeps the spaces inside the line. If a >> read leaves a newline in the stream, a following getline can return an empty line. Call std::cin.ignore() before getline when that happens.

2.1.7 Practice

  1. Write a program that prints your name on one line and a subject on the next.
  2. Add a blank line between two sections of output.
  3. Print a path with backslashes and a sentence with quotation marks.
  4. Break the code in three different ways, one at a time, and read the first error each time.

Interview questions

3 questions from this note. Open one to see the answer; no account needed.

  • 01Why does execution start at main?

    The C++ runtime calls main after the program starts. Other functions run only when main or another function calls them.

  • 02What does std:: before cout mean?

    It names the std namespace and tells the compiler where to look for cout. The prefix prevents collisions with names from other libraries.

  • 03What is the difference between newline and std::endl?

    Both insert a newline. std::endl also flushes the stream immediately. Use newline unless the flush is needed.