Lesson 4
C++ Basic Syntax
A focused lesson on C++ program structure and basic syntax elements.
Lesson content
C++ Basic Syntax
Structure of a C++ Program
A C++ program consists of preprocessor directives, namespaces, function declarations, and the main() function which is the entry point.
Headers and Includes
Headers provide declarations for standard library functions and classes. You include them using #include directive.
- #include <iostream> for input/output operations
- #include <vector> for dynamic arrays
- #include <string> for string manipulation
- #include <algorithm> for standard algorithms
Namespaces
Namespaces organize code and prevent naming conflicts. The std namespace contains the C++ Standard Library.
First C++ Program
Your first C++ program prints a message to the console using cout and ends with a return statement.
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to C++ programming" << endl;
return 0;
}Welcome to C++ programming is the output produced by the example program.