Hello and welcome.
In this video, I want to explain to you what compiler errors are.
Up to this point.
Right.
We've we've written some code and we've noticed that in the build output window, we issue a compile
command and we know when we have errors.
Right.
Usually there's a nice description that says there's an error in your code, but what exactly does that
mean?
So when you write code for a programming, languages, programming languages are very, very strict.
They have rules.
And you have to follow those rules in order to get a program executable.
A compiler will actually check the syntax of the code.
And if it's correct and if there's no semantic errors, it will generate from the source code, machine
language type code, and you can run a program.
And so you have to follow these rules and they're very detailed rules.
And so you have to be very detail oriented.
So compiler errors can come in two forms.
They can come in the form of a syntax error or a semantic error.
A syntax error is when you have something wrong with the structure of the code, you're not following
the rules of the language in terms of its syntax, right?
You're not putting parentheses in the right correct space.
You're not adding all of your statements with a semicolon.
You're not you're misspelling keywords.
These are all common examples.
A semicolon is a really common one when you're first starting off.
So if you declare a variable and you assign it to five but you don't have that semicolon at the end,
that's a syntax error.
Something's wrong with that structure.
The same as if you were to use a printf statement and you say a printf statement inside double quotes,
but you forget the ending, double quote.
That's a syntax error.
And so something is wrong with that structure in the compiler will typically when it scans your source
code and it finds this error, it will usually give a descriptive error meaning that can point you in
the right direction and also usually gives the line number.
And we've seen that in previous videos.
The other type of error that the compiler can catch is a semantic error.
It doesn't always catch semantics errors if they're logic errors, but semantic usually means something
is wrong with the meaning.
The intent of the program.
So, for example, if you were to add two variables A and B, but those variables were of different
types and it didn't make sense, right?
It doesn't make sense to add A and B.
The compiler can sometimes catch these semantic errors.
Other semantic errors have to do with logic and we're going to discuss those.
And the compiler doesn't always find those.
The structure was correct and everything was fine with the structure, but logically something was incorrect.
There's an error in the logic of the code.
So when you think of semantic errors, what the compiler can catch, it's more has to do with type catching
and and things that you're not supposed to do with, with different rules of the language concerning
like variable types.
And so now let's take a look in code light to see some common examples of some syntax and semantic errors
in the C programming language.
So here we are in code light with a simple Hello world type application.
This is code that's generated for you from your project settings.
If you've saved it as a template.
Very simple code right?
And when you compile this file, it will tell you whether or not you have errors in syntax.
You can right click again and you can say compile.
And right now we don't have any errors, right?
But syntactically we're following all the rules.
But the syntax includes, again, all the structure of the code.
So if you don't have a semicolon or if you don't have a pound symbol before an include and you try to
compile it.
You're going to see that there's an error and usually the error compiler error has some kind of meaning.
Now, as you write more and more code and you get more and more practice, these compilers aren't going
to be so obtuse.
They're not going to be so non-descriptive you're going to kind of be familiar with like, for example,
this error says we have an error on line one, right?
And so it's nice.
You can double click this.
It will go to that error, but it says expected equals know semicolon.
It doesn't really tell you what is wrong.
You know, through programming practice you'll realize, oh, I forgot the pound symbol.
So some compiler errors aren't as descriptive as you'd like if you're new to programming.
So you add that, add that pound symbol, you recompile and you're good to go.
If you misspelled standard i o sometimes it won't be able to find it.
So you save it and you recompile and you see an error that says a little more descriptive.
Right?
No such file or directory Stdio.h that kind of tells you like, oh, maybe you misspelled it.
So misspellings are common errors.
You know, not putting things in the proper perspective in terms of mismatched parentheses.
You know, you forget a parentheses, you know, again, you recompile.
Let's see what that error looks like.
Again, that's not super descriptive.
It's saying, you know, there's basically a parentheses before the ending curly brace because it doesn't
know it thinks maybe that this right parentheses is going to end somewhere down in here.
It can't always tell you exactly what's going on.
So parentheses and missing curly braces sometimes aren't as descriptive.
But if you have something like a missing semicolon, that's pretty straightforward.
That compile error tells you exactly what's going on.
Expected semicolon before the return kind of even points you to where you're supposed to put that semicolon.
So you can double click that, put a semicolon if you're missing a double quote and you recompile.
Right?
You see now something that says basically missing, terminating double quote.
And again, it kind of points you to where the first quote happens and it gives you multiple errors,
Right, even though you only really have one.
So you had that double quote and you're good to go.
Now, this main is saying you need to return an int.
What if you actually tried to return something like inside inside of double quotes?
Again, you're not following that structure of the language and it tells you basically you're returning
a character pointer from a function type.
Again, these compile errors may not make sense to you right now, but as you write more and more code,
they will make more sense.
But it's all about following that structure.
Now, if you try to do things that make it make sense syntactically, but the meaning is not correct.
So for example, say we do a print F and then we try to do an arithmetic operation like dividing by
eight or dividing by seven.
We save it and we compile it and now we see more of a semantic error where we have a division symbol
after the double quotes.
You know where it was actually expecting Semicolon C Mainly we'll find syntax errors, semantic errors.
The meaning of the code usually have to do with logic.
Not found as much semantic errors happen more in a language like C plus plus when you're using.
Objects.
So be on the lookout for syntax errors from the compiler.
Common things misspelling misuse of.
Keywords not ending your parentheses when you're starting them blocks of code when you don't have the
ending and beginning block And you can have multiple errors, right?
So say we say we we have Hello world.
We're missing a semicolon.
We're returning a string.
You know, we're missing a bracket, you know, and we build it.
And this is a rather small program.
But when we try to compile it now, we're seeing multiple errors, right?
Two errors.
And it's kind of tricky, like, where do I start?
Usually what you'll do is you'll fix the first error because sometimes one error can cause more errors.
So you always want to look at the compiler error at the very top and fix that one first.
So you want to fix the one on line five, right?
And then you may want to fix and then recompile and then maybe fix the one on line six after that.
And now it says Jason.
So now you can say return zero, but always fix that first error before you fix the subsequent errors
because one can be causing the other one.
And by the way, we're always going to have errors in our code.
We even the most seasoned programmers are going to have syntax errors.
And so it's common to have errors.
What's really important is understanding the messages that are coming from the compiler to tell you
how to correct them.
And again, that will come in practice.
We'll talk more about compilers, common compilers in future videos, but just try to make sure you're
following structure of the language as you learn it, the syntactic languages and so on.
The next few lectures we'll talk about how we can have different types of errors.
I mentioned logic errors.
We can also talk a little bit about warnings.
We'll also talk about linker errors and runtime errors.
Thank you.