Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:46:23+00:00 2026-06-17T13:46:23+00:00

I just encountered a DEBUG macro in C that I really like #ifdef DEBUG_BUILD

  • 0

I just encountered a DEBUG macro in C that I really like

#ifdef DEBUG_BUILD
#  define DEBUG(x) fprintf(stderr, x)
#else
#  define DEBUG(x) do {} while (0)
#endif

I’m guessing a C++ analogue would be :-

#ifdef DEBUG_BUILD
#  define DEBUG(x) cerr << x
#else
#  define DEBUG(x) do {} while (0)
#endif
  1. Is the second code snippet analogous to the one in C?
  2. Do you have any favorite C++ debug macros?

EDIT :
By “Debug Macros” I mean “macros that might come in handy while running a program in debug mode”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:46:24+00:00Added an answer on June 17, 2026 at 1:46 pm

    Is the second code snippet analogous to the one in C?

    More or less. It’s is more powerful, as you can include <<-separated values in the argument, so with a single argument you get something that would require a variable number of macro arguments in C. On the other hand, there is a slim chance that people will abuse it by including a semicolon in the argument. Or even encounter mistakes due to a forgotten semicolon after the call. So I’d include this in a do block:

    #define DEBUG(x) do { std::cerr << x; } while (0)
    

    Do you have any favourite C++ debug macros?

    I like the one above and use it quite often. My no-op usually just reads

    #define DEBUG(x)
    

    which has the same effect for optimizing compilers. Although the comment by @Tony D below is correct: this can leave some syntax errors undetected.

    I sometimes include a run-time check as well, thus providing some form of a debug flag. As @Tony D reminded me, having an endl in there is often useful as well.

    #define DEBUG(x) do { \
      if (debugging_enabled) { std::cerr << x << std::endl; } \
    } while (0)
    

    Sometimes I also want to print the expression:

    #define DEBUG2(x) do { std::cerr << #x << ": " << x << std::endl; } while (0)
    

    In some macros, I like to include __FILE__, __LINE__ or __func__, but these are more often assertions and not simple debug macros.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just encountered a weird error which saying that find is not a member
I've just encountered a rather strange problem. A Winforms (although that may not be
I just encountered this article of an extention of css sprites that enables the
I just encountered the following behavior: for (var i = 0; i < 50;
Hi I am a C++ beginner just encountered a problem I don't know how
I am just starting working with jquery and have encountered the following problem <div
Just going through the sample Scala code on Scala website, but encountered an annoying
Hi I have just started learning WCF and I have encountered an error I
I think I may have encountered a bug in mysql, or is it just
I've just started using HTTParty, and i've encountered a problem in the way it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.