← Back to C++ Course | Chapter 1: Introduction & Basics | Lesson 4 of 15

C++ First Program

Your first C++ program is like saying hello for the first time. You write a main function that shows a message on the screen and then finishes.
Syntax
cpp
#include <iostream>

int main() {
  // statements
  std::cout << value << std::endl;
  return 0;
}

The Main Function

Every C++ program needs exactly one main() function, because that's the function the operating system calls to start your program running.

Execution always begins at the first line inside main() and continues from there, regardless of where other functions are defined in the file.

Example: The Main Function

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Execution starts here" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Printing Output

cout, short for 'character output', sends data to the console using the << stream insertion operator, which you can chain to print multiple values in one statement like cout << "Age: " << age.

Think of << as an arrow pushing data into the output stream, one piece at a time.

Example: Printing Output

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare age and set it to 25
	int age = 25;
	// Print to the console with cout
	std::cout << "Age: " << age << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Header Files

Header files declare functionality that isn't built into the core language itself, so you have to explicitly pull them in with #include.

<iostream> specifically declares cout, cin, and the other stream objects — without including it, the compiler wouldn't recognize cout at all.

Example: Header Files

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "cout comes from <iostream>" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard Namespace

The std namespace groups together every name the standard library defines, which prevents naming collisions with your own code.

Writing using namespace std; lets you write cout instead of the fully qualified std::cout, though larger projects often skip this and write std:: explicitly to avoid ambiguity.

Example: Standard Namespace

cpp
// Include std::cout and std::cin
#include <iostream>
// Lets us write cout instead of std::cout
using namespace std;

// Program execution starts in main()
int main() {
	// Print to the console with cout
	cout << "No need for std:: prefix now" << endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Returning Zero

return 0; at the end of main() isn't just a formality — it's the exit code the operating system receives, and by convention 0 means the program finished successfully.

Returning a nonzero value instead signals that something went wrong, which shell scripts and other programs can check for.

Example: Returning Zero

cpp
#include <iostream>

int main() {
	std::cout << "Program finished successfully" << std::endl;
	return 0; // 0 = success exit code
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting #include <iostream>, which makes cout undefined and produces a compile error.
  2. Writing cout << "Hello" without std:: or using namespace std;, so the compiler reports that cout was not declared.
  3. Using void main() instead of int main(), which is not standard C++ and may not compile on strict compilers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.