Your First C++ Program
Writing and running your first C++ application
Interview Relevant: Foundation for all C++ development
Your First C++ Program
Let's break down the classic "Hello, World!" program to understand C++ basics.
Program Structure
- #include: Preprocessor directive to include header files
- iostream: Input/Output stream library
- main(): Entry point of every C++ program
- return 0: Indicates successful program execution
Code Examples
A complete C++ program with comments explaining each part.
cpp
1#include <iostream> // Include the I/O library
2
3int main() {
4 std::cout << "Hello, World!" << std::endl;
5 return 0; // Return success
6}Interactive program that reads user input.
cpp
1#include <iostream>
2using namespace std; // Avoid typing std:: repeatedly
3
4int main() {
5 cout << "Enter your name: ";
6 string name;
7 cin >> name;
8 cout << "Hello, " << name << "!" << endl;
9 return 0;
10}