简体   繁体   English

有没有更短的方法通过puts输出C int?

[英]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: 我必须在正在编写的基于USB(libusb)C的命令行实用程序中执行以下操作:

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. 不能与puts一起使用...您可以使用printf,但是不能将数据存储在pid变量中。 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. 如果您使用的是Linux,可以使用glibc扩展名对printf进行扩展,该扩展名允许您注册自定义printf格式(Google应该启用某种格式),但我不建议您仅保存一两行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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