36

gcc 4.4.4 c89

What is the standard way to null terminate a string? When I use the NULL I get a warning message.

*dest++ = 0; 
*dest++ = '\0'; 
*dest++ = NULL; /* Warning: Assignment takes integer from pointer without a cast */

Source code:

size_t s_strscpy(char *dest, const char *src, const size_t len)
{
    /* Copy the contents from src to dest */
    size_t i = 0;
    for(i = 0; i < len; i++)
    *dest++ = *src++;

    /* Null terminate dest */
     *dest++ = 0; 

    return i;
}

Another question: I deliberately commented out the line that null terminates. However, it still correctly printed out the contents of the dest. The caller of this function would send the length of the string by either included the NULL or not. i.e. strlen(src) + 1 or stlen(src).

size_t s_strscpy(char *dest, const char *src, const size_t len)
{
    /* Copy the contents from src to dest */
    size_t i = 0;
    /* Don't copy the null terminator */
    for(i = 0; i < len - 1; i++)
    *dest++ = *src++;

    /* Don't add the Null terminator */
    /* *dest++ = 0; */

    return i;
}
Brian
  • 14,610
  • 7
  • 35
  • 43
ant2009
  • 27,094
  • 154
  • 411
  • 609
  • 13
    I prefer `*dest++ = '\0'; ` since `'\0'` has the correct type in this context. – Paul R May 26 '10 at 08:05
  • 5
    @Paul R: Why don't you make this an answer? – Lucas May 26 '10 at 08:07
  • `*dest++ = 0;` why post-increment the pointer after assigning the deferenced value to 0? – Larry Jun 05 '15 at 01:13
  • 1
    Detail: In C, a character array may have a null character making the array a _string_. A _string_ always has a null character, else it is not a string. So a pedantic rewrite would be "What is the standard way to null terminate a character array making it a string?". – chux - Reinstate Monica Sep 21 '15 at 21:05

4 Answers4

42

To your first question: I would go with Paul R's comment and terminate with '\0'. But the value 0 itself works also fine. A matter of taste. But don't use the MACRO NULLwhich is meant for pointers.

To your second question: If your string is not terminated with\0, it might still print the expected output because following your string is a non-printable character in your memory. This is a really nasty bug though, since it might blow up when you might not expect it. Always terminate a string with '\0'.

Lucas
  • 13,679
  • 13
  • 62
  • 94
20

From the comp.lang.c FAQ: http://c-faq.com/null/varieties.html

In essence: NULL (the preprocessor macro for the null pointer) is not the same as NUL (the null character).

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 12
    +1; it really bugs me all the time i see "NULL-terminated strings": heck, the string terminator is not NULL, it's NUL! – Matteo Italia May 26 '10 at 08:54
18

Be very careful: NULL is a macro used mainly for pointers. The standard way of terminating a string is:

char *buffer;
...
buffer[end_position] = '\0';

This (below) works also but it is not a big difference between assigning an integer value to a int/short/long array and assigning a character value. This is why the first version is preferred and personally I like it better.

buffer[end_position] = 0; 
INS
  • 10,594
  • 7
  • 58
  • 89
7

'\0' is the way to go. It's a character, which is what's wanted in a string and has the null value.

When we say null terminated string in C/C++, it really means 'zero terminated string'. The NULL macro isn't intended for use in terminating strings.

Scott Langham
  • 58,735
  • 39
  • 131
  • 204
  • `'\0'`, if it is a character, is a pretty big one!. I'm sure that on most machines today, with a `C` compiler, you have sizeof('\0') == 4. (Maybe 8031 micro-controllers are an exception?). – Joseph Quinsey May 26 '10 at 18:02
  • @Joseph. I'm not quite sure where you're coming from. sizeof('\0') comes out as 1 byte on my compiler. For a double byte character, sizeof(L'\0') it's 2. – Scott Langham May 27 '10 at 08:14
  • 6
    I double-checked: in a .c file it is 4, while in a .cpp file, or with g++, it is 1. The question was tagged as C, not C++. – Joseph Quinsey May 27 '10 at 14:30
  • 2
    @Scott In C, `\0` in an `int`. Try running on a **C** compiler: `printf("%zu\n", sizeof ('\0'));` Certainly your result will be more than 1. – chux - Reinstate Monica Sep 21 '15 at 21:00