Tip:
Highlight text to annotate it
X
In this video we are going to cover a little more detail on output - going beyond just
the one line we did in the previous video. And we're going to talk about what you should
do when you get compiler errors. And we'll talk about what compiler errors are and how
to deal with them. The best way to really look at and experiment with output is to do
just that - and just experiment. So this is the program we used last video. If we copy
and paste more of these lines you'll notice that once we compile and our run out program
you'll see that each line is "Hello World".
It actually goes in order. If I switch this to 1, 2 and 3. save it, recompile and run
- you can switch between previous commands by pressing up and down on the keyboard - you'll
see: 1 2 3. So when the program's running it does one line, then the next, then the
next.
Now, also in the last video we said we would cover this new line character. What this does
is, it says put enter - basically press enter once it hits this line. So if I remove this
newline character and run this program you'll see it almost prints out "12". It prints out
the "1", prints out the "2" right after that. So the newline character is quite important.
It can be placed anywhere. It's just a backslash 'n' (\n). If I place the backslash 'n', it
will give a similar output to the first one. The only difference is, is it prints the "1",
then when it gets to the second line it prints the newline character, the "2", and then the
newline character. You can place them wherever you want.
There are other special characters that use the slashes. For example, if you want to put
just an actual slash, you can put slash slash. And, that will put one slash. So, you'll that
it prints out the "1". This converts to a normal slash - and then it prints out the
"2". So it's, 1/2 and 3. So that's one way you can play around with output and experiment
with how it's represented on the screen.
Next, we're going to talk about what happens when you get compile errors. Compile errors
are errors that happen when you try to compile your code. Here we have "error.c" that's already
setup. If I try to compile "error.c", you'll see that it doesn't actually work. Once it
hits an error, it stops the compile completely. So, if we try to run "a.out" it actually prints
out our previous program because it didn't actually make our program. So let's try and
step through our errors and see if we can actually solve this so we actually get our
program working again. The way you can read errors is first comes the filename, except
because we only have one file in this case, they're all going to be in "error.c". Then
we have the estimated line, and the estimated column along the line. I say estimated because
the compiler is just guessing where the errors are - it can't actually, really, understand
what you mean, but it's trying to make it's best guess and tell you where the error is.
Then comes if it's an error or a warning. Warnings don't actually cause the compile
to fail. Warnings don't actually cause the compile to fail, but they're often the source
of problems and so when you see a warning you should probably fix it. Errors on the
other hand, cause the program to fail to compile. And those are the most important, and most
serious to fix because your program is definitely not working. So, we'll start from the beginning
and next it gives us a description of what the error probably is. It says "expected declaration
specifiers before". Well, most people right away that doesn't make much sense. It thinks
there's something before this printf that we need but it's hard to tell. So, we can
move onto the next one. This one makes a little more sense, on line 5 as well, which happens
to be (you can see the lines at the bottom) - which says it's missing a terminating quote
character. Well, if we look here you can notice that we are actually missing a second quote.
So, now that we've solved that error, let's recompile and see how it affects our output.
So, it still things there's something wrong with our line 5 starting and all these errors
are the same. So, let's see if we can fix anything else though, and we can come back
to those. It's still saying we're missing a second quote on this line here - which we
are. So, if we look at this line it says we're expecting a open brace because we have a close
brace here - Well, look at that we're missing an open brace so we need to re-add that. Now,
if we recompile, lets see what other errors we have. This one it knows kind of what's
going on - it says we're expecting a semicolon before return. So, here's return and here's
before return - look at that we are missing a semicolon. So, now our error program is
completely working. Let's try it out. As you can see our program did run and compile but,
did it run as we'd expect it to? Well, maybe not. The "2" and the "3" are right beside
each other and that's not what we want our counter to look like. What we have here is
another type of error - a runtime error, where it completely compiles and runs but it doesn't
do exactly as you'd expect. So, we have to add a newline character here to make our program
run as we thought it would. And, now our program is a bit more debugged and it works as expected.
So, there are some hints to correct errors. It's always a challenge to find errors - especially
in C compilers. The best thing is to try and decipher, take your time and slowly work through
it.
Next week we'll briefly talk about commenting.