- Let's continue our discussion of variables.
So far we saw different ways in declaring variables.
Now let's talk a little bit about
when and where it makes the most sense.
You already know that variables must be declared
before use or the compiler would not understand
the meaning of your code.
Now you have a few choices.
You could declare your variable all in one place.
This will make, in some cases,
the program more readable
and will give you an opportunity to create a header
at the beginning of your code
detailing all the variables and listing their intended use.
Additional option, is you declare them as you need them.
If you're only going to use a variable in one place,
in one small portion of your code,
it might make sense to declare the variable right there.
Finally, you could declare all your variables
in an external file and use include with a .h and .c
pair of files, just as we saw with functions.
Let's take a look at the code and illustrate this in action.
This particular program uses, as you can see,
multiple types of variables, declared in multiple places.
So let's take a look.
We have an int x, int y, and int z.
This is simply an example, as we saw in the previous video
that you could group together, few variables,
and have them all in one place.
There's nothing particularly important
that this program does.
I just wanted to illustrate to you the fact that
you could put them in one place or not.
In line 20, is it where we use all three.
Let's take a look at line 23 and line 24.
In this particular program, the variable A
is only used at that particular position.
So it makes sense to define a right before
I'm using it to getchar and return the value.
The same goes for b, and the same goes for c.
It would've been perfectly correct and perfectly fine
to declare x y z and a b c, all six of them
at the beginning of your program
and then use them as needed.
Ultimately it's up to your personal style,
the conventions in place at your professional location
and other considerations such as style,
who else you're working with, and so on.
You will eventually develop your own preference,
your own style, and you'll figure out
what works best for you and is most appropriate
for the project at hand.
Let's look at line 35.
First of all you would notice
that it doesn't exactly fit on the screen
and it's not clear right away
what this line is suppose to do.
Careful reading reveals that we first declare
a double xx 2.3, then a double yy 1.2
then a double zz 2.3, then we are printing the value
of xx, then we have a comment,
then we have another print, and so on.
This is actually perfectly correct in c.
If you're trying to deliberately make code harder to read,
harder to debug, harder to understand, even to you
if you should come back to this later,
then by all means go ahead and write code like this.
Let's test this code and see what it does when run.
Notice that there is no prompt.
This is because on line 24, getchar simply expects
the user to enter something without any prompt.
Knowing that, I'm gonna type a number "1" , enter
and a second 1, and now I'm getting to
the output of the program.
As you can see, this particular program,
variables were declared in a very very messy way.
They're hard to read, and they probably impacted
the programmers ability to think efficiently
about displaying the results.
The output as you can see, it's also very hard to read.
Note, there is no relationship between
how you write your code and how your output is presented.
You can write code that is very messy,
very hard to read, and yet the output could be very easy.
For example, if I were simply going to reformat the printf
we can get a better outcome.
However, it is not necessary for this example
as we will have plenty of opportunity to practice
in the upcoming videos how we can improve the formatting
of text on the screen.
To recap our discussion this video is,
you can declare variables in multiple ways,
in multiple locations.
You can make your code easy to read or hard to read.
You are the master of your code.
Whatever you put in there,
this is what other people will see,
this is what you will see later on
when you come back to look at it.
It's ultimately up to you to write the best,
most clear code you can.