Basic syntax: statements
Concepts
Section titled “Concepts”- Source file
- Compiling and running a program
- Program statements
- Order of statement execution
- Console output statement
- Functions without parameters
- The special
mainfunction - Function body
- Empty function
- Comments
- Order of definitions in a program
Analysis tasks
Section titled “Analysis tasks”What will happen when these code snippets are compiled and run?
1. Empty main
Section titled “1. Empty main”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.
2. Printing a string
Section titled “2. Printing a string”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:
cstands for the word “console”;outis 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.
3. Corrected code for printing a string
Section titled “3. Corrected code for printing a string”#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.
4. Printing on the same line
Section titled “4. Printing on the same line”#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;5. Empty function A
Section titled “5. Empty function A”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 statusld is the name of the linker program.
6. Function A
Section titled “6. Function A”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.
7. Corrected function A
Section titled “7. Corrected function A”#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
8. Function main
Section titled “8. Function main”#include <iostream>
int main(){ std::cout << "Hello World." << std::endl;}Answer
The program will compile successfully:
- the
mainfunction is defined; - the
<iostream>header is included beforestd::coutis used.
When run, it will print “Hello World.”
9. Function Main
Section titled “9. Function Main”#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.
10. COUT instead of cout
Section titled “10. COUT instead of cout”#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; | ^~~~11. Comment
Section titled “11. Comment”#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.
12. Everything commented out
Section titled “12. Everything commented out”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.
13. A function calls a function
Section titled “13. A function calls a function”#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 завершилась15. Almost the same situation
Section titled “15. Almost the same situation”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.
Practice
Section titled “Practice”-
Write a program that prints
"Jello"to the console. -
Define a function
Athat printsA. Call it in themainfunction.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. FunctionAmust call functionsBandC. Each ofA,B, andCmust print a message indicating that it has been called (forA, use"called A").