How do I debug / reformat C printf calls with more arguments in vim?
I have a function call in a program I support that has 28 arguments to call printf. It prints a lot of data in a CSV file. I'm having trouble finding what's going on and I have some mismatches in the parameter types. I have enabled -Wall in gcc and I get warnings like:
n.c:495: warning: int format, pointer arg (arg 15)
n.c:495: warning: format argument is not a pointer (arg 16)
n.c:495: warning: double format, pointer arg (arg 23)
The function is as follows:
fprintf (ConvFilePtr, "\"FORMAT3\"%s%04d%s%04d%s%s%s%d%s%c%s%d%c%s%s%s%s%s%s%s%11.lf%s%11.lf%s%11.lf%s%d\n", some_28_arguments_go_here);
I would like to know if there is a vim plugin that highlights the printf format specifier when I go with the cursor over a variable.
Other solutions? What's the best way to reformat the code to make it more readable?
a source to share
Not sure if I know a good vim trick from the head, but I do know a good C macro to make it a little easier:
#define last( f, a, ft, ... ) f ft, a, __VA_ARGS__
#define pair( f, a, ftat ) last( f, a, ftat )
// ...
printf( pair( "%s", "hello",
pair( "%s", "world",
pair( "%c", '\n',
last( "%4x", 0xfeed,
"%f\n", 3.14159 )))));
a source to share
You can split it and save only one call fprintf
. I often do something like:
fprintf (ConvFilePtr,"\"FORMAT3"
"%s"
"%04d%s"
"%04d"
"%s%s%s"
"%d"
"%s%c"
"%s%d%d"
"%c%s"
"%s%s%s"
"%s%s"
"%s%11.lf"
"%s%11.lf"
"%s%11.lf"
"%s%d\n", str1,
int1, str2,
int2,
etc...);
You realize that you still only have one call (which is important, since I / O is often an order of magnitude slower than pushing variables on the stack), and you can order the variables so that they are grouped logically, making it easier to spot the problem.
a source to share