Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printf behaviour with $ notation when sequence of arguments incomplete

Tags:

c

printf

I'm working on my own implementation of the standard C library function printf. I am currently trying to add the (non-standard) behaviour for when the notation specifies an argument number ($).

In this case, if you supply 5 arguments and only argument 1, 2 3 and 5 are referenced in the format string, since the arguments are stored in a list obtained with va_start, I have a problem. To get to argument 5, I'll have to skip argument 4. This means I'll have to call va_arg on argument 4. That means I have to specify a type for argument 4, which I don't have any information about.

My question is: what data type does printf use in this situation? Or if that is unknown, what type would you use? I have tried void but va_arg doesn't accept it as a data type (which kind of makes sense). I know all of this is undefined behaviour but I'm trying to work out how printf handles it so any ideas are welcome!

like image 612
Laure Ravier Avatar asked May 20 '26 17:05

Laure Ravier


1 Answers

what data type does printf use in this situation?

It doesn't. printf expects that all numbered arguments are used. From the man page:

There may be no gaps in the numbers of arguments specified using '$'; for example, if arguments 1 and 3 are specified, argument 2 must also be specified somewhere in the format string.

So it's perfectly fine to assume in your example that if $5 is referenced in the format string that all prior ones are as well.

like image 109
dbush Avatar answered May 22 '26 05:05

dbush



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!