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 anatomy of a classic "Hello, World!" program. A robust understanding of these foundational components is essential before scaling to larger projects.

Important: C++ is heavily standardized. Even simple scripts maintain a rigid structure that scales predictably to million-line enterprise codebases.

Program Structure Breakdown

#include Preprocessor Directive

Instructs the compiler to fetch and insert code from external libraries before compilation begins.

<iostream> Input/Output Stream

The specific standard library header required for terminal console inputs (cin) and outputs (cout).

int main() Entry Point Function

The mandatory starting point of every C++ program. Execution begins here and concludes when this function returns.

return 0; Exit Code

Communicates to the operating system that the program executed successfully without fatal errors.

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}

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In