# Using #define to include Serial.printf() or not

**URL:** <https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168>\
**Category:** Programming\
**Created:** [October 5, 2024, 5:27am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168 "2024-10-05T05:27:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@dr\_phil](https://forum.arduino.cc/u/dr_phil)\
**Post date:** [October 5, 2024, 5:27am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/1 "2024-10-05T05:27:03Z")

</div>

I'm using Ralph Bacons YouTube video that explains how to incorporate `Serial.printf()` or not.  
This works very well and I've increased the number of `#define` required in order to select the correct `Serial.printf()`.

The code is as follows:

```cpp
#define DEBUG1 0 // if 0 then DEBUG(n) does not display, if 1 then it does
#define DEBUG2 0
#define DEBUG3 0
#define DEBUG4 0 

#if DEBUG1 == 1
#define debug1(a) Serial.printf(a)
#else
#define debug1(a) 
#endif

#if DEBUG2 == 1
#define debug2(a,b) Serial.printf(a,b)
#else
#define debug2(a,b) 
#endif

#if DEBUG3 == 1
#define debug3(a,b,c) Serial.printf(a,b,c)
#else 
#define debug3(a,b,c) 
#endif

#if DEBUG4 == 1
#define debug4(a,b,c,d) Serial.printf(a,b,c,d)
#else 
#define debug4(a,b,c,d) 
#endif

```

so in the code you use `debug1()`, `debug2()`, `debug3()` and `debug(4)`.  
Whilst this works fine I was thinking if there was a way to determine which `debug(n)` was required so you could just use `debug()` and the `#` code would be simpler.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [October 5, 2024, 5:48am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/2 "2024-10-05T05:48:22Z")

</div>

You could use a macro with variadic arguments that replaces all your variants

```arduino
#define FOO(...) printf( __VA_ARGS__ )

```

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [October 5, 2024, 5:51am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/3 "2024-10-05T05:51:45Z")

</div>

```arduino
#define DEBUG 1 // or 2 or 4 etc.

#ifdef DEBUG == 1
// do debug 1 stuff
#endif

#ifdef DEBUG == 2
// do debug 2 stuff
#endif

```

-or-  
you can use more complex logic to test  
for debug modes e.g.

```arduino
#ifdef (DEBUG && 3) == 3
// do both 1 and stuff
#endif

```

There are a lot of ways to do this  
using binary or other logical constructs.

---

<div class="post-metadata">

**Author:** ![dr\_phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@dr\_phil](https://forum.arduino.cc/u/dr_phil)\
**Post date:** [October 5, 2024, 7:14am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/4 "2024-10-05T07:14:24Z")

</div>

Thank you for your prompt reply. To test your suggestion I've written this test code:

```cpp
#define DEBUG 1 // if 0 then debug does not display

#if DEBUG == 1
#define debug(...) Serial.printf( __VA_ARGS__ )
#else
#define debug(...) 
#endif

String test = "hello";

void setup() {
  Serial.begin(115200);
  while (!Serial);
     ;
  debug("this is a one test \n");
  debug("this is a two test %s \n", test );
  debug("three things %d %d \n", 1, 2);
  debug("four things %d %d \n", 3, 4, 5);
}

```

That works except with 4 variables it only prints 3. So I increased the number of dots from  
2 to 4

```cpp
#define debug(....) Serial.printf( __VA_ARGS__ )

```

but now I get a compile error

```plaintext
Compilation error: expected ')' after "..."

```

Would appreciate your comment please.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [October 5, 2024, 7:19am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/5 "2024-10-05T07:19:34Z")

</div>

> [@dr\_phil](#):
>
> That works except with 4 variables it only prints 3.

Do you mean that it not print "5" in the line below?

> [@dr\_phil](#):
>
> `debug("four things %d %d \n", 3, 4, 5);`

Do you know the syntax of the printf operator? It prints as many variables as the number of "%xx" placeholders you specified.  
So you need to add another %d in your line that way:

`debug("four things %d %d %d\n", 3, 4, 5);`

---

<div class="post-metadata">

**Author:** ![dr\_phil](https://avatars.discourse-cdn.com/v4/letter/d/c2a13f/32.png) [@dr\_phil](https://forum.arduino.cc/u/dr_phil)\
**Post date:** [October 5, 2024, 7:22am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/6 "2024-10-05T07:22:45Z")

</div>

Sorry, my error. Fixed it

```cpp
debug("four things %d %d %d \n", 3, 4, 5); 

```

So just use 3 dots and now works just fine.

Many thanks!

---

<div class="post-metadata">

**Author:** ![christoph1](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christoph1/32/1126611_2.png) [@christoph1](https://forum.arduino.cc/u/christoph1)\
**Post date:** [March 3, 2025, 1:20am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/7 "2025-03-03T01:20:00Z")

</div>

Thanks! I'm summarizing the good solution:

```arduino
#define DEBUG_SWITCH true // Switch on or off the debugging logs
#if DEBUG_SWITCH == true
    #define DEBUG_printf(...) Serial.printf( __VA_ARGS__ )
#else
    #define DEBUG_printf(...)
#endif
// #include <LibPrintf.h> // If your Arduino IDE does not know "printf", seek library "arduino-printf" at GitHub

while (!Serial) { } // Wait until the serial bus to your Laptop is ready for transmission. Some types of microprocessor require this delay.
DEBUG_printf(F("\n[SETUP] This is the log of sketch '" __FILE__"', compilled on " __DATE__" " __TIME__"\n"));

DEBUG_printf("[LOOP] I send integer numbers (e.g. %d), float numbers (%3.2f), hex numbers (0x%X) and strings (%s) to the serial port.\n", 1, 2.2, 3*3*3, "Four");

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 30, 2025, 1:20am UTC](https://forum.arduino.cc/t/using-define-to-include-serial-printf-or-not/1308168/8 "2025-08-30T01:20:32Z")

</div>

Closed on August 30, 2025, 1:20am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
