Hi i always mix up a NULL and '\0' and 0.
Can you write me when to use these things?

Dani AI

Generated

gave a solid, practical outline. A few focused clarifications and common pitfalls that tend to show up in real code:

C-style strings are terminated by the NUL character; that termination is the role of the character literal '\0'. Zeroing a byte buffer (for example with memset(buf, 0, n)) produces bytes equal to '\0', which is why it works for C strings. Character literals have subtle type differences across languages (in C a character literal is an int, in C++ it is a char), but that rarely changes how '\0' is used as a terminator.

Pointers and integer zero interact specially in C. The integer constant 0 is defined by the language as a null pointer constant too, so assigning 0 into a pointer makes it a null pointer. The macro NULL exists for readability and is provided by standard headers; its exact definition can vary between implementations. In modern C++ code, prefer nullptr (C++11+) to avoid overload ambiguities and clearer intent.

Short examples and gotchas:

char s[] = "hi";
/* s[2] == '\0' — string terminator */
char *p = NULL;    /* or 0 in C; prefer nullptr in C++ */
if (!p) { /* pointer is null */ }
  • Passing a null pointer to string-format functions (for example printf("%s", ptr)) is undefined and commonly crashes; test pointers before using them where a valid address is required.
  • In C++, f(0) can pick the integer overload; f(nullptr) removes ambiguity.
  • Use '\0' when working at the character/string level to make intent explicit; use pointer checks (if (ptr) / if (!ptr)) or NULL/nullptr to express pointer state; use plain 0 for numeric arithmetic.

These points bridge the short answer given earlier to the practical issues that produce bugs in the wild.

Recommended Answers

All 2 Replies

>i always mix up a NULL and '\0' and 0.
The short answer is that you use NULL with pointers, '\0' with characters, and 0 with everything else.

The long answer is that '\0' and 0 are identical. In C they're both integers with the value 0. You can use them interchangeably, but it's best to only use '\0' in a character context, because it's a character literal. You can use 0 at any time because it's so multi-functional and that fact is well known. You're less likely to confuse people.

NULL is not interchangeable with '\0' and 0 because it may be cast into a pointer type[1]:

#define NULL ((void*)0)

Therefore you can only safely use NULL in a pointer context because if it casts the expression to void*, you'll get mysterious warnings and errors when using NULL in a non-pointer context.

[1] In other words, the standard doesn't require this. It says that "an integer constant expression with the value 0, or such an expression cast to type void*, is a null pointer constant". So you can use 0 instead of NULL in a pointer context as well. 0 always works. NULL may also be defined just as 0, but you can't be sure of it.

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.