In this video, we'll talk about compiler warnings and why they happen.
Big tip, don't ignore compiler warnings. We'll talk more about that in a minute.
The compiler has noticed a potential problem in your code,
and it's warning you about it.
That's what compiler warnings are all about.
It's only a warning because the compiler is still able to understand your code
and produce object code from it.
However it's letting you know to be aware that something could be problematic.
In this example, we've declared a variable called miles driven.
And it's an integer, it can hold any kind of whole number.
Then on the next line, we display the value of miles driven to the console.
The compiler produces a warning here.
It's warning us that you've used the variable miles driven
but you never initialized it.
So in other words, the value that it contains could be anything.
Once again, don't ignore compiler warnings.
Many programmers treat them as errors
and fix the potential issues up front.
Let's head over to CodeLite and we'll see some of these warnings.
So now I'm back in CodeLite, and I’ve created a project called compiler warnings.
And again, it's a default project just simple hello world.
Now what I’d like to do is let's use that favorite number variable again.
So let's create that here. I’m going to say int space
and the name of my variable. Let's call it favorite number.
Perfect. Now suppose
I want to print out that favorite number,
so I’m going to remove this hello world right here. And I’m just going to replace it with favorite number.
So that's all I want to do. I’ve got a favorite number I want to print it out.
Now if I try to compile this,
syntactically there's nothing wrong with this program.
But there is a problem and the compiler is warning you right here.
It's saying favorite number is used uninitialized in this function.
So in other words, you haven't told the compiler what the value
of favorite number is.
You haven't asked the user to input anything into favorite number
and you're going to print it.
The compiler telling you be careful because anything could print here.
And in this case anything, will print here. So let's run this, you can see what it looks like.
I’m going to run, I’ll build and execute
and look at what favorite number is, 4200971.
Probably, not what you expected.
That's the reason for the warning. The compiler is telling you, you never set that
variable to any specific value.
So when I print it, who knows what's going to be in there.
Okay. We'll talk more about that when we talk about variables.
So that's one of the issues that you could have.
Another issue you could have is let me go back to hello world here.
Another issue you could have is suppose that
in this case now we do assign something to that favorite number.
Let's just say favorite number is,
I don't know,
100. So look what we've done now. We've created a variable.
We've assigned a value to the variable, but we haven't used it.
So the compiler's thinking wait a minute, did you
plan to use that variable? Why did you create a variable and not use it at all?
So we'll compile,
and the warning is variable favorite number set but not used.
Okay, so it's telling you, are you sure you want this?
There's no reason for you to create a value and then not use it.
And similarly, if I get rid of favorite number here
and I just do something like that and compile,
there's the warning again, unused variable.
And you've created a variable, but you haven't even used it. So what do you want.
The idea is you should have a no warnings policy in your code.
You should have clean compiles with no warnings.
Now there's many, many more warnings, and we'll run into them when we talk about
if statements and looping
and assigning and doing math. And we'll talk more about those mornings when we get there.