# Sprintf formats ok with debugLoop but not with debug

**URL:** <https://forum.arduino.cc/t/sprintf-formats-ok-with-debugloop-but-not-with-debug/1205566>\
**Category:** Programming\
**Created:** [December 31, 2023, 10:09am UTC](https://forum.arduino.cc/t/sprintf-formats-ok-with-debugloop-but-not-with-debug/1205566 "2023-12-31T10:09:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bionicbone](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@bionicbone](https://forum.arduino.cc/u/bionicbone)\
**Post date:** [December 31, 2023, 10:09am UTC](https://forum.arduino.cc/t/sprintf-formats-ok-with-debugloop-but-not-with-debug/1205566/1 "2023-12-31T10:09:59Z")

</div>

I like to use this so I can quickly stop all the Serial.Print lines outputting by uncommenting the top line and commenting the second line, for ESP32 like this and it works

```arduino
//#define debug(fmt, ...)
#define debug(fmt, ...) Serial.printf("%s: " fmt "\r\n", __func__ , ## __VA_ARGS__ ) // ESP

```

For the nano, I seem to have to change this slightly because Serial.printf is not supported, so I have to use sprintf first...

The question is, why does debugLoop correctly apply the number 95 to the output, but debug does not.

What am I messing ?

```arduino
char buffer[60];

//#define debugLoop(fmt, ...)
#define debugLoop(fmt, ...) sprintf(buffer, "%s: " fmt "\r\n", __func__ , ## __VA_ARGS__ ); Serial.print(buffer); // Nano

//#define debug(fmt, ...)
#define debug(fmt, ...) sprintf(buffer, fmt "\r\n"); Serial.print(buffer); // Nano

void setup() {
  Serial.begin(115200);
}

void loop() {
  debugLoop("my number is %d", 95); // Outputs loop: my number is 95
  debug("my number is %d", 95); // Outputs my number is 459

  while (true);
}

```

Output:

```arduino
loop: my number is 95
my number is 459

```

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [December 31, 2023, 10:21am UTC](https://forum.arduino.cc/t/sprintf-formats-ok-with-debugloop-but-not-with-debug/1205566/2 "2023-12-31T10:21:27Z")

</div>

> [@bionicbone](#):
>
> why does debugLoop correctly apply the number 99

You mean 95?

> [@bionicbone](#):
>
> but debug does not.

My guess it that it's because you don't use `## __VA_ARGS__ ` in debug. So no parameter is given to sprintf() to correspond to the "%d", so it picks up some random memory bytes.

---

<div class="post-metadata">

**Author:** ![bionicbone](https://avatars.discourse-cdn.com/v4/letter/b/9fc29f/32.png) [@bionicbone](https://forum.arduino.cc/u/bionicbone)\
**Post date:** [December 31, 2023, 10:26am UTC](https://forum.arduino.cc/t/sprintf-formats-ok-with-debugloop-but-not-with-debug/1205566/3 "2023-12-31T10:26:00Z")

</div>

Perfect, thanks for that

---

<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:** [June 28, 2024, 10:26am UTC](https://forum.arduino.cc/t/sprintf-formats-ok-with-debugloop-but-not-with-debug/1205566/4 "2024-06-28T10:26:26Z")

</div>

Closed on June 28, 2024, 10:26am UTC

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