Why C Strings End with \0 – Null Terminated Strings Explained Simply

Why C Strings End with \0 – Null Terminated Strings Explained Simply

Ever wondered why C strings are just character arrays that magically know where they end? It’s all because of the null terminator – that sneaky \0 at the end.

In this video we break it down super simply:

  • What a C string actually is (spoiler: just bytes)
  • Why old-school assembly needed separate length labels
  • How adding a single zero byte fixed everything
  • Quick look at ASCII codes for “Hello”
  • Why \0 is the perfect terminator (and how to write it in code)
  • How loops know when to stop reading the string
  • Real example in assembly with and without null termination

Perfect if you’re learning C, diving into low-level programming, or just curious about what’s really happening when you write “hello” in C.

Introduction to Null Terminators 00:00:00
What is a C String? 00:00:11
Problems with Explicit Length in Assembly 00:01:06
Idea of a Special Terminator Symbol 00:01:24
Introduction to ASCII 00:02:20
Why ASCII is Outdated – Move to UTF 00:03:59
Example – “Hello” as ASCII Bytes 00:06:32
Mapping “Hello” to ASCII Codes 00:07:00
How Strings are Stored in Memory 00:08:46
The Null Terminator (\0) Explained 00:09:39
Null is ASCII Code 0 (NUL) 00:10:02
Escape Sequence \0 in Code 00:10:49
How Loops Use Null Terminator to Stop 00:11:55
Null-Terminated Strings in Assembly 00:12:41
Summary of C Strings 00:13:23
Outro and Call to Action 00:13:32

Thanks for watching!

Find us on other social media here:

  • https://www.NeuralLantern.com/social

Please help support us!

  • Subscribing + Sharing on Social Media
  • Leaving a comment or suggestion
  • Subscribing to our Blog
  • Watching the main “pinned” video of this channel for offers and extras

hey there let’s quickly talk about null terminators in c strings

so first off what’s a c string it’s basically just a character array it’s just an array of numbers

and the numbers will be interpreted as characters and what’s a null terminator it’s pretty easy it’s

just a a way to terminate the string so that a program can automatically determine where the

string ends in assembly a lot of times we’ll have you know a string like we’ll

say oh gosh I don’t want to write this out right now we’ll say you know my

string and we’ll define it as an array of bytes and then we’ll say you know

hello or something right and then later in order to tell the assembly program

how long the string is we’ll use an extra symbol this is not really an

My string length is going to be equal to, this is the ASM assembly by the way in case

you’re wondering, some kind of special symbol, my string length.

And then now we have defined the actual string and then the length of the string.

But this is kind of annoying, right?

We have to define an extra thing to say how long the string is.

And then we have to pass two different things that are kind of the same name later on when

we want to print a string.

But that’s not really the best way to do it.

that’s not really the best way to do it what if instead we just put a special symbol I don’t know

what should I put right now put a special zero I’ll just put like a question mark and we’ll say

that the question mark is a special symbol and then when we want to have part of our program

print the string we can just only give it the string and it can scan the string and as soon

as it sees that special symbol let’s say the question mark for now it’ll know when to stop

processing it or whatever we could allow the program to scan in advance to compute the the

length and maybe store it somewhere so we can keep using it we can allow the program to just

continuously print out characters maybe they’re buffered or something or processing characters

and then just stop when we get to the question mark but the point is it’s a little bit less work

for us to have a special symbol at the very end okay so a spoiler the symbol is going to be a zero

Now let’s look at a special table that is called ASCII.

Well, it’s not, the table’s not called ASCII,

but this is sort of like a scheme that was invented a long time ago.

I forget what this stands for.

It’s like American standard, maybe character something, something.

It’s basically for American purposes.

A long time ago, we decided, you know what?

I’d like to represent characters using one byte per character.

bits or you know a value that can go between 0 and 255 and we’ll just start assigning these numbers

to different characters so you can see here on this ascii table it’s kind of like an old thing

let me turn on my little annotator you can see on this table well the letter h is mapped

to the number 72. so this is this was arbitrary at the time now it’s a standard because everybody

accepts uh you know ascii uh in certain parts of your programs it’s kind of a little out of date

of a little out of date now because you know in the modern era it’s a lot smarter for your programs

to support a world of different characters you know for all different languages than only to

support characters that are common in english and even here there’s probably some stuff missing that

english users would probably want to add like certain accents and symbols and things like that

