Why does a char array needs to be NULL-terminated? Why does not an int array for example needs to have a delimiter at the end?
-
5what would you use as a delimiter for an int array? – congusbongus Nov 25 '13 at 04:45
-
4char array is like any other array, need not be null terminated. – Naveen Nov 25 '13 at 04:46
-
@congusbongus : this is actually a good rhetoric question. I'll extend the implied consequences for beginners : using `0` makes sense regarding the ASCII encoding. Because it is the code for `null char`. Another type can be used to encode strings. For example `wchar_t`, which will follow the same coding convention. `int` will rarely be used to hold strings, therefore what value can be used as a `null` ? Generally none. Especially not `0` which maybe something you want to store in your array as normal content. – v.oddou Nov 25 '13 at 04:56
-
2`NULL` is (a macro that expands to) a null *pointer* constant. The null character `'\0'` that terminates a C string is not the same thing. – Keith Thompson Nov 25 '13 at 05:08
6 Answers
char[]
doesn't have to be NUL terminated. It's a convention used when you want to use char arrays as strings. You can use a char[] for your own purposes without any terminator.

- 28,809
- 4
- 51
- 69
-
`character strings` to be precise. And this convention is regarded by stdlib/string.h, functions of the standard that work on strings. For practicability a lot of people working with strings adopted that convention. Also the C standard itself null terminates string literals. – v.oddou Nov 25 '13 at 04:48
-
2@v.oddou: *strings* to be precise. The term is defined in 7.1.1 of the C standard: "A *string* is a contiguous sequence of characters terminated by and including the first null character." – Keith Thompson Nov 25 '13 at 05:10
-
To me, the question is really "why do we need a null character at the end of the char-array to say where the end is? Is that solving a problem that does not exist i.e. 'i dont know how to find the end of an array'... " I assume the answer is "you can use 'sizeof' on static arrays, but not dynamic arrays, so ending in NULL character is a convention that is taken advantage of in numerous string functions..." - See http://stackoverflow.com/questions/15929869/count-the-number-of-elements-in-an-array-in-c (or it's duplicates) – Simon Feb 12 '15 at 02:34
-
@simon You've missed the point. You don't need an end marker on a char array. You only need it if you want to use the C convention of using a char array as a string. Why? dunno - ask the guys who wrote C back in the days memory was measured in Ks, hard disks were measured in Megs (if you were lucky enough to have one) – John3136 Feb 12 '15 at 05:45
-
I think I'm on point. I didn't say you need it, I said it was convention: I gather because numerous string functions rely on that null character that it's a convention often adhered to. But yes, agree, it doesn't have to be adhered to. – Simon Feb 12 '15 at 22:53
-
@john Sorry miss-read your comment slightly, I guess I was thinking the term "char array" and "string" were interchangeable but "string" has a specific meaning... you might genuinely need an array of individual characters, whereas a string is assumed to be used contiguously, and as such has benefit to end in a null character... or something like that? – Simon Feb 12 '15 at 23:00
-
@Simon I think you're about right. As Keith Thompson says (above) the term "string" is defined in the C standard and has specific meaning. Not all char arrays are strings, and I'll let the language lawyers argue over whether all strings are char arrays (I say they are, but with a constraint of NUL temination)... – John3136 Feb 12 '15 at 23:26
It's a matter of convenience.
The ISO C standard, section 7.1.1, defines a string this way:
A string is a contiguous sequence of characters terminated by and including the first null character.
There are a number of possible ways to represent character strings, such as using a count and an array, or a count and a pointer. Null termination happens to be the way chosen for C, for string literals and the standard library functions that deal with strings.
It's convenient because the null character is not really used for anything else. It's not printable, it's not a control character with some defined display behavior like moving the cursor in some particular way.
You can have arrays of just about anything, but the convention of using a zero value to mark the end of a sequence happens not to be as convenient for other types. For integer or floating-point types, zero is a valid value that you might want to have as normal data in an array.
Pointers do have a distinguished value that can be used to mark the end of a sequence: the null pointer NULL
. And in fact it's sometimes used that way. A C program's command-line arguments are passed as a sequence of pointers to strings; the length of that sequence is indicated by the value of argc
and marked by a terminating null pointer. See also the environ
pointer and the exec*()
functions on Unix-like systems.
(But for some applications, a null pointer can also be a valid value, so it can't be used as a terminator.)
Character string manipulation is a fairly big part of what the C language and library are about, so it makes sense to have a convention for how to represent character strings. The convention does not apply as neatly to arrays of other types.
(Incidentally, it's important remember that NULL
is a macro that expands to a null pointer constant. It's incorrect to use the name NULL
to refer to the null character '\0'
. Both, depending on the context, can be represented in C source as the constant 0
, but they're quite different things.)

- 254,901
- 44
- 429
- 631
You don't need C
char[]
to be null terminated but string
. Because in C
a string
means a null terminated array of characters and \0
null terminator this tells the library that where the string ends.
Why?
Because initially C
as being a basic language having low abstraction doesn't have string
as a datatype a string
in C
is just a collection of char
.
To make it more clear, technically is NO data type called string
, its just a facade that is implemented for programmers accessibility making it a high level concept which is often implemented using the basic data type of char
.

- 3,795
- 2
- 19
- 22
-
1No, as string is not a pointer to a `char[]`; it's a sequence of characters. You can have a pointer to a string; the pointer is not itself a string. – Keith Thompson Nov 25 '13 at 05:11
-
Thank you for pointing, corrected. read it in a book, don't exactly remember currently, would point out once i find it again. But if its not than when i refer to a string *a char array* how does it knows where the first element of the array lies? – SajjadHashmi Nov 25 '13 at 05:18
-
-
`string str ="How do i know?"` .. `printf(str);` how does `str` knows where `H` i.e. the starting of `char[]` lies? – SajjadHashmi Nov 25 '13 at 05:39
-
2There is no type `string` in C. If you mean `char *str = "How do I know?"`, then `str` points to the `'H'`. If you mean `char str[] = "How do I know?"`, then `str` is an array; the name `str`, since it's an expression of array type, is implicitly converted in most contexts to a pointer to the array's first element -- in this case, the `'H'`. Take a look at the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Nov 25 '13 at 05:42
-
so thats what i wrote that `technically there is NO data type called string,` and a `string` is a pointer to `char`? like `str` points to `H` ? – SajjadHashmi Nov 25 '13 at 05:48
-
You're right that there's no data type called `string` (unless you define it yourself). But a *string* is a sequence of characters; it is *not* a pointer. You can have a pointer *to* a string; the pointer is not itself a string. – Keith Thompson Nov 25 '13 at 05:53
I used NULL
-terminated int
, char*
, struct
, etc. arrays a lot of times and I saw same solutions in others code, too. It is a quite common phenomenon :)
But: there is no language directive for the NULL
-terminated strings, at least not in the language core, although some standard C lib uses it (predominantly in <string.h>
)

- 13,359
- 7
- 71
- 99

- 11,875
- 18
- 85
- 108
In C, the concept of a string is represented as an array-of-char
that is NUL-terminated. Consequently, not every char
array is NUL-terminated; you could have such an array that does not represent a string. You could have a char
array that has embedded NUL bytes that do not represent terminators (because the array contains arbitrary binary data and does not represent a string).
You do not generally see terminators (sentinel values) for other array types because in general, what would value would you use? The sentinel value needs to be one that would not used for other elements of the array. You do see sentinel values for some special cases; for example, it's sometimes reasonable to use NULL
as a sentinel value for arrays of pointers (e.g. the argv
argument to main()
).
As for why C strings are NUL-terminated at all (as opposed to Pascal-style strings), that's a design the language authors chose. There are tradeoffs to either approach. Also see What's the rationale for null terminated strings?
In int array , the array elements will never be used together as a string. However, this is the case of char array. By terminating a character array with null, you will be able to use it as a string.

- 5,072
- 6
- 47
- 93