3.1 — Syntax and semantic errors

Software errors are prevalent. It’s easy to make them, and it’s hard to find them. In this chapter, we’ll explore topics related to the finding and removal of bugs within our C++ programs, including learning how to use the integrated debugger that is part of our IDE.

Although debugging tools and techniques aren’t part of the C++ standard, learning to find and remove bugs in the programs you write is an extremely important part of being a successful programmer. Therefore, we’ll spend a bit of time covering such topics, so that as the programs you write become more complex, your ability to diagnose and remedy issues advances at a similar pace.

If you have experience from debugging programs in another compiled programming language, much of this will be familiar to you.

Syntax and semantic errors

Programming can be challenging, and C++ is somewhat of a quirky language. Put those two together, and there are a lot of ways to make mistakes. Errors generally fall into one of two categories: syntax errors, and semantic errors (logic errors).

A syntax error occurs when you write a statement that is not valid according to the grammar of the C++ language. This includes errors such as missing semicolons, using undeclared variables, mismatched parentheses or braces, etc… For example, the following program contains quite a few syntax errors:

Fortunately, the compiler will generally catch syntax errors and generate warnings or errors, so you easily identify and fix the problem. Then it’s just a matter of compiling again until you get rid of all the errors.

Once your program is compiling correctly, getting it to actually produce the result(s) you want can be tricky. A semantic error occurs when a statement is syntactically valid, but does not do what the programmer intended.

Sometimes these will cause your program to crash, such as in the case of division by zero:

More often these will just produce the wrong value or behavior:



Modern compilers have been getting better at detecting certain types of common semantic errors (e.g. use of an uninitialized variable). However, in most cases, the compiler will not be able to catch most of these types of problems, because the compiler is designed to enforce grammar, not intent.

In the above example, the errors are fairly easy to spot. But in most non-trivial programs, semantic errors are not easy to find by eyeballing the code. This is where debugging techniques can come in handy.

3.2 -- The debugging process
2.x -- Chapter 2 summary and quiz

17 comments to 3.1 — Syntax and semantic errors

  • sami

    Identify lexical, syntax and semantic errors from the following C program. Write line number, error message, and type of the error for each error in the program [Marks: 4]
    01: float Factorial(int N) {
    02:       float F;
    03:       F==1;
    04:       while(i<F)
    05:         F*=i;
    06:       return F
    07: }
    08: Void mane()
    09:       integer r;
    10:       r=Factorial(5);
    11:       IF(r>0)
    12:           PrintResult(r);
    13: }

    • errors :
      line 3 used the conditional "==" operator that looks if the values are equal but used like the "=" operator
      line 4 "i" is not defined, "F" isnt declared
      line 5 since "i" is not defined you cant do stuff with it.
      line 6 missing semicolon

      line 8 c++ is case sensitive so "void" and "Void" are not the same
      line 9 used "integer" instead of int
      line 10 Factorial function doesnt return a value since we didnt put a semicolon after return also type "integer" isnt the same as "float"
      line 11 c++ is case sensitive so "if" and "IF" are not the same
      line 12 PrintResult is not defined

      this program doesnt have a "main" function

      not errors but things not to do
      uses 2 functions starting with a capital letter ( "Factorial" and "PrintResult")
      "Factorial" function has a parameter starting with a capital letter

    • M Umer Sohail Khan

      01: float Factorial(int N) {
      02:       float F;                  
      03:       F==1;                syntax error
      04:       while(i<F)          
      05:         F*=i;
      06:       return F
      07: }
      08: Void mane()         syntax errror   identifier identifier
      09:       integer r;       syntax error     indentifier indentifier
      10:       r=Factorial(5);   semantic error   type mismatch
      11:       IF(r>0)           syntax error     IF working as identifier in this line
      12:           PrintResult(r);           semantic error not defined
      13: }

  • Cherry Wan

    hello ,how to select books for c++?

  • Husen Patel

    Finally, I got a valuable C++ tutorial that tells you what real C++ Programming is.

  • Shubham

    I am following along with this tutorial for the past week, and having a lot of fun, but I want to also do some more practice using what I've learnt, but I do not know where to look. I tried Code forces but it requires conditional programming and stuff so cannot do that.

  • Thank you for the article.  It's very helpful.

  • Korachi

    All I have to say is thank and pls keep up this work it means a lot to many especially me.

  • Daniel Glover

    Do you guys have a YouTube channel?..Like I would be really interested in watch your videos since this website it the best I've every seen..Like thanks❤

  • Jalent

    I really want to thank this website, thank the people behind it especially when I occasionally can't log in the website(really desperate at that time :((. Anyway,I'll try my best to learn and do something for you if I could!!!

  • dom


    You rock. Your tutorials thus far have taught me more about life then my dad.
    I look forward to the continued enlightenment.

    Many thanks. Ill donate when I can afford :)

  • Envy

    I don't know if it's intended or not but the first code snippet has an additional error that isn't mentioned in the commment: std:cout has only one instead of two colons.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]