0

I know in C '\0' is always at the end of a string, but is '\0' always mark the end of a string?

Just like "1230123" can be recognized as "123"?

One edition of the question used the notation '/0' instead of '\0'.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Claire He
  • 45
  • 5
  • 3
    Its not '/0' but '\0'. – H.S. Jan 24 '19 at 02:15
  • 5
    `\0` is not the same character as `0`. – Dragonthoughts Jan 24 '19 at 02:49
  • String definition in [G4G](https://www.geeksforgeeks.org/strings-in-c-2/) : Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character `\0`. There is an extra terminating character which is the Null character `\0` used to indicate termination of string which differs strings from normal character arrays. – EsmaeelE Jan 24 '19 at 03:43

3 Answers3

2

A byte with the value 0 by definition marks the end of a string. So if you had something like this:

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

It would print abc.

This is different from "1230123" where the 4th character in the string is not the value 0 but the character '0', which has an ASCII code of 48.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Great answer, and i suggest to add [link](https://ascii.cl/) to [ASCII](https://en.wikipedia.org/wiki/ASCII) table that illustrate `\0` as ASCII code of NULL and 48 as ASCII code of `0`. – EsmaeelE Jan 24 '19 at 03:36
  • @EsmaeelE But, it is unlikely that when any user runs the program, the program will have the locale for the C standard library's string functions is set to ASCII. Programs that depend on characters having specific character codes (other than 0) are, um—to put it kindly—"overly specific". – Tom Blodget Jan 24 '19 at 08:16
2

The null terminating character is represented as \0 and not /0 and it always mark end of string because, in C, strings are actually one-dimensional array of characters terminated by a null character \0.

This

char s[] = "1230123";

is same as this

char s[] = {'1', '2', '3', '0', '1', '2', '3', '\0'};
                            |                    |
                    This is character '0'        |
                    whose decimal value is 48    |
                                                 |
                                     This is null terminating character
                                     whose decimal value is 0

Check this example:

#include <stdio.h>

int main (void)
{
    int x,y,z;
    char s1[] = "1230123";
    char s2[] = {'1','2','3','\0','4','5','6','\0'};
    printf ("%s\n", s1);
    printf ("%s\n", s2);
    return 0;
}

Output:

1230123
123      <======= The characters after null terminating character is not printed.
H.S.
  • 11,654
  • 2
  • 15
  • 32
2

A string literal can have a '\0' in the middle.

A string only has a '\0' at the end.

'\0' is the null character and has a value of 0. '0' is the character zero.
See value of '\0' is same ... 0?


C has, as part of the language, string literals.

The two string literals below have a size of 8: the 7 you see plus the 1 not explicitly coded trailing null character '\0'.

"abc-xyz"    // size 8
"abc\0xyz"   // size 8

C, as part of the standard library, defines a string.

A string is a contiguous sequence of characters terminated by and including the first null character.

Many str...() functions only work with the data up to the first null character.

strlen("abc-xyz")  --> 7
strlen("abc\0xyz") --> 3
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • 1
    I liked that you used string syntax for showing the use of '\0' in the middle of a string, rather than array of chars syntax, like the other answers. Somehow, using string syntax makes it more clear that '\0' has the same "citizen range" as any other escape sequences. – cesss Nov 03 '22 at 11:42