How do you create a debug only function that takes a variable argument list? Like printf()

I still do it the old way, by defining a macro (XTRACE, below) which correlates to either a no-op or a function call with a variable argument list. Internally, call vsnprintf so you can keep the printf syntax: #include <stdio.h> void XTrace0(LPCTSTR lpszText) { ::OutputDebugString(lpszText); } void XTrace(LPCTSTR lpszFormat, …) { va_list args; va_start(args, lpszFormat); … Read more

Common array length macro for C? [duplicate]

Here’s a better C version (from Google’s Chromium project): #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) It improves on the array[0] or *array version by using 0[array], which is equivalent to array[0] on plain arrays, but will fail to compile if array happens to be a C++ type that overloads operator[](). The division causes a … Read more

C++ #include semantics

After reading all answers, as well as compiler documentation, I decided I would follow the following standard. For all files, be them project headers or external headers, always use the pattern: #include <namespace/header.hpp> The namespace being at least one directory deep, to avoid collision. Of course, this means that the project directory where the project … Read more

What is the use of the #error directive in C?

It’s a preprocessor directive that is used (for example) when you expect one of several possible -D symbols to be defined, but none is. #if defined(BUILD_TYPE_NORMAL) # define DEBUG(x) do {;} while (0) /* paranoid-style null code */ #elif defined(BUILD_TYPE_DEBUG) # define DEBUG(x) _debug_trace x /* e.g. DEBUG((_debug_trace args)) */ #else # error “Please specify … Read more

Can macros be overloaded by number of arguments?

(Edit: See the end for a ready-made solution.) To get an overloaded macro, first we need a macro which selects between several implementations. This part doesn’t use a variadic macro. Then a variadic macro which generically counts its arguments produces a selector. Plugging the argument count into a dispatcher produces an overloaded macro. Caveat: This … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)