- Strings. You may have noticed
that we discussed several types of data,
integers, flows, and so on,
but we haven't mentioned a type called strings,
even though we did use strings,
and they're very prominent in any programming language,
in particular, in C.
In C, strings are defined as a series of characters,
such as this particular one.
There is no string type per se in C.
Rather, strings are just stored
in arrays of type char,
like in the following definition.
We are defining the type of an array
to store, for example, 30 positions.
Each one of those positions is a string.
The more formal definition is still
not entirely covering the topic of arrays officially,
but you can refer to them for now
as a sequential collection of elements of similar type.
In our case, that type is character.
So let's look at an example of a string in C,
which has 32 characters.
As you can see, I am labeling those starting from zero.
It's very important to remember that
each time we deal with an array, and with strings,
we start with the first character being labeled as zero,
all the way to, in this case, to 32.
Let's see how this is actually implemented
in a C program.
If you notice that this definition,
the character on line 17,
the character type of the address 30
is being assigned a string,
can be anything of any length.
However, notice that the number of characters in the string
is larger than 30.
We will see if there is an impact of that.
So I am allocating 30.
I am instructing the compiler to allocate
a sequential collection of characters,
which I am labeling address,
and I am storing something starting at position zero
that's labeled address.
Then in a second example, I'm defining
another string called name, which has
fewer than the number of charactres
I explicitly instructed the compiler to allocate.
Let's run this code and see what happens.
As you can see, there are two warning messages.
One message is on line 17, and it says that
string for char array is too long.
So this is a warning.
It's not necessarily an error.
Remember, C assumes you know what you are doing,
so it doesn't prevent the program from running.
It doesn't do anything with the screen at this point.
Nothing in the processing of the strings is affected.
The second one still has a warning.
However, this is a different type of warning.
It just tells us that we declared that string,
and we haven't used it.
So this string is perfectly fine
to allocate more than you need,
and often that's the case.
This one gives you two errors,
both for not using the string variable,
and for allocating it less than what it needs.
So far, we have seen a simple example
on how to use strings.
In the subsequent videos, we are now going to
get deeper into string manipulation.