close

Writing Your First C++ Program in Dev-C++

By Colin
tutorial beginners cpp-basics programming

Learn how to write, compile, and run your first C++ program in Dev-C++. Complete beginner tutorial with code examples and explanations.

Writing Your First C++ Program in Dev-C++

Congratulations on installing Dev-C++! Now it’s time to write your first C++ program. In this tutorial, you’ll learn the basics of C++ programming and how to use Dev-C++ to bring your code to life.

What You’ll Learn

By the end of this tutorial, you’ll know how to:

  • Create a new C++ source file
  • Write basic C++ code
  • Compile and run programs
  • Understand basic C++ syntax
  • Fix common beginner errors

Your First Program: Hello World

Every programmer’s journey starts with “Hello World” - a simple program that displays text on the screen.

Step 1: Create a New File

  1. Open Dev-C++
  2. Click File > New > Source File (or press Ctrl+N)
  3. A blank editor window appears

Step 2: Write the Code

Type this code exactly as shown:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

Step 3: Save Your File

  1. Click File > Save As (or press Ctrl+S)
  2. Choose a location (create a folder like C:\MyPrograms\)
  3. Name your file hello.cpp
  4. Make sure it ends with .cpp extension

Important: Always save C++ files with the .cpp extension!

Step 4: Compile and Run

  1. Click the Compile & Run button (or press F11)
  2. Dev-C++ will compile your code
  3. A black console window appears with your output

Success! You’ve written your first C++ program!

Understanding the Code

Let’s break down what each line does:

Line 1: #include <iostream>

This includes the Input/Output Stream library, which allows your program to:

  • Display text with cout
  • Read input with cin

Think of it as importing tools you need for your program.

Line 2: using namespace std;

This line saves you from typing std:: before standard commands. Without it, you’d write:

std::cout << "Hello" << std::endl;

With it, you just write:

cout << "Hello" << endl;

Line 4: int main() {

Every C++ program must have a main() function. This is where your program starts running. The int means it returns an integer value.

Line 5: cout << "Hello, World!" << endl;

  • cout = “console output” (displays text)
  • << = output operator (sends text to console)
  • "Hello, World!" = the text to display
  • endl = end line (moves to next line)
  • ; = ends the statement

Line 6: return 0;

This tells the operating system the program finished successfully. 0 means “no errors.”

Line 7: }

Closes the main() function.

Your Second Program: User Input

Now let’s make an interactive program that asks for your name:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;

    cout << "What is your name? ";
    cin >> name;

    cout << "Hello, " << name << "!" << endl;
    cout << "Welcome to C++ programming!" << endl;

    return 0;
}

Save as: greeting.cpp

What’s New Here?

  • #include <string> - Allows us to use text strings
  • string name; - Creates a variable to store text
  • cin >> name; - Reads user input

When you run this, type your name and press Enter!

Your Third Program: Simple Calculator

Let’s do some math:

#include <iostream>
using namespace std;

int main() {
    int num1, num2, sum;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    sum = num1 + num2;

    cout << "The sum is: " << sum << endl;

    return 0;
}

Save as: calculator.cpp

What’s New?

  • int - Integer (whole number) variable
  • num1 + num2 - Addition operation
  • Variables can store numbers and perform calculations

Common Beginner Mistakes

Error: “undeclared identifier ‘cout’”

Problem: Forgot #include <iostream> or using namespace std;

Solution: Add both at the top of your file.

Error: “expected ’;’ before…”

Problem: Missing semicolon at end of statement

Solution: Add ; at the end of each statement.

Error: “main must return int”

Problem: Wrong function signature

Solution: Use int main() not void main()

Program Closes Immediately

Problem: Console window closes before you can see output

Solution: Add this before return 0;:

system("pause");

Dev-C++ Keyboard Shortcuts

Speed up your coding with these shortcuts:

ShortcutAction
Ctrl+NNew file
Ctrl+SSave
F9Compile only
F10Run only
F11Compile & Run
Ctrl+FFind text
Ctrl+HReplace text
Ctrl+/Comment/uncomment line

Practice Exercises

Try these exercises to reinforce what you’ve learned:

Exercise 1: Personalized Message

Write a program that asks for:

  • Your name
  • Your age
  • Your favorite color

Then displays: “Hello [name]! You are [age] years old and your favorite color is [color].”

Exercise 2: Area Calculator

Create a program that:

  • Asks for rectangle length and width
  • Calculates the area (length × width)
  • Displays the result

Exercise 3: Temperature Converter

Build a program that:

  • Asks for temperature in Celsius
  • Converts to Fahrenheit using: F = (C × 9/5) + 32
  • Displays both temperatures

Next Steps

Great job completing your first programs! Continue learning with:

  1. Master debugging: Top 10 Debugging Tips for Dev-C++
  2. Explore resources: Best Free C++ Learning Resources

Tips for Success

  • Practice daily - Even 15 minutes helps
  • Type code yourself - Don’t just copy/paste
  • Experiment - Change values and see what happens
  • Read error messages - They tell you what’s wrong
  • Ask for help - Join programming communities

Useful Resources

Want to learn more?

Last updated: January 22, 2025 | Reading time: 12 minutes