so you know the the letter o lowercase o it’s 111 uh the letter o uppercase is 79 so we’ve got you

so we’ve got you know a basic idea of mapping so for the purposes of this example i’m just

gonna assume we’re using ascii to make things easier okay so that’s an ascii table um

oh i guess i should say that the modern scheme that we usually use is called utf let me just uh

write that down on a notepad right now so we’ve got utf right now it’s just a different standard

but the same idea you know with ASCII we only had one byte with UTF we have a wide variety of

choices we can do UTF-8 which basically also means one byte it closely map maps to ASCII I

think all or almost all the characters will map to ASCII and then we have UTF-16 which means two

bytes or 16 bits and then we have UTF-32 which is basically four bytes or 32 bits as far as I

while. UTF-32 should be able to handle all the characters that all the languages of the world

commonly use. Just to double check, if you take two to the something power where the power, let’s

say y, two to the y, where y is the number of bits in question, then you can get an idea for the

number of combinations that you can represent. So two to the 32nd power, that’s about four billion.

I should have known that off the top of my head. That’s about four billion possible combinations.

So I don’t know.

I think in my opinion, probably 32 is enough for all symbols in the world, for all languages

in the world.

I’m not sure.

If not, then we would probably want to use UTF-64 if that exists, which definitely would

cover all symbols in the world.

But that’s really not the point of this video.

I just wanted to mention it because if you’re writing an application on your own, you want

to start your own business or you’re, you know, you’re working for someone, you probably

almost certainly do not want to use ASCII.

do not want to use ascii you probably want your database and your program to be able to use utf

16 or 32 because you want to be able to support international users and that’s not going to work

i had a website a long time ago that i wrote the database was in uh it was like latin 8 or

some scheme in the database where it was basically ascii just you know eight bits per character and i

could type characters in their native language and they were kind of disappointed and i lost a

lot of business until i realized oh i should upgrade my database and my program to support

utf but anyway the point of this video is not international character support the point of this

video is what is a null terminator and how do you use it in a c string so i’m going to get back on

Hello. Okay, so we got hello. This would basically be an array of characters or an array of in ASCII

one byte characters. So I’ll put like a number here and a number here and there’ll be five numbers.

What numbers are they? Well, if we’re using ASCII, then we should map these

using the ASCII table. So again, in the ASCII table here, what is capital H?

the number 72 and then the lowercase e that’s going to be the number 101 so i’ll just maybe do

like an arrow here the number 101 and then the capital h is going to be the number 72 and then

we got two l’s lowercase so that’s going to be this right here and that’s going to be the number

108 and then lowercase o that’s going to be the number 111 so basically you know you just go

through the characters you want to represent and map them using the ascii table or hopefully

table or hopefully an actual lookup table inside your program and then you can do the same thing

backwards you know what is what does 72 mean it means h so i’m going to erase this for now

and i’m going to go back to my little notepad here and i’m just going to fill out the numbers

so the first is going to be 72 oh actually before i fill these out hang on let me just

show you that this is basically what we’re trying to translate here so we got like an uppercase h

In many programming languages, definitely C++, which is the one I’m thinking of, we would use double quotes for an actual long string, like a proper string, a literal string, or like an STD string or something.

And we would use an array of single quoted characters to say this is one character that maps to one or more bytes, in this case, one byte.

So I’m going to say H-E-L-L-O and put them all in single quotes.

And then we’ll just sort of like translate the numbers here.

translate the numbers here. So we said the H was 72 because it was capital. It was like a lower

number. And then the E is going to be 101. And then the L is 108. We’ll do two of those 108.

And then the O was a 111. So you can imagine somewhere in an array of data in the computer,

there’s the number 72, 101, 108, you know, whatever. And even further thinking about it,

about it a little more deeply the number 72 is not actually in the machine it’s really a sequence of

ones and zeros right so just imagine that inside the machine there’s just a ton of ones and zeros

for this particular sequence you know five times eight which would be the number of uh you know

eight is like the number of bits per byte since we’re doing one byte characters um what is that

40 let me just double check here i want to make sure i’m saying things that are somewhat correct

that are somewhat correct we’ll do eight times five whoops i think i screwed something up

eight times five 40 yeah okay so you can imagine 40 ones and zeros just inside the machine

anyway so we talked about before not wanting to specify the length of a string so we’ll just

