Setting up a C++ Development Environment

Forgetting Semicolons

What happens if I forget to put a semicolon at the end of a line?

3D art showing a blacksmith character

When you forget a semicolon in C++, the compiler will generate an error because it can't figure out where one statement ends and the next begins. The error messages can sometimes be confusing though!

Let's look at an example:

#include <iostream>
using namespace std;

int main() {
  // missing semicolon!
  cout << "First line\n"   
  cout << "Second line\n";
}
error: expected ';' before 'cout'

The compiler tries to help by pointing to where it got confused, but sometimes the error appears on the line after the missing semicolon. This is because C++ doesn't know a statement is supposed to end until it hits either a semicolon or something that can't possibly be part of the current statement.

To make your code easier to debug:

  • Add semicolons as you write each line, don't wait until the end
  • When you get errors, always check the line before the error for a missing semicolon
  • Use proper indentation to make statement boundaries clearer
  • Most modern IDEs will highlight missing semicolons as you type

Remember: In C++, semicolons are statement terminators, not line terminators. This means you can actually write multiple statements on one line (though it's not recommended for readability):

#include <iostream>
using namespace std;

int main() {
  cout << "One"; cout << "Two"; cout << "Three";  
}
OneTwoThree

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved