Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is '\0' in C++?

I'm trying to translate a huge project from C++ to Delphi and I'm finalizing the translation. One of the things I left is the '\0' monster.

if (*asmcmd=='\0' || *asmcmd==';')

where asmcmd is char*.

I know that \0 marks the end of array type in C++, but I need to know it as a byte. Is it 0?

In other words, would the code below be the equivalent of the C++ line?

if(asmcmd^=0) or (asmcmd^=';') then ...

where asmcmd is PAnsiChar.

You need not know Delphi to answer my question, but tell me \0 as byte. That would work also. :)

like image 911
qwerty101 Avatar asked Aug 06 '10 12:08

qwerty101


People also ask

What is the use of '\ 0 in C?

\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 ).

What is the '\ 0 character in C?

'\0' is defined to be a null character. It is a character with all bits set to zero. This has nothing to do with pointers. '\0' is (like all character literals) an integer constant with the value zero.

What is the difference between 0 and '\ 0 in C?

'0' is the ASCII numeral zero. It has value 0x30. '\0' is the binary number 0. Both are type int, but have completely different values.

What is the '\ 0 used for at the end of a string?

\0 is used to mark end of character string in C. Most C std library functions requires the string to be terminated this way in order to work. Since C does not know how long is your string you must mark the end with a \0 so it knows it has reached the end of your string.


3 Answers

'\0' equals 0. It's a relic from C, which doesn't have any string type at all and uses char arrays instead. The null character is used to mark the end of a string; not a very wise decision in retrospect - most other string implementations use a dedicated counter variable somewhere, which makes finding the end of a string O(1) instead of C's O(n).

*asmcmd=='\0' is just a convoluted way of checking length(asmcmd) == 0 or asmcmd.is_empty() in a hypothetical language.

like image 112
tdammers Avatar answered Oct 03 '22 12:10

tdammers


Strictly it is an escape sequence for the character with the octal value zero (which is of course also zero in any base).

Although you can use any number prefixed with zero to specify an octal character code (for example '\040' is a space character in ASCII encoding) you would seldom ever have cause to do so. '\0' is idiomatic for specifying a NUL character (because you cannot type such a character from the keyboard or display it in your editor).

You could equally specify '\x0', which is a NUL character expressed in hexadecimal.

The NUL character is used in C and C++ to terminate a string stored in a character array. This representation is used for literal string constants and by convention for strings that are manipulated by the<cstring>/<string.h>library. In C++ the std::string class can be used instead.

Note that in C++ a character constant such as '\0' or 'a' has type char. In C, for perhaps obscure reasons, it has type int.

like image 33
Clifford Avatar answered Oct 03 '22 10:10

Clifford


That is the char for null or char value 0. It is used at the end of the string.

like image 32
Daniel A. White Avatar answered Oct 03 '22 10:10

Daniel A. White