Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Posix supply format string macros for printf/scanf?

Tags:

c++

c

posix

printf

The printf and scanf families of functions in C consume a handful of primitive format specifiers that correspond to the fundamental data types – %d for int, %llu for unsigned long long int, etc.

However, there are a large number of standardized type aliases that one would like to use in practice, such as int32fast_t, and one cannot and should not have to know the underlying fundamental type. For the aliases in stdint.h, the C standard thankfully specifies a set of macros to generate the corresponding format strings, like PRI32, in inttypes.h.

Is there an analogous set of macros for Posix? Posix has tons of opaque types like ssize_t, pid_t, rlim_t, suseconds_t, etc, which are all variations on the basic intgral types. How can one portably use those types in format strings?

like image 679
Kerrek SB Avatar asked Aug 30 '13 13:08

Kerrek SB


1 Answers

The sfio package (part of AT&T Labs' open source AST software) has functions analogous to printf and scanf which let you specify the size of the numeric value (typically using sizeof()) as an additional parameter. Some examples:

sfprintf(sfstdout, "%I*d", sizeof(intval), intval);
sfscanf(sfstdin, "%I*f", sizeof(fltval), &fltval);

USENIX paper: Extended Data Formatting Using Sfio .

like image 106
Mark Plotnick Avatar answered Sep 17 '22 15:09

Mark Plotnick