CHAPTER 03
Beginner
C++ Syntax and First Program
Updated: May 17, 2026
5 min read
# CHAPTER 3
C++ Syntax and First Program
1. Introduction
Every language has grammar rules — in programming, we call this syntax. C++ is strict about its syntax. Just as English sentences end with a period, C++ statements end with a semicolon (;). In this chapter, we will dissect the classic "Hello World" program.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the structure of a C++ program.
-
Explain
#include <iostream>.
-
Understand the role of the
main()function.
-
Use
std::coutto print text.
-
Understand the purpose of
using namespace std;.
3. Anatomy of a C++ Program
Let's look at a standard C++ program:
cpp
Let's break it down line by line:
| Component | Meaning |
|---|---|
#include <iostream> | A preprocessor directive. It tells the compiler to include the Input/Output Stream library. Without this, cout won't work. |
int main() { | The entry point of every C++ program. int means it returns an integer. { marks the beginning of the code block. |
std::cout | "Character Output". It prints text to the screen. std:: means it belongs to the standard namespace. |
<< | The insertion operator. It pushes the string "Hello, World!" into cout. |
std::endl | Ends the line (moves cursor to the next line) and flushes the output buffer. |
; | The semicolon signifies the end of the statement. |
return 0; | Exits the main() function. Returning 0 tells the OS the program ran successfully. |
4. Namespaces
Typingstd:: before everything gets tedious. A namespace is a declarative region that provides a scope. To avoid typing std::, you can add using namespace std; at the top of your file.
cpp
*(Note: In large enterprise projects, using namespace std; is discouraged to prevent naming conflicts, but it is perfectly fine for beginners).*
5. Comments in C++
Comments are text ignored by the compiler. They explain code to humans.
cpp
6. Mini Project: Simple Calculator (Hardcoded)
Let's use our basic syntax to create a program that does simple math.
cpp
7. Memory-Level Explanation
When the OS runs your C++ program, it looks for the specific memory address labeledmain. Execution strictly begins at the first opening brace { of main() and ends at the closing brace }.
8. Common Mistakes
-
Forgetting the semicolon:
cout << "Hi"instead ofcout << "Hi";. This is the #1 beginner error.
-
Wrong arrows for cout: Using
>>instead of<<. Think of<<as arrows pushing data *into* the console.
-
Case Sensitivity:
Main()orCoutwill cause compiler errors. C++ is strictly case-sensitive.
9. Best Practices
-
Always indent code inside curly braces
{ }for readability.
- Add comments explaining *why* you did something, not *what* you did.
-
Prefer
\noverstd::endlif you are printing in a massive loop, asendlflushes the buffer which can be slow.
10. Exercises
-
1.
Write a C++ program that prints your name, age, and country on three separate lines using a single
coutstatement.
- 2. Intentionally remove a semicolon from your program and try compiling it. Read the compiler error message.
11. MCQ Quiz with Answers
Question 1
What is the entry point of a C++ program?
Question 2
Which symbol marks the end of a statement in C++?
Question 3
What does #include <iostream> do?
Question 4
What does cout stand for?
Question 5
Which operator is used with cout?
Question 6
What does return 0; indicate in main()?
Question 8
What does std stand for in std::cout?
Question 9
Which of these is a correct single-line comment?
Question 10
What is the difference between \n and endl?
12. Interview Questions
-
Q: Explain the significance of the
main()function in C++.
-
Q: Why is
using namespace std;considered bad practice in large software projects?
- Q: Explain the compilation process of a C++ file.
13. Summary
The structure of a C++ program relies on#include <iostream> for I/O operations, the main() function as the starting point, and {} to define code blocks. std::cout is used to print text, and all statements must end with a semicolon.