place a special marker instead of a question mark we’ll use the number zero so i’m going to put a

I forgot if I actually pointed this out or not. I’ll just do this to be sure.

Somewhere on this, you can see that the number zero in decimal, or zero in hexa, or zero in octal,

you’re welcome, you know how to do those other systems, is just considered null. So it’s sort

of a control code, or it’s not necessarily a symbol, it’s more of a control code. We’ll say

the number 72 that means h capital h but if we see the number zero that means null and in the

scheme that we’re going to use we will say that null terminates the string that’s why we call it

a null terminator so null terminators are basically just zeros that terminate the string

i could put null here inside of the the character representation just to kind of show you

other languages and we want to do single quoted characters we can usually use an escape sequence

which is just basically slash zero this is not a video about escape sequences but imagine that

every time there is a slash a forward slash and then you know one character after that then the

character will be interpreted zero for example if we just put zero by itself then that actually would

be the ascii code for zero or just the character code for zero in this particular case zero is uh

where the heck is the zero you can’t even find it oh there it is right there that’s like so the

the character zero is represented by the number 48 but that’s not what we actually want we want

the actual number zero because we’re trying to get a null terminator so anyway we couldn’t just put

a regular zero in there because it would be interpreted as 48 instead we put a slash which

tells the computer that the thing that follows is a control character so that will be interpreted as

interpreted as actually a zero or null terminator.

So now that we know how to put a null terminator in an array of characters and we know what

it’s going to look like if we’re just talking about numbers, we now know that if you have

a program that has let’s say a for loop and it’s kind of like going through, you know,

do all the characters, if it’s going through every single character one by one and doing

something to them, printing them, modifying, adding them to an algorithm, whatever.

this for loop or actually this should probably be a while loop since for loops

are kind of limited and the while loop will know when to break because as soon as it sees a

character that is actually just the number zero it’ll say oh i found the null terminator i will

now ignore this and break the loop i won’t do anything with this character because the null

is a terminator it’s not part of the string um what else you know actually i think that’s pretty

much it if you’re in assembly uh i’m just going to go back to yasm assembly for a second because i

like i like to talk about that lately and imagine we have like a string called my string and we say

it’s a sequence of bytes and i say hello you know this is how you would do this in x84 60 x 84 64

yasm assembly on ubuntu or just linux then uh you know this string right here is just a string with

null terminate with no null terminator you want to add a null terminator to the end of it so that

null terminator to the end of it so that you can write a better function that would scan your string

and print it without knowing in advance how long it was then you just do a comma zero at the end

and now you can easily write a function to scan or read or process your strings because this is

really what’s what’s happening under the hood it’s just like a sequence of characters and then it

ends with the zero and there you go that’s the basics of c strings with null terminators

this video i hope you had fun and learned a little bit of stuff i’ll see you in the next video

hey everybody thanks for watching this video again from the bottom of my heart i really

appreciate it i do hope you did learn something and have some fun if you could do me a please

a small little favor could you please subscribe and follow this channel or these videos or whatever

it is you do on the current social media website that you’re looking at right now

looking at right now, it would really mean the world to me and it’ll help make more videos

and grow this community. So we’ll be able to do more videos, longer videos, better videos,

or just I’ll be able to keep making videos in general. So please do me a kindness and subscribe.

You know, sometimes I’m sleeping in the middle of the night and I just wake up because I know

somebody subscribed or followed. It just wakes me up and I get filled with joy. That’s exactly what

happens every single time. So you could do it as a nice favor to me or you could troll me if you

You could troll me if you want to just wake me up in the middle of the night, just subscribe.

And then I’ll just wake up.

I promise that’s what will happen.

Also, if you look at the middle of the screen right now, you should see a QR code, which

you can scan in order to go to the website, which I think is also named somewhere at the

bottom of this video.

And it’ll take you to my main website where you can just kind of like see all the videos

I published and the services and tutorials and things that I offer and all that good

stuff.

If you have a suggestion for clarifications or errata or just future videos that you want to see,

please leave a comment.

Or if you just want to say, hey, what’s up?

What’s going on?

You know, just send me a comment.

Whatever.

I also wake up for those in the middle of the night.

I wake up in a cold sweat and I’m like,

It would really mean the world to me.

I would really appreciate it.

So again, thank you so much for watching this video.

And enjoy the cool music as I fade into the darkness.

as I fade into the darkness which is coming for us all.

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply