Ordo
Ordo

Reputation: 705

The terminating NULL in an array in C

I have a simple question.
Why is it necessary to consider the terminating null in an array of chars (or simply a string) and not in an array of integers. So when i want a string to hold 20 characters i need to declare char string[21];. When i want to declare an array of integers holding 5 digits then int digits[5]; is enough. What is the reason for this?

Upvotes: 3

Views: 5900

Answers (9)

tperk
tperk

Reputation: 91

Actually - you don't have to NUL-terminate your strings if you don't want to! The only problem is you have to re-write all the string libraries because they depend on them. It's just a matter of doing it the way the library expects if you want to use their functionality.

Just like I have to bring home your daughter at midnight if I wish to date her - just an agreement with the library (or in this case, the father).

Upvotes: 0

Adam Maras
Adam Maras

Reputation: 26883

Null terminators are required at the end of strings (or character arrays) because:

  1. Most standard library string functions expect the null character to be there. It's put there in lieu of passing an explicit string length (though some functions require that instead.)
  2. By design, the NUL character (ASCII 0x00) is used to designate the end of strings. Hence why it's also used as an EOF character when reading from ASCII files or streams.

Technically, if you're doing your own string manipulation with your own coded functions, you don't need a null terminator; you just need to keep track of how long the string is. But, if you use just about anything standardized, it will expect it.

Upvotes: 4

Ben Zotto
Ben Zotto

Reputation: 71068

It's not about declaring an array that's one-bigger, it's really about how we choose to define strings in C.

C strings by convention are considered to be a series of characters terminated by a final NUL character, as you know. This is baked into the language in the form of interpreting "string literals", and is adopted by all the standard library functions like strcpy and printf and etc. Everyone agrees that this is how we'll do strings in C, and that character is there to tell those functions where the string stops.

Looking at your question the other way around, the reason you don't do something similar in your arrays of integers is because you have some other way of knowing how long the array is-- either you pass around a length with it, or it has some assumed size. Strings could work this way in C, or have some other structure to them, but they don't -- the guys at Bell Labs decided that "strings" would be a standard array of characters, but would always have the terminating NUL so you'd know where it ended. (This was a good tradeoff at that time.)

Upvotes: 3

Jacob Relkin
Jacob Relkin

Reputation: 163318

The purpose of null termination in strings is so that the parser knows when to stop iterating through the array of characters.

So, when you use printf with the %s format character, it's essentially doing this:

int i = 0;
while(input[i] != '\0') {
   output(input[i]);
   i++;
}

This concept is commonly known as a sentinel.

Upvotes: 3

rerun
rerun

Reputation: 25505

The reason is it was a design choice of the original implementors. A null terminated string gives you a way to pass an array into a function and not pass the size. With an integer array you must always pass the size. Ints convention of the language nothing more you could rewrite every string function in c with out using a null terminator but you would allways have to keep track of your array size.

Upvotes: 3

EnabrenTane
EnabrenTane

Reputation: 7466

Because of the the technical reasons of how C Strings are implemented compared to other conventions

Upvotes: 1

DigitalRoss
DigitalRoss

Reputation: 146261

It is only by convention that C strings end in the ascii nul character. (That's actually something different than NULL.)

If you like, you can begin your strings with a nul byte, or randomly include nul bytes in the middle of strings. You will then need your own library.

So the answer is: all arrays must allocate space for all of their elements. Your "20 character string" is simply a 21-character string, including the nul byte.

Upvotes: 3

wkl
wkl

Reputation: 80041

You don't have to terminate a char array with NULL if you don't want to, but when using them to represent a string, then you need to do it because C uses null-terminated strings to represent its strings. When you use functions that operate on strings (like strlen for string-length or using printf to output a string), then those functions will read through the data until a NULL is encountered. If one isn't present, then you would likely run into buffer overflow or similar access violation/segmentation fault problems.

In short: that's how C represents string data.

Upvotes: 7

Reinderien
Reinderien

Reputation: 15328

It's not absolutely necessary to have the character array be 21 elements. It's only necessary if you follow the (nearly always assumed) convention that the twenty characters be followed by a null terminator. There is usually no such convention for a terminator in integer and other arrays.

Upvotes: 2

Related Questions