Using printf type variable parameters in your function
I needed this in a bit of debug code. I wanted it to work like printf where there’s a format string containing one or more % format specifications and then write this into a buffer and dump it where ever.
C has a library stdarg which lets you do this. It’s not the most intuitive but it’s definitely worth understanding.
What I’m wanting to do is a function that does something like this (assume s1,s2 and s3 are char *).
sprintf(buffer,"Some string values %s %s %s",s1,s2,s3);
doSomething(buffer);
But in my own function and with the ability to have 0,1,2 or how ever many parameters without having to write a separate function for each. Kind of what printf does.
Here’s the code:
#include <stdarg.h>
void op(char* s, ...) {
char buffer[50];
va_list argptr;
va_start(argptr, s);
vsprintf_s(buffer,sizeof(buffer),s,argptr);
OutputDebugStringA(buffer);
va_end(argptr);
}
The … represent the variable number of parameters. it’s called the variadic operator. To access the actual parameters needs the various va_ macros and types. For instance va_list is a type that manages the list of parameters. The va_start macro takes the list and the parameter before the list. vsprintf_s is the Microsoft secure version of vsprintf. Both are the variable parameter equivalent of sprintf/sprintf_s.
OutputDebugString is the Windows debug string function. Finally the va_end tidies up everything.
So you use this just like printf, except the output goes to the Debug channel and can be picked up in Visual Studio (if debugging) or by running the SysInternals free DebugView utility.
Note, the original version of this used OutputDebugString but I found it was outputting gibberish. I correctly guessed that it was linking to OutputDebugStringW ; the MBCS version and changing it to OutputDebugStringA (the ASCII version) fixed it. Something to watch out for on Windows.

So after yesterday’s post I also installed Code::Blocks on Ubuntu 20.04 LTS, the recent six monthly Ubuntu release. Guess what, it’s a much newer version of Code::Blocks that looks slightly different and does include SDL2. Although the demo program it creates is C++ not C (That coloured bar picture is the demo). I haven’t used it enough to see what’s different between this and version 16.01.

I’ve done 70 odd blog posts so far since I started this in March 2020 and there’s a fair number of gems and nuggets in there. Finding them though is probably a bit of hassle, so to let you see them easily, I’ve created a page of tips, accessible from 
Unfortunately there’s no gamepad-tool for the PI, but there is a program called jstest and in particular a visual one called jstest-gtk. That’s it on the left. You install it on any Linux system, including Raspbian on the Pi with the command
I’ve never dealt with a game pad (joypad etc.) before. It seems that there is no absolute mapping, it’s more like TV remote controls. There’s lots of them and they’re all different. So although SDL2 has a set of enum values for the buttons, I found that my game pad wasn’t responding to what I thought were the game keys.
Back in November 2018, I was in Nottingham and passed a stand at the Winter Fayre. It was offering a console with 18,000 games on it for about £70. I bought one and what I got was an Orange PI with a 16 GB SD Card (all but full) and two USB joypads.
When programmers have to explain why it took longer to get something working,you don’t often here reasons like this. A simple syntax error error took me an hour to find and fix. Yet it does happen and it happened to me today.