Hello, and welcome back.
We are in a new section.
In this section, we're going to talk about character strings.
You've probably heard me say the word string quite a bit
throughout the class, and you may have been a little confused.
A string is really just a word.
It's something enclosed in double quotes.
We're going to talk about how we can store strings in character arrays.
So up until this point, we're pretty familiar with a single
character, the char data type or the "care" data type, however you want to pronounce it. We know what that is but we
haven't really learned about words.
To assign a single character to a char variable, the character
is enclosed within a pair of single quotation marks.
We're familiar with this.
We've learned all about
this. In this example plus sign equals plus inside single quotes.
The important thing there is the single quotes.
You've also learned that there is a distinction made between
single quotes and double quotes.
So if you put plus sign variable and you assign it something in double
quotes and plus sign is of type char, that's not going to work,
because you can only assign single characters to the char data type.
A string constant or string literal is a sequence of characters or symbols
between a pair of double quotes.
So now double quotes are going to come a new play.
Anything between these double quotes is interpreted by
the compiler as a string.
And so really it can be alphanumeric characters, it can
be special characters but they usually make up words you can
kind of think of it as text.
You can have spaces.
But anything in double quotes is a string literal.
Every time you have displayed a message using the printf
function, we have defined the message as a string constant.
Remember printf, the first argument we put some text inside there and
we have some format specifiers.
That is a string consonant.
You also hear the term string literal it's the same thing.
So we say print f this is a string everything in between that those
double quotes is a string constant and we can put it on one or two lines.
You also notice that you can have print statements that have
escape sequences backslash n. Now this third one's um pretty cool
which, I'll explain in a second.
But the main point is you have to understand the difference
between single quotes and double quotes and what's inside there.
With the double quotes, you have strings.
With the single quotes, you have characters.
There are two types of constants.
For this third example, this printf where we have the four and then we
have a backslash then a single quote.
If you want to actually display a single quote as
output, you have to escape it.
And you can escape special characters by using the backslash.
So in the third example, it's actually going to print out
four double quote you write, and then another double quote.
So the compiler interprets an explicit double quote without preceding
backslash as a string delimiter.
Every other time you use a double quote it's beginning and ending of
a string but what if you wanted to display it then you have to just
put the backslash in front of it.
And it will tell the compiler hey this is not actually ending the string.
It's something that we want to display or something we
want to use as that character.
Also you have to use the escape sequence if you want to do the
backslash as well because the backslash is an escape sequence.
So when you see the backslash backslash, you're actually displaying
that backslash in a printf function.
If you see it anywhere else, you're assigning the backslash to
a variable or however it's used.
But a backslash itself followed by another backslash will
escape the backslash string.
And again, a backslash and a string always signals the
start of an escape sequence.
So one by itself is not going to display.
These are special characters.
So when you're in double quotes, you may see the backslashes
to escape special characters and even the backslash itself.
Here's how string is in memory.
A string in memory is each single character is
represented by a bit here.
And so if you have a string called this is a string, you're
going to see it where the space has something in memory.
And also you should notice at the end that backslash zero
that's the null terminator which we're going to talk about.
But for the second string, again, you see everything that's in memory.
Even the backslash n which represents a new line is in memory.
And then for you right, you'll notice the quote is in memory.
But the backslash isn't.
Because the backslash is delimiting the quote.
It's escaping the quote.
So this is how it looks in memory.
You should notice that the string in memory always has at least
one more byte for that backslash zero, which is the null character.
Also looks like the diagram on the bottom is incorrect.
You'll notice the "you" is not shown here so after the quotation double quote,
you don't see the you there. The you should be there somewhere and it kind of applies
it's there somewhere they are really just showing it these are the ASCII
values underneath and so then it's just showing-not showing it, but
you would be right before the right.
And that's what I want to talk about is this null character.
It's a special character with the code value zero.
It's added to the end of each string so that the compiler knows where
it ends and also when you run it.
This character is known as the null character, and you
don't just put a zero in it you can put a backslash zero.
And that's what was in this previous slide at the very end.
And it could be anywhere actually in memory.
And depending on where it is in memory, a function is going to
go and look until it sees that backslash zero and it may display
it or may manipulate it if you're using different string functions.
It's very important that null character.
A string is always terminated by a null character.
So the length of a string is always one greater than the number
of characters in the string.
This is going to become very important when we when we use character arrays
and we specify the size of the array the array size is always going
to have to be one more than how many characters you want to store
because of this null character.
We also have a concept of null that we haven't really talked
about in too much detail.
It usually comes into play when we talk about pointers,
but do not confuse the null character with the null keyword.
So there's a null keyword in one of the include headers that you can use.
And this is a symbol that represents a memory address
that doesn't reference anything.
So you use it when you when you use pointers.
A null character is just a string terminator.
We'll talk more about null when we talk about pointers but make sure you
understand the distinction between null character and the null keyword.
You can add a backslash zero character to the end of a string explicitly
and this will create two strings and I'll show you how to do this when we
talk about defining character arrays.
Here's an example with the null terminator.
We have a print f here, and we say the character and then the backslash
zero which terminates a string.
So what's going to happen is if you compile and run this program, the
output that you're going to get is the first line is going to be the
character and then the backslash o there's going to be a it's going to
end it and only the first part of the string is going to be displayed.
There's going to be no more output because print thinks
that it's the end of the string.
So it just prints out the character.
Output is going to end after those first two words because the
function stops outputting once it reaches the null character.
So the second backslash zero at the end of the string, so even though
you don't see a backslash zero here right before the ending double
quote, right after string dot, there's still one there, but you
don't ever it's never gets reached.
So there's an implicit null terminator at the end of the string and the
printf so it knows where to end.
And if you add another one, it's only going to display up to that first one.
So the first null terminator that's found in the character series
always marks the end of the string.
So understand these null characters.
This was just a quick overview to describe what a string
is and understand that strings are in double quotes.
And I also want to describe the null character.
In the next lecture, we'll understand what character strings
are in terms of how you can create these different this one type
of variable to store strings.
Thank you.