简体   繁体   中英

Is there a shorter way to output C int with puts?

I'm having to do the following a lot in a USB (libusb) C based command line utility I am writing:

char pid[20];
sprintf(pid, "Product ID : %#06x", anInteger);
puts(pid);

Is there a shorter, one-liner way to do this?

而不是使用sprintfputs ,只需更改为printf

printf("Product ID : %#06x", descriptor.idVendor);

使用printf?

printf("Product ID : %#06x\n", descriptor.idVendor);

Not with puts... You could use printf, but then you can't store the data in the pid variable. Unfortunately you can't have it both ways. You could make use of a glibc extension to printf if you're on Linux that allows you to register a custom printf format (Google should turn up something), but I don't recommend it just to save a line or two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM