Use the ‘\0’ literal for the terminal null character

The fragment is taken from Notepad++ project. The error is detected by the following diagnostic: The error text: V528 It is odd that pointer to ‘char’ type is compared with the ‘\0’ value. Probably meant: *headerM != ‘\0’.

TCHAR headerM[headerSize] = TEXT("");
size_t Printer::doPrint(bool justDoIt)
  if (headerM != '\0')


Thanks to this code’s author, using the ‘\0’ literal to denote the terminal null character, we can easily spot and fix the error. The author did a good job, but not really.

Imagine this code were written in the following way:

if (headerM != 0)

The array address is verified against 0. The comparison doesn’t make sense as it always true. What’s that – an error or just a redundant check? It’s hard to say, especially if it is someone else’s code or code written a long time ago.

But since the programmer used the ‘\0’ literal in this code, we can assume that the programmer wanted to check the value of one character. Besides, we know that comparing the headerM pointer with NULL doesn’t make sense. All of that taken into account, we figure that the programmer wanted to find out if the string is empty or not but made a mistake when writing the check. To fix the code, we need to add a pointer dereferencing operation.

Correct code

TCHAR headerM[headerSize] = TEXT("");
size_t Printer::doPrint(bool justDoIt)
  if (*headerM != _T('\0'))


The number 0 may denote NULL, false, the null character ‘\0’, or simply the value 0. So please don’t be lazy – avoid using 0 for shorter notations in every single case. It only makes the code less comprehensible, and errors harder to find.


Use the following notations:

  • 0 – for integer zero;
  • nullptr – for null pointers in C++;
  • NULL – for null pointers in C;
  • ‘\0′, L’\0’, _T(‘\0’) – for the terminal null;
  • 0.0, 0.0f – for zero in expressions with floating-point types;
  • false, FALSE – for the value ‘false’.

Sticking to this rule will make your code clearer, and make it easier for you and other programmers to spot bugs during code reviews.

Written by Andrey Karpov.

This error was found with PVS-Studio static analysis tool.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.