Skip to content

Basic syntax: statements

  • Source file
  • Compiling and running a program
  • Program statements
  • Order of statement execution
  • Console output statement
  • Functions without parameters
  • The special main function
  • Function body
  • Empty function
  • Comments
  • Order of definitions in a program

What will happen when these code snippets are compiled and run?

int main()
{
}
Answer

This program will compile successfully.

The main function will run automatically when this program is started.

The function itself contains no statements in its body (the curly braces are empty). For this reason, running main will print nothing.

int main()
{
std::cout << "Hello world";
std::cout << std::endl;
}
Intuition for std::cout <<

std is short for standard. You can think of std:: as indicating a name from the standard library.

The name cout combines:

  • c stands for the word “console”;
  • out is short for “output”, as in output to the console.

<< sort of pushes the following string into std::cout.

Answer

This program will not compile. A compilation error will occur because there is no #include <iostream> line before int main() (the <iostream> header is not included). Without this line, the compiler does not know what std::cout is.

#include <iostream>
int main()
{
std::cout << "Hello world";
std::cout << std::endl;
}
Answer

This program will compile. When it runs, the statements in the main function will execute, printing “Hello world”.

What does std::endl do?

endl stands for “end line” (end + line). It ends the current line of output (like pressing Enter) and flushes the output so it appears on the screen right away.

#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
}
Answer

This code is equivalent to the following:

std::cout << "Hello world";
std::cout << std::endl;
void A()
{
}
Answer

The source code will compile, but the program will not link successfully because there is no main function (the linking stage will be covered in more detail in a later lesson).

(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status

ld is the name of the linker program.

void A()
{
std::cout << "Hello World." << std::endl;
}
Answer

A compilation error will occur because the <iostream> header is not included (there is no #include <iostream> line). The compiler does not know what std::cout is.

test.cpp: In function ‘void A()’:
test.cpp:3:10: error: ‘cout’ is not a member of ‘std’
3 | std::cout << "Hello World";
| ^~~~
test.cpp:1:1: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
+++ |+#include <iostream>
1 | void A()

To fix this, add #include <iostream> as the first line.

#include <iostream>
void A()
{
std::cout << "Hello World." << std::endl;
}
Answer

The source code will compile, but the program will not link because there is no main function. See question 1

#include <iostream>
int main()
{
std::cout << "Hello World." << std::endl;
}
Answer

The program will compile successfully:

  • the main function is defined;
  • the <iostream> header is included before std::cout is used.

When run, it will print “Hello World.”

#include <iostream>
int Main()
{
std::cout << "Hello World." << std::endl;
}
Answer

C++ is case-sensitive for function names and the names of other entities. There is no main function, so the program will not link, just as in the earlier example.

#include <iostream>
int main()
{
std::COUT << "Hello World." << std::endl;
}
Answer

A compilation error will occur because C++ is case-sensitive: cout must be written in lowercase. COUT in all caps is a different name, and there is no such name in std.

test.cpp: In function ‘int main()’:
test.cpp:5:10: error: ‘COUT’ is not a member of ‘std’
5 | std::COUT << "Hello World." << std::endl;
| ^~~~
#include <iostream>
int main()
{
// std::cout << "Hello" << std::endl;
std::cout << "World" << std::endl;
}
Answer

Only World will be printed. The line that prints Hello is commented out (// at the beginning), so it will not execute.

int main()
{
// std::cout << "Hello" << std::endl;
// std::cout << "World" << std::endl;
}
Answer

It will compile without any issues. Comments are ignored during compilation, so the compiler does not need to know about std::cout.

When run, it will print nothing.

#include <iostream>
void A()
{
std::cout << "Hello World" << std::endl;
}
void B()
{
A();
}
Answer

The source code will compile, but the program will not link because there is no main function.

14. main calls a function that calls a function

Section titled “14. main calls a function that calls a function”
#include <iostream>
void A()
{
std::cout << "A" << std::endl;
}
void B()
{
std::cout << "B-before" << std::endl;
A();
std::cout << "B-after" << std::endl;
}
int main()
{
B();
}
Answer

The program will compile successfully.

The statements execute in this order:

B(); // функцию вызвали
std::cout << "B-before" << std::endl;
A(); // функцию вызвали
std::cout << "A" << std::endl;
// A завершилась
std::cout << "B-after" << std::endl;
// B завершилась

Identify the difference between this example and the previous one. What changed, and will it change the outcome?

#include <iostream>
void B()
{
std::cout << "B-before" << std::endl;
A();
std::cout << "B-after" << std::endl;
}
void A()
{
std::cout << "A" << std::endl;
}
int main()
{
B();
}
Answer

This program will not compile. The call to A(); in function B appears before the definition of function A in the source file.

Calling B(); in the main function does not cause the same problem because function B is defined before it is called.

In this example, A must be defined before B calls it.

  • Write a program that prints "Jello" to the console.

  • Define a function A that prints A. Call it in the main function.

    Hint

    A function definition uses the following syntax.

    void A()
    {
    }

    A function call looks like this:

    A();

    The function definition must come before the function call. The following code will not work:

    int main()
    {
    A()
    }
    void A()
    {
    }

    Correct code:

    void A()
    {
    std::cout << "A";
    std::cout << std::endl;
    }
    int main()
    {
    A()
    }
  • Define 3 functions: A, B, C. Function A must call functions B and C. Each of A, B, and C must print a message indicating that it has been called (for A, use "called A").