26

Possible Duplicate:
What does the \0 symbol mean in a C string?

I am new at iPhone Development. I want to know, what does '\0' means in C, and what is the equivalent for that in objective c.

2
  • 1
    \O or \0 O or Zero? \0 means null ASCII value is 0 Jan 22, 2013 at 15:10
  • 1
    6 million results on google, with just \0
    – Eregrith
    Jan 22, 2013 at 15:21

7 Answers 7

29

The null character '\0' (also null terminator), abbreviated NUL, is a control character with the value zero. Its the same in C and objective C

The character has much more significance in C and it serves as a reserved character used to signify the end of a string,often called a null-terminated string

The length of a C string (an array containing the characters and terminated with a '\0' character) is found by searching for the (first) NUL byte.

11

In C, \0 denotes a character with value zero. The following are identical:

char a = 0;
char b = '\0';

The utility of this escape sequence is greater inside string literals, which are arrays of characters:

char arr[] = "abc\0def\0ghi\0";

(Note that this array has two zero characters at the end, since string literals include a hidden, implicit terminal zero.)

6

The '\0' inside character literals and string literals stands for the character with the code zero. The meaning in C and in Objective C is identical.

To illustrate, you can use \0 in an array initializer to construct an array equivalent to a null-terminated string:

char str1[] = "Hello";
char str2[] = {'H', 'e', 'l', 'l', 'o', '\0'};

In general, you can use \ooo to represent an ASCII character in octal notation, where os stand for up to three octal digits.

3
  • ... or '\xXY' for hexadecimal. Not sure how that is a "generalization" of '\0' though, they're different concepts.
    – unwind
    Jan 22, 2013 at 15:20
  • @unwind \0 is zero in octal notation. Microsoft's page on escape characters does not have a specific entry on \0, "bundling" it together with other octal character literals. Jan 22, 2013 at 15:23
  • 1
    @unwind: note also that 0 is an octal integer constant, and is a special case of the fact that in general one can write 0[0-7]* for an octal integer constant. Not that it makes any difference whether 0 is formally defined to be an octal vs decimal constant, but as it happens the grammar classifies it as octal, as it does the \0 escape :-) Jan 22, 2013 at 15:46
6

To the C language, '\0' means exactly the same thing as the integer constant 0 (same value zero, same type int).

To someone reading the code, writing '\0' suggests that you're planning to use this particular zero as a character.

3

\0 is zero character. In C it is mostly used to indicate the termination of a character string. Of course it is a regular character and may be used as such but this is rarely the case.

The simpler versions of the built-in string manipulation functions in C require that your string is null-terminated(or ends with \0).

4
  • to add it in a string (in Objective C) what should i do... Jan 22, 2013 at 15:11
  • @SaurabhJadhav it seems the usage in Objective-C is similar to the one in C. Also have a look here Jan 22, 2013 at 15:13
  • i have looked at the link you gave it to me but i want it another way for eg. in objective c NSString * str = @"A\0B\0C"; so i want the output as followed A B C..ie i want escape that \0 from the string.. Jan 23, 2013 at 12:25
  • If you want such output, you need to replace '\0' with a space character. I believe you can do this using a single iteration(I am not sure if there is a built-in function in Objective-C to do that) Jan 23, 2013 at 12:44
0

In C \0 is a character literal constant store into an int data type that represent the character with value of 0.

Since Objective-C is a strict superset of C this constant is retained.

4
  • C does not specify the number of bits in a char.
    – Kerrek SB
    Jan 22, 2013 at 15:13
  • Yup, you're right. Fixing the anwer
    – Jack
    Jan 22, 2013 at 15:14
  • @KerrekSB What about CHAR_BIT from <limits.h>? But how's that relevant? Did I miss an edit to this answer? Jan 22, 2013 at 15:17
  • CHAR_BIT tells you what that particular compiler implements. The C standard doesn't say much about what this value is - it could be 8, 9, 11 or 32 in CHAR_BIT. Jan 22, 2013 at 15:18
-4

It means '\0' is a NULL character in C, don't know about Objective-C but its probably the same.

3
  • 5
    NULL is a pointer, \0 is a character.
    – Kerrek SB
    Jan 22, 2013 at 15:10
  • 1
    @KerrekSB NULL can be defined as simply 0 (explicitly allowed by C99), in which case there won't be difference from '\0'. Jan 22, 2013 at 15:18
  • 2
    One-l nul terminates a string, two-l null stands for no thing.
    – jscs
    Jan 22, 2013 at 19:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.