Hello and welcome.
In this video, we're going to talk about compiler warnings in big, big tip.
Do not ignore compiler warnings.
A lot of times you'll see them in the build output window and some new programmers will just ignore
them.
You do not want to ignore them.
Basically, warnings by the compiler are telling you that there's an issue with your code that could
lead to a potential problem.
The code is syntactically correct.
The compiler is able to generate object code and able to generate your program, but it's not perfect
code and there could be issues with the code.
It's only a warning again, because syntactically it's correct and you can generate that machine code.
So for example, if we create a variable called total and then we try to display the value of that variable
total, we'll get a warning in the build output window saying total is used.
But it hasn't, but it's uninitialized.
And what that means is basically saying total doesn't have a value associated with it.
It's in memory somewhere and it doesn't have a value.
So when you display the you try to display that value, it could display anything.
And so again, it's syntactically correct, but it doesn't really you have to initialize your variable.
So the compiler tells you and so you don't want to ignore compiler errors.
In fact, most programmers will treat warnings as errors and they will fix all their warnings.
Now we actually turn on the the compiler warnings for all.
Warnings because there's different categories of warnings that you can turn on turn off.
We use the dash w all compiler flag so the all warnings are displayed and you really should fix all
of your warnings.
So let's look at an example in code like so here's our Hello world example.
Let's actually just do what we just did in the slide and create a variable total and don't assign any
value to it and then try to print it out.
And so if we say total is and we try to print it out and then we try to compile it, you'll see that
we get a warning, right?
Basically saying total is used on initialized.
We can still run the program.
If we go up to build, we can run it because again, it's able to generate the code.
It's just a warning.
And what we see is it says total is zero and that can actually be any number because it hasn't been
initialized in C program.
And you always want to initialize all of your variables so that they actually contain some valid value.
A lot of times in C you the actual memory that a variable stores it will be inherited from a previous
program.
So if you don't initialize your variable, it could be anything.
In this case when you ran, it happened to be zero.
But we just kind of got lucky, right?
It could be any value.
So you always want to fix those warnings.
Now, if we go back to our Hello World application, let's go to Project three and make that the active
project.
If we have a Hello world type application in here, we'll create an actual variable Again, total,
we'll initialize it so we won't have the initialization warning and we'll actually try to compile this.
And in this case, when we compile, we see another type of warning, right?
Unused variable.
So this is saying we created a variable, but we never used it.
We never displayed it.
We never used it for computation or arithmetic or anything like that.
So why do we create it?
Why are you creating a variable that you never use?
It's sort of wasteful code, dead code.
So you should delete it, right?
Because we have an unused variable and so we can remove the initialization and still not use it, right?
So if we actually have something like this and we save it and again we compile it, you're going to
see unused variable.
So the unused variable warning takes precedence over the initialization warning, we never used it.
So we're going to see a lot of warnings in your code as we go through and we learn about if statements
while loops all the different concepts of C, We'll talk about some of those warnings in later lectures.
But really what you want to learn from this lecture is that warnings are a big deal.
You don't want to ignore warnings.
In fact, you should have a policy that whenever you see a warning, your goal is to fix it.
Do not ignore warnings.
Thank you.