54

When terminating a string, it seems to me that logically char c=0 is equivalent to char c='\0', since the "null" (ASCII 0) byte is 0, but usually people tend to do '\0' instead. Is this purely out of preference or should it be a better "practice"?

What is the preferred choice?


K&R says: "The character constant '\0' represents the character with value zero, the null character. '\0' is often written instead of 0 to emphasize the character nature of some expression, but the numeric value is just 0.

3
  • 1
    Hey Joe DF! Reading now K&R and had same question. Googled "\0 in c string" and second link is to your question. It helped me ;) Commented Jul 24, 2016 at 19:03
  • 1
    @vasili111 I'm glad it helped you. :) Commented Sep 21, 2016 at 5:15
  • The difference between 0 and '\0' is that the former is in decimal and the latter is in octal. ;-) Commented Nov 25 at 0:20

4 Answers 4

48

From Control code table (ASCII):

Binary   Oct  Dec  Hex  Abbr  Unicode  Control char  C Escape code  Name
0000000  000  0    00   NUL   ␀        ^@            \0             Null character

There isn’t any difference, but the more idiomatic one is '\0'.

Putting it down as char c = 0; could mean that you intend to use it as a number (e.g., a counter). '\0' is unambiguous.

Sign up to request clarification or add additional context in comments.

4 Comments

Does the C standard guarantee ASCII?
@CiroSantilli六四事件法轮功纳米比亚胡海峰 No, I'm looking at the C99 standard and there's a few footnotes that mention ASCII with respect to implementing trigraphs and language implementations in ASCII and that's it. It is something that's implementation defined (bear in mind character sets such as IBM's EBCDIC). But I think you'd struggle to find a modern C implementation that doesn't rely on the ASCII character set. There's some useful information relating to this here.
Yes, that's about what my read of the C99 gave as well. Thanks for that question, hadn't found it before.
Especilally, in C/C++, 0 or '\0' also used to terminate the string literal, ex: "abc\0"+"def" will be "abc". This is a place where '\0' is more visually
20

'\0' is just an ASCII character. It is the same as 'A', or '0' or '\n'.
If you write char c = '\0', it's the same as char c = 0;. If you write char c = 'A', it's the same as char c = 65.

It's just a character representation and it's a good practice to write it, when you really mean the NULL byte of string. Since char is one byte in C (integral type), it doesn't have any special meaning.

3 Comments

or 0b1000001, or 0101, that's not important in my answer. It's all number.
The character set doesn't have to be ASCII compatible, so 'A' does not have to be the same as 65. (E.g. EBCDIC is not ASCII compatible.) I don't know any character set that uses a non-zero value for the NUL character though.
@PaulGroke Thanks for the comment! I wonder if there is a character set that uses a non-zero value for the null character. To note: 1) as Nobilis has mentioned: the C standard does not guarantee ASCII; 2) as Keith Thompson has mentioned (useful to know): the signedness of plain char is implementation-defined (source: stackoverflow.com/a/62682671/9881330).
6

The preferred choice is that which can give people reading your code an ability to understand how do you use your variable, as a number or as a character.

The best practice is to use 0 when you mean your variable as a number and to use '\0' when you mean your variable is a character.

Comments

0

The previous answers are already quite clear. I just share what I learned about this issue with a demo.

#include <stdlib.h>
#include <stdio.h>


char*
mystrcat(char *dest, char *src) {
    size_t i,j;
    for(i = 0; dest[i] != '\0'; i++)
        ;
    for(j = 0; src[j] != '\0'; j++)
        dest[i+j] = src[j];
    dest[i+j] = '\0';
    return dest;
}

int main() {
    char *str = malloc(20); // malloc allocates memory, but it doesn't initialize the memory
    // str[0] = '\0';
    str[0] = 0;
    for (int k = 0; k <10; k++) {
        char s[2];
        sprintf(s, "%d", k);
        mystrcat(str, s);
    }
    printf("debug:%s\n", str);
    return 0;
}

In the above program, I used malloc to initialize the pointer, but malloc doesn't initialize the memory. So after the mystrcat operation (which is nearly the same as the strcat function in glibc), the string may contain messy code (since the memory content is not initialized).

So I need to initialize the memory. In this case, str[0] = 0 and str[0] = 0 both can make it work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.