cpp
first program
03_first_program.cpp⚙️cpp
/**
* ============================================================
* YOUR FIRST C++ PROGRAM
* ============================================================
*
* This file walks you through the anatomy of a C++ program.
*
* To compile and run:
* g++ -std=c++17 03_first_program.cpp -o first_program
* ./first_program
*
* ============================================================
*/
// ============================================================
// PART 1: PREPROCESSOR DIRECTIVES
// ============================================================
/*
* Lines starting with # are preprocessor directives.
* They are processed BEFORE compilation.
*
* #include tells the preprocessor to insert the contents
* of another file at this location.
*/
#include <iostream> // Input/Output stream library
// Provides: cout, cin, endl, etc.
#include <string> // String library
// Provides: std::string class
// ============================================================
// PART 2: THE MAIN FUNCTION
// ============================================================
/*
* Every C++ program MUST have exactly one main() function.
* This is where program execution begins.
*
* int main() means:
* - 'int' = the function returns an integer
* - 'main' = the function name (special - entry point)
* - '()' = no parameters (you can also use int argc, char* argv[])
*/
int main() {
// ========================================================
// PART 3: STATEMENTS
// ========================================================
/*
* Statements are instructions that perform actions.
* Each statement ends with a semicolon (;)
*
* std::cout = "standard character output" (console output)
* << = insertion operator (sends data to output)
* std::endl = end line (newline + flush buffer)
*/
// Simple output
std::cout << "Hello, World!" << std::endl;
// Multiple items in one statement
std::cout << "Welcome to " << "C++ Programming!" << std::endl;
// Using variables
std::string name = "Learner";
int year = 2024;
std::cout << "Hello, " << name << "!" << std::endl;
std::cout << "You're learning C++ in " << year << std::endl;
// ========================================================
// PART 4: COMMENTS
// ========================================================
// This is a single-line comment
/*
* This is a
* multi-line comment
*/
// Comments are ignored by the compiler
// Use them to explain your code!
// ========================================================
// PART 5: RETURN STATEMENT
// ========================================================
/*
* The return statement ends the function and sends
* a value back to the caller.
*
* For main():
* return 0; = Program executed successfully
* return non-zero; = Program had an error
*
* The operating system receives this value.
*/
return 0; // Success!
}
// ============================================================
// PROGRAM OUTPUT:
// ============================================================
/*
Hello, World!
Welcome to C++ Programming!
Hello, Learner!
You're learning C++ in 2024
*/
// ============================================================
// KEY TAKEAWAYS:
// ============================================================
/*
* 1. #include brings in external code
* 2. Every program needs main()
* 3. Statements end with semicolons
* 4. std::cout outputs to console
* 5. << chains multiple outputs
* 6. return 0 indicates success
*/
// ============================================================
// EXERCISES:
// ============================================================
/*
* 1. Modify the program to print your own name
* 2. Add more cout statements with different messages
* 3. Try removing a semicolon - see what error you get
* 4. Try removing #include <iostream> - see what happens
* 5. Change return 0 to return 1 - check echo $? after running
*/