# Simple Macro program I think

**URL:** <https://forum.arduino.cc/t/simple-macro-program-i-think/1376678>\
**Category:** Programming\
**Created:** [April 26, 2025, 12:41am UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678 "2025-04-26T00:41:32Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [May 1, 2025, 1:37pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/22 "2025-05-01T13:37:11Z")

</div>

> [@frank2644](#):
>
> How do I start? When I pasted the template into my code I got a lot of complier errors starting with:  
> "error: variable or field 'printMany' declared void"

Post the complete code your tried and the complete error message verbatim.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [May 1, 2025, 1:55pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/23 "2025-05-01T13:55:10Z")

</div>

> [@gfvalvo](#):
>
> Post the complete code your tried and the complete error message verbatim.

You should do this each and every time. Even then if you only changed a **single** character.  
You should post your complete sketch for two reasons:

1. eliminating any room for interpretation **where** you modified the code
2. for convenience of all other users. Because when code is posted as a code-section you can move the mouse to the upper-right corner of the code-section and copy the complete sketch into the clipboard with a **single** mouseclick.

Code-sections have a limited height. So you can even post code with 3000 lines of code without using much space on the screen.

The template is what its name says: a template that shows the compiler what you want to do

So writing down this part of the code is essential to make it work  
taken from user @kenb4

```arduino
template <typename T, typename ... Remainder>
void printMany(T first, Remainder ... remainder) {
  Serial.print(first);
  Serial.print(' '); // comment out if desired
  printMany(remainder...);
}

```

So all in all you need to add **all** these lines to your existing code

the definition of the function printMany() **and** the definition of the template  
template \<typename T, typename ... Remainder\>.....

```arduino
void printMany() {
  Serial.println(); // blank line after other arguments (if any)
}
template <typename T, typename ... Remainder>
void printMany(T first, Remainder ... remainder) {
  Serial.print(first);
  Serial.print(' '); // comment out if desired
  printMany(remainder...);
}

```

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [May 1, 2025, 1:59pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/24 "2025-05-01T13:59:34Z")

</div>

> [@gfvalvo](#):
>
> ```arduino
> void printMany() {
> }
> template <typename T, typename ... Remainder>
> void printMany(T first, Remainder ... remainder) {
> Serial.println(first);
> printMany(remainder...);
> }
> 
> void setup() {
> Serial.begin(115200);
> delay(1000);
> 
> float f {3.14};
> uint8_t u16 {10};
> int16_t i16 { -5001};
> const char str[] {"Hello World"};
> 
> printMany(f, u16, i16, str);
> }
> 
> void loop() {
> }
> 
> ```

This reminds me a bit of functional programming like LISP where you are operating recursively on a list which has a head (which they called CAR) and tail (CDR). The tail itself has a head and a (smaller) tail so the list is a naturally recursive construct. However, there it is done dynamically at runtime. Also most of it I have forgotten. With the C++ template construct it appears it is almost like a compile time text editing operation to rollout a copy for each recursive iteration.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [May 1, 2025, 2:45pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/25 "2025-05-01T14:45:21Z")

</div>

> [@frank2644](#):
>
> I feel bad that you folks have taken the time to help and I can't use it.

You need not. First, it's our time to spend or waste as the case may be. Second, you are not the only one reading this thread, and hoping to learn something every day we visit.

Learn or at least be exposed to.

> [@6v6gt](#):
>
> it is almost like a compile time text editing

Another thing it reminds me of is assembly language macro processors. When exploited by the wizards they can do all kinds of things… compute N!, play a decent game of tic-tac-toe with the user and so forth. Oh how they rolled back in the day. 😑

And it seems like the germ of an idea that landed up with modern templates.

a7

---

<div class="post-metadata">

**Author:** ![frank2644](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/frank2644/32/1126542_2.png) [@frank2644](https://forum.arduino.cc/u/frank2644)\
**Post date:** [May 1, 2025, 4:35pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/26 "2025-05-01T16:35:57Z")

</div>

Okay, here's the entire error msg followed by the sketch.

sketch\_may01a:5: error: variable or field 'printMany' declared void

void printMany(T first, Remainder ... remainder) {  
^  
sketch\_may01a:5: error: 'T' was not declared in this scope

sketch\_may01a:5: error: 'Remainder' was not declared in this scope

void printMany(T first, Remainder ... remainder) {  
^  
exit status 1  
variable or field 'printMany' declared void

```arduino
void printMany() {
  Serial.println(); // blank line after other arguments (if any)
}
template <typename T, typename ... Remainder>
void printMany(T first, Remainder ... remainder) {
  Serial.print(first);
  Serial.print(' '); // comment out if desired
  printMany(remainder...);
}

void setup() {
  Serial.begin(115200);
  delay(1000);

  float f {3.14};
  uint8_t u16 {10};
  int16_t i16 { -5001};
  const char str[] {"Hello World"};

  printMany(f, u16, i16, str);
  printMany(f, u16, i16);
  printMany(f, u16);
  printMany(f);
  printMany();
  Serial.println("done");
}
void loop() {
  // put your main code here, to run repeatedly:

}
```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [May 1, 2025, 4:39pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/27 "2025-05-01T16:39:27Z")

</div>

> [@frank2644](#):
>
> Okay, here's the entire error msg followed by the sketch.

That code compiles error-free for me. Are you sure it's the exact, complete code that caused the compilation error? Are there any other .ino, .h, or .cpp files in the project?

What Arduino board are you compiling for?

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [May 1, 2025, 4:53pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/28 "2025-05-01T16:53:21Z")

</div>

You should also include the complete compiler log.  
Here is a tutorial how to adjust the arduino IDE for maximum verbose compiler-log.  
This compiler log will answer these questions:

> [@gfvalvo](#):
>
> Are you sure it's the exact, complete code that caused the compilation error? Are there any other .ino, .h, or .cpp files in the project?
> 
> What Arduino board are you compiling for?

> [@If you get a compiler-error or an upload-error provide all details for analysing the error](https://forum.arduino.cc/t/if-you-get-a-compiler-error-or-an-upload-error-provide-all-details-for-analysing-the-error/971255/1):
>
> Hi newcomer, the Arduino-Forum community can be of great help. If the community is able to support you in a way that helps solving your problems depends on detailed information that you should provide. trying to be quick by posting a too short posting is just slowing down finding the solution. Because all that happends is that you are asked for the detailed information and have to provide it anyway. Only difference the answer and the solution comes later The most important thing is to post …

---

<div class="post-metadata">

**Author:** ![frank2644](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/frank2644/32/1126542_2.png) [@frank2644](https://forum.arduino.cc/u/frank2644)\
**Post date:** [May 1, 2025, 5:45pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/29 "2025-05-01T17:45:37Z")

</div>

gfvalvo

No other files in the project, a brand new sketch.  
The board is a wemos D1 mini (esp8266). I tried an Arduino Mega/Mega 2560 board with the same error results.

My IDE is 1.8.5.

The sketch is exactly as in my last msg, no changes. I tried again making sure to copy the sketch in your post #4 and got the same errors.

Thanks, Frank

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [May 1, 2025, 5:56pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/30 "2025-05-01T17:56:32Z")

</div>

> [@frank2644](#):
>
> My IDE is 1.8.5.

Uh wow. Pretty old. This version makes your computer vulnerable through the log4j-java security-gap

I highly recommend that you update to version 1.8.19

There is a possability to have a completely independant second Arduino IDE as a portable version

> [@Tutorial how to install a portable version of Arduino-IDE 1.8.19](https://forum.arduino.cc/t/tutorial-how-to-install-a-portable-version-of-arduino-ide-1-8-19/1205125/1):
>
> Install a portable version of the Arduino IDE that is completely independent of the other Arduino IDE: This is the download link [Arduino-IDE 1.8.19 als ZIP-File](https://downloads.arduino.cc/arduino-1.8.19-windows.zip) Extract the zip file into a folder of your choice. I'm now posting screenshots as the ZIP file [image] is stored into folder F:\IDE-PORTABLE [image] Unzip the zip-file into this folder [image] change to sub-directory [image] it should look like this in subfolder [image] create a sub-folder portable The name has …

For an Arduino Mega it should compile immidiately after installing  
You will have to install the ESP8266 package for ESP8266 support.

Anyway maybe the reason might be the super old Arduino IDE and or an old ESP8266 package.

What is so hard about following the compiler-log tutorial and then posting the complete compiler-log?

There is no way around posting this verbose compiler-log !

I have taken your code from post # 26 and pasted into a WOKWI-Demo that compiles just fine.

So without a much more detailed information about **YOUR** computers configuration which will be delivered by the verbose compiler-log  
we **can't** help you !

> **[template-Demo-001 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/429757194424069121)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

---

<div class="post-metadata">

**Author:** ![frank2644](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/frank2644/32/1126542_2.png) [@frank2644](https://forum.arduino.cc/u/frank2644)\
**Post date:** [May 1, 2025, 7:27pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/31 "2025-05-01T19:27:55Z")

</div>

StefanL38  
Thank you.  
My current IDE is the portable version and I agree it's pretty old. Back around 2020 I tried a later version and discovered that my sketches needed some changes to work. I decided then not to bother, but that was 4 years ago. I supose I should bite the bullet now and upgrade. I guess I can keep both versions and gradually migrate sketches as needed.

At the moment my disk space is full. I have been fighting a losing battle trying to clear space for the last week or so and I finally ordered a new drive today.

I do have my above Macro that satisfiers my needs for now and I want to continue working on that related sketch so It will be a while before I get back to this issue.

Incidentally I copied the code from my sketch into the WOKWI-Demo and like you said it worked perfectly, very slick sketch. I then tried to run it on an esp32 and the compile failed with lots of error messages. It may have been something I did as I have no clue how to configure that system. Or maybe that failure is indicative of my esp8266 related problem.

Anyway, I hear you about the verbose complier log. I'll do that in the future.

BTW, I would normally assume 1.8.19 is older than my 1.8.5 version.

Thanks Again to all,  
Frank

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [May 1, 2025, 7:35pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/32 "2025-05-01T19:35:20Z")

</div>

> [@frank2644](#):
>
> BTW, I would normally assume 1.8.19 is older than my 1.8.5 version.

which number is higher "5" or "19" ?

1.8.19 is the last version of Arduino IDE 1.X  
After 1.8.19 comes Arduino IDE 2.X  
I still use IDE 1.8.19 because IDE 2.X has so many bells and whistles that I don't need  
and in V2.X the serial monitor is crippled down to be almost unusable.

> [@frank2644](#):
>
> At the moment my disk space is full. I have been fighting a losing battle trying to clear space for the last week or so and I finally ordered a new drive today.

Sounds like you are using a pretty old computer.  
How about updating to a lenovo Thinkpad T480 or L580 with intel-processor generation 8?  
Not older because generation 7 and older has no support under Windows 11.  
There is software available to make Windows 11 look and feel like windows 7.

As a leasing-returner they are about $200.

---

<div class="post-metadata">

**Author:** ![frank2644](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/frank2644/32/1126542_2.png) [@frank2644](https://forum.arduino.cc/u/frank2644)\
**Post date:** [May 1, 2025, 8:48pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/33 "2025-05-01T20:48:46Z")

</div>

StefanL38  
Without much thought conventional math says .5 is higher then .19, my bad. I do know better.

Yes, my computer is a little old using W10 with an I5 processor, still okay for my needs.

I've been using refurnished $100-$200 computers for years, never been sorry.

BTW, I'm really a hardware guy who at age 35 designed and built my first computer in 1975 from discrete components around an RCA microprocessor. There wasn't much software back then and I had to write my own assembler. Started with 256 bytes of memory and a bunch of toggle switches. Eventually grew to 4k memory with the video section of a TV for a monitor. With all that history it's a wonder I can think at-all. I will say, it's a whole different world today.

Thank you.  
Frank

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [May 2, 2025, 10:48am UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/34 "2025-05-02T10:48:05Z")

</div>

> [@frank2644](#):
>
> Yes, my computer is a little old using W10 with an I5 processor, still okay for my needs.

I don't think the issue you're having has anything to do with the age of your computer or which version of the Arduino IDE you're using. Variadic Templates (the technique I posted) were introduced with C++11. Every board support package for every flavor of Arduino board out there supports at least C++11. In fact, it's really on the cores for AVR-based boards that are stuck on C++11. Most others use C++17 or later.

You're experiencing some kind of installation or configuration issue. Unfortunately, not being a very knowledgeable tool chain guy, I'm unable to provide guidance in that area.

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [May 2, 2025, 11:39am UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/35 "2025-05-02T11:39:41Z")

</div>

If the IDE is old, there maybe an issue with the preprocessor creating unwanted function prototypes, which could explain the odd behaviour. This seems to get better with each attempt to fix it but will always be vulnerable to exotic constructs.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [May 2, 2025, 11:49am UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/36 "2025-05-02T11:49:11Z")

</div>

> [@6v6gt](#):
>
> If the IDE is old, there maybe an issue with the preprocessor creating unwanted function prototypes, which could explain the odd behaviour.

Good point, I forgot about that. I've seen issue with templates in the main .ino with earlier IDE versions. Moving them to a .h fixed it. IMO the auto prototype generator was a mistake from the beginning. Folks should have simply been introduced to supplying their own function prototypes early on.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [May 2, 2025, 12:17pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/37 "2025-05-02T12:17:24Z")

</div>

@frank2644, I know you're happy (enough) with your macro solution and are probably ready to move on. But, per @6v6gt's post, there's one more thing you might try. Break my example code into two files and see if it will compile.

**Main .ino File:**

```arduino
#include "Header.h"

void setup() {

  Serial.begin(115200);
  delay(1000);

  float f {3.14};
  uint8_t u16 {10};
  int16_t i16 { -5001};
  const char str[] {"Hello World"};

  printMany(f, '\n', u16, ", ", i16, "\n\n... ", str, '\n');
}

void loop() {
}

```

**Header.h:**

```arduino
#ifndef HEADER_H
#define HEADER_H

#include <Arduino.h>

template <typename T>
void printMany(T arg) {
  Serial.print(arg);
}

template <typename T, typename ... Remainder>
void printMany(T first, Remainder ... remainder) {
  Serial.print(first);
  printMany(remainder...);
}

#endif

```

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [May 2, 2025, 5:29pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/38 "2025-05-02T17:29:11Z")

</div>

> [@gfvalvo](#):
>
> IMO the auto prototype generator was a mistake from the beginning . . .

Don't get me started on the "prototype generation feature". I've said (nearly) all I want to say on that in various posts and conclude that, at least with .ino files, we simply have to live with it as a "sin from the past". I hope only that this mess is an object lesson for future developers who would even consider "improving" the C++ language by a series of backdoor hacks. Fortunately, this "feature" is restriced to .ino files so experienced users can soon find a work around.

However, having said that, I will take the opportunity of undermining one of the key pillars on which this debacle was built, that is the desire to protect new users from the otherwise well understood and basic principle that program variables etc. have to be declared before being used. Take the "multiple .ino" scenario. A user may build an application by combining multiple .ino files. These are consolidated into a single file based on alphabetical sort order of file name. The user will come to experience that variables declared in files with names lower in the alphabetical sorting order have a higher likelihood of being available throughout the application than those declared in files with a correspondingly higher alphabetical name ranking.

In general, however, I must now say that I consider these as minor blemishes on an otherwise well conceived and implemented philosophy which has very successfully brought microcontrollers to the masses.

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [May 3, 2025, 5:38am UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/39 "2025-05-03T05:38:49Z")

</div>

> [@frank2644](#):
>
> Basically I want to simplify the coding to print multiple items.

imho the simplest solution is to **stream** elements.  
Streaming can be used for all outputs which inherit Print.h .  
These are the HW Serials, SoftSerial, webclients, lot of LCD libraries and many more.

```arduino

#include <Streaming.h>

void setup() {
  Serial.begin(115200);
  delay(1000);

  float f {3.14};
  uint8_t u16 {10};
  int16_t i16 { -5001};
  const char str[] {"Hello World"};

  Serial << f << u16 << i16 << str << F("fixtext") << endl;

}

void loop() {
  // put your main code here, to run repeatedly:

}
```

include the Streaming library,  
instead of print use \<\<  
when you want to end a line stream endl

---

<div class="post-metadata">

**Author:** ![frank2644](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/frank2644/32/1126542_2.png) [@frank2644](https://forum.arduino.cc/u/frank2644)\
**Post date:** [May 4, 2025, 7:05pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/40 "2025-05-04T19:05:32Z")

</div>

gfvalvo,

Thanks,  
That worked! Compiled and executed as expected.

BTW, I improved my related macro as follows:  
#define printMany(arg1,arg2,arg3,arg4) Serial.print(arg1); Serial.print(arg2);Serial.print(arg3);Serial.print(arg4);

using AutoHotkey pressing F2 puts this into my sketch: printMany("","","","");

Then I simply put up to 4 parms where the quote marks are located. If I use say only one parm the unused args do no harm as double quotes without an intervening char apparently do nothing. Of course the macro can be expanded to 5 or more args if that is helpful.

So I have taken some of the inconvenience out of my macro solution. Your solution is better, but with my limited knowledge I hesitate to use something I do not understand. Maybe in the future as I gain knowledge I may be more comfortable using it.

Thanks again  
Frank

---

<div class="post-metadata">

**Author:** ![frank2644](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/frank2644/32/1126542_2.png) [@frank2644](https://forum.arduino.cc/u/frank2644)\
**Post date:** [May 4, 2025, 7:25pm UTC](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678/41 "2025-05-04T19:25:07Z")

</div>

noiasca,

Thanks, I never knew about streaming till now. That could come in handy in the future.

Actually, my problem involves calling a custom function not capable of accepting streaming. I decided to use Serial.print in my sudo code to simplify my question.

Thanks again,  
Frank

[Previous page](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678.md?page=1)

[Next page](https://forum.arduino.cc/t/simple-macro-program-i-think/1376678.md?page=3)
