Michael
Michael

Reputation: 6899

C String Null Zero?

I have a basic C programming question, here is the situation. If I am creating a character array and if I wanted to treat that array as a string using the %s conversion code do I have to include a null zero. Example:

char name[6] = {'a','b','c','d','e','f'};
printf("%s",name);

The console output for this is:

abcdef

Notice that there is not a null zero as the last element in the array, yet I am still printing this as a string.

I am new to programming...So I am reading a beginners C book, which states that since I am not using a null zero in the last element I cannot treat it as a string.

This is the same output as above, although I include the null zero.

char name[7] = {'a','b','c','d','e','f','\0'};
printf("%s",name);

Upvotes: 3

Views: 349

Answers (5)

EvilTeach
EvilTeach

Reputation: 28837

Yes. You do. There are a few other ways to do it.

This form of initialization, puts the NUL character in for you automatically.

char name[7] = "abcdef";
printf("%s",name);

Note that I added 1 to the array size, to make space for that NUL.

One can also get away with omitting the size, and letting the compiler figure it out.

char name[] = "abcdef";
printf("%s",name);

Another method is to specify it with a pointer to a char.

char *name = "abcdef";
printf("%s",name);

Upvotes: 0

Graphics Noob
Graphics Noob

Reputation: 10050

What most likely happened is that there happened to be the value of 0 at memory location name + 6. This is not defined behavior though, you could get different output on a different system or with a different compiler.

Upvotes: 0

Matteo Italia
Matteo Italia

Reputation: 126777

You're just being lucky; probably after the end of that array, on the stack, there's a zero, so printf stops reading just after the last character. If your program is very short and that zone of stack is still "unexplored" - i.e. the stack hasn't grown yet up to that point - it's very easy that it's zero, since generally modern OSes give initially zeroed pages to the applications.

More formally: by not having explicitly the NUL terminator, you're going in the land of undefined behavior, which means that anything can happen; such anything may also be that your program works fine, but it's just luck - and it's the worst type of bug, since, if it generally works fine, it's difficult to spot.

TL;DR version: don't do that. Stick to what is guaranteed to work and you won't introduce sneaky bugs in your application.

Upvotes: 5

salezica
salezica

Reputation: 76899

You must include the null character at the end.

It worked without error because of luck, and luck alone. Try this:

char name[6] = {'a','b','c','d','e','f'};
printf("%s",name);
printf("%d",name[6]);

You'll most probably see that you can read that memory, and that there's a zero in it. But it's sheer luck.

Upvotes: 2

AnT stands with Russia
AnT stands with Russia

Reputation: 320371

The output of your fist printf is not predictable specifically because you failed to include the terminating zero character. If it appears to work in your experiment, it is only because by a random chance the next byte in memory happened to be zero and worked as a zero terminator. The chances of this happening depend greatly on where you declare your name array (it is not clear form your example). For a static array the chances might be pretty high, while for a local (automatic) array you'll run into various garbage being printed pretty often.

Upvotes: 3

Related Questions