Hello, and welcome back. In this lecture, I want to talk about constant strings.
We know that string literals or a string constant is inside double quotes.
But what about the variable itself. How can you make sure that the variable
can never change that value?
This is referred to as a constant. So constant string
is sometimes necessary in your program
where you do not want your values of your variables to ever change.
So for example, if we're creating a circumference of some calculation,
we would do 3.14159 times diameter.
3.14159 is a constant. It never changes.
It represents pi.
So it would be really nice if instead of using that magic number, we could use
a variable and make sure that that variable never changes.
So there are good reasons
to use a symbolic constant instead of just typing in the number every time.
First off, a name tells you a lot more than a number does.
Owed equals 0.015 times house value
is much less meaningful than owed equals tax rate times value.
So we want to have variable names that represent
numbers that never change.
If you read through a long program,
the meaning of the second vision version is a lot plainer.
It's a lot easier to look at that code and understand that 0.015 represents the tax rate.
Suppose you have used a constant in several places
and it becomes necessary to change its value,
you would have to go and change that value everywhere in the program
as opposed to just changing in one spot where you define that constant.
I talked about this earlier when I use the term magic numbers.
That's what this is referred to.
Whenever you see a number used in a program it's not stored in a variable,
it's usually best to create that number and put it in a constant.
Because then if you use it multiple times
and that variable changes,
all you have to do is change it once. You don't have to change it everywhere you use it.
So avoid magic numbers, use constants.
One way you can define a constant in c is you can use to define
preprocessor directive.
So this preprocessor directive lets you define constants,
and I actually used it in a couple programs. I'm now trying to help explain it.
So if you wanted to do a tax rate and it was a constant,
you could say define with the pound sign before it.
We know it's a preprocessor directive because of the pound sign,
and then we would say the name and then the value.
When your program is compiled, the value 0.015
is going to be substituted everywhere you have used tax rate,
just like a variable but it's actually not a variable.
You cannot assign any other values to that.
If it was a variable, you'd be able to assign data to it,
but not in this case because it's a preprocessor.
So the preprocessor directive says you can't assign data
and you specify that with the define.
So notice the syntax for this. You have the pound symbol followed by defined keyword.
And then after that, you have your name.
We're not using any assignment operators.
We just have our name and then the space and then the value.
So to assign a value to that define, all you need is a space.
There's no semicolon at the end. It's not a statement. It's very specific.
So what's happening here is there are compile time substitution happening for
these preprocessors. The preprocessor runs before the compiler remember.
Preprocessor is going to run and it's going to see these defined carry roots. And then everywhere it sees
a tax rate, it's just going to put in 0.015 for the compiler.
Defined statements can appear anywhere in the program.
There's no such thing as a local define. Once you have a define,
anybody can use it.
Now best practice or convention is usually
to put the defined statements at the beginning of the program
or inside an include file if you want to
share in more than one source file but you don't have to do that.
Because it's running at the preprocessor,
it doesn't have any scope, it doesn't have any local or global.
You can just put it anywhere. And anywhere in the program you can see it, even if it's
being used before it's defined.
Defined statements are also really good because they help make your programs more portable.
Sometimes you can use a defined statement and relate it to a particular computer you're running on
when the program is running, and it will get that very value.
The defined statement can also be used for characters and string constants, not just for numbers
so. If we wanted to do something like this, perfectly valid.
You just enclose the single characters inside a single quote.
And if you want to actually put in an entire string
literal, you just put it in double quotes. And that's what this oops is doing.
Another way you can create constants and see is a more recent
mechanism. C90 added basically another way which is
providing the constant modifier.
You can use the constant keyword to convert a declaration for a variable into a declaration for a constant.
And this will allow you never to be able to change that variable. So all you do is you say const
and then the name the data type and then the name of the variable.
And what this does in this example is going to now make months a symbolic constant for 12.
We're not calling it a variable because a variable can change,
that's why it's called variable.
You have constants or variables
and constant in months is not a variable, does not change.
Constant makes months into a read-only value.
You can display months and use it in calculations. You cannot alter the value of months.
So constants are important if you do not want data to change,
and also they can replace magic numbers.
Const is a newer approach and is more flexible than using define.
It lets you declare a type.
It also has better control
over which parts of a program can use the constant.
So you should be using constant whenever you can,
but you'll see a lot of programs using defined because as it was the first mechanism.
Also, there's a third way you can create a constant,
and that's using the enum. Remember, we talked about enums.
Enums are a way to create your own type of variable,
and only have certain data associated with it. You can't change the value of enums.
Once you define an enum, it has to have that those values.
It can have different values, but it still
is sort of a way to create a symbolic constant, not complete way, but a sort of a way.
Also, with const, you can initialize a character array and declare it as a constant,
that's a good way of handling standard messages.
So if you say const char message equals the end of the world is nigh,
that will declare the message as a constant and it's protected from ever being modified explicitly
within your program. Any attempt to try to modify message by doing a str copy
will result in an error message from the compiler.
And now you can use this for a standard message that may be
displayed as output in various parts of the program.
The technique for defining standard messages is particularly useful
if they are used in many places, like I mentioned.
Prevents accidental modification of such constants in other parts of the program,
just like magic numbers.
So constants you want to use them in your program,
two ways to employ them essentially,
using the define and using the constant keyword.
Please pay attention to this.
You might see it in some upcoming examples.
Thank you.