- Occasionally when learning something new
you need to pause and reflect.
That's the topic of this movie, pausing and reflecting.
Specifically with regards to variables and values,
it's time for me to go back and get you caught up
on some things that might've already been seen
in an earlier movie, but you have questions about.
Plus I'll introduce a few new concepts.
Things I feel it's about time you knew, especially
if you are new to learning the C programming language.
In this movie I cover creating and using constants.
Plus I offer additional detail on how to use some
of the printf function's placeholders.
A constant in the C language is different from
an immediate value and a variable.
It's more like a universal value
that doesn't change throughout the code.
Constants are values used consistently
throughout your source code.
They are things that don't change.
They are created by the preprocessor #define directive.
Normally the constant is written in
all caps, letters and underlines only.
Now when the compiler encounters a constant
it expands it out to its assigned value.
In this example the constant VERSION is created
and it's assigned to the value three.
The compiler converts the word VERSION into
the immediate value three throughout the code.
And remember that preprocessor directives
are not C language statements.
Don't put a semicolon after the assignment,
unless you need the semicolon to be
a part of whatever's being defined.
That hardly ever happens, so just don't add a semicolon.
Here is a string constant defined as AUTHOR.
The string must be enclosed in double quotes.
This is an example of one constant that uses
two other constants to calculate its value.
So here the constant GAME_GRID would equal 120
unless either the ROWS or COLUMNS constants are changed.
And they wouldn't be changed in the code,
they would be changed in the define
statement that you see here.
This code is based on an exercise
presented in an earlier movie.
I've cleaned it up a bit, but now
I'm going to set some constants.
I'll insert two define directives to set the
number of rows and columns in the grid.
Now I'll change the value 10 used as an immediate
value in the loops to reflect the constants.
Save the changes, build and run.
The program still outputs 100 random numbers in 10 rows
and 10 columns, but now I can easily
change that to say 20 row of seven columns.
All I have to do to make that change
is to change the constants.
I don't have to hunt through the
code to find those proper values.
That's how constants can save you time.
Save, build and run.
And now you see seven columns, twenty rows.
Another example of using constants can
be seen by opening exercise constant2.
This code is an update to an exercise from an earlier movie.
The number of characters to be input is set by the
INPUT_MAX constant defined at line three.
The constant is then used to set the buffer size
at line seven and again with the fgets function at line 10.
The advantage here is that you can change both values
at once simply by modifying the INPUT_MAX constant.
You don't have to hunt through the code.
Load up exercise placeholder1.
This is a modification to an earlier
exercise shown in another movie.
Build and run the code to help you
remember what the problem was.
I'll type nine and press enter.
Wow and there's the ugly output.
You can fix this problem by adjusting
the placeholders in the printf statement.
Modify each %f placeholder at line 10 by inserting
.1 inside each one so that they read %.1f.
Save the changes, build and run.
The .1 format limits floating point output to
only one digit after the decimal point.
Edit again and type .3 for the
format in the printf statement.
Save, build and run.
And now three digits appear after the decimal point.
Return to exercise constant1.
In the printf statement at line 17,
you see a two between the % and d.
What the 2 does is set an output width
to two characters minimum, so when single digits
appear in the output they're right aligned.
Build and run this code.
See how single digits are lined up in the output?
Now edit the code again and remove the two.
Save, build and run.
Now the single digits don't look right.
No one expects them to line up in the tens column.
The placeholders have lots of options.
These can be sandwiched between the % sign and the letter.
I could make dozens of movies to describe them all,
but another one to look at is %s
found in the placeholder2 source code.
The two strings right and left are displayed
by using %s placeholders, but there are extra
characters between the % and s.
The 20 specifies an output width,
just like the two did in %d.
In line eight the %20s right justifies the output
adjusting the string's location based
on its length inside that width.
In line nine the negative sign before
the 20 left justifies the string,
which is how text is normally output.
Build and run this code to see how it works.
The width argument in the %s placeholder
sets the width to 20 characters.
Right justified, left justified.
This placeholder is ideal for displaying lists and tables.
Many more placeholder width, precision, padding,
and other options are available.
The documentation for the printf function lists
the variety plus a few examples.