_DEBUG vs NDEBUG

c, c++, debugging

Solution

Visual Studio defines `_DEBUG` when you specify the `/MTd` or `/MDd` option, `NDEBUG` disables standard-C assertions. Use them when appropriate, ie `_DEBUG` if you want your debugging code to be consistent with the MS CRT debugging techniques and `NDEBUG` if you want to be consistent with `assert()`.

If you define your own debugging macros (and you don't hack the compiler or C runtime), avoid starting names with an underscore, as these are reserved.

Problem

Which preprocessor define should be used to specify debug sections of code? Use `#ifdef _DEBUG` or `#ifndef NDEBUG` or is there a better way to do it, e.g. `#define MY_DEBUG`? I think `_DEBUG` is Visual Studio specific, is NDEBUG standard?

Original source