Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug/reformat C printf calls with lots of arguments in vim?

Tags:

c

vim

I have a function call in a program that I'm maintaining has 28 arguments for a printf call. It's printing a lot of data in a CSV file. I have problems following finding where what goes and I have some mismatches in the parameters types. I 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 like this:

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? How to better reformat the code to make it more readable?

like image 710
Costi Avatar asked Jan 19 '26 00:01

Costi


2 Answers

Not sure I know a good vim trick off the top of my head, but I 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 )))));
like image 141
rampion Avatar answered Jan 21 '26 23:01

rampion


split the format string and the call into several fprintf calls

like image 30
Gregory Pakosz Avatar answered Jan 22 '26 00:01

Gregory Pakosz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!