且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以使用%s打印字符串的最大大小吗?

更新时间:2023-11-09 08:58:22

printf()有上限.它最多可以成功处理N char个. N在最低 4095.

任何一次转换可以产生的字符数至少应为 4095.C11dr§7.21.6.115


如果期望输出很大(320 kB),请考虑使用fputs(s, stream)而不是没有c4095限制的fprintf(stream, "%s", s);.

类似于 printf/fprintf根据c99的最大尺寸 >

What is the maximum size which can be printed using %s in c language.

I was trying to print a buffer in file using fprintf but at a point I felt that it was going more to 320KB . And fprintf was writing truncated string to the file . Is there any limit with %s ?

printf() has an upper limit. It will successfully handle up to N chars. N is at least 4095.

The number of characters that can be produced by any single conversion shall be at least 4095. C11dr §7.21.6.1 15


[Edit]

With such a large (320 kB) expected output, if possible, consider using fputs(s, stream) rather than fprintf(stream, "%s", s); which does not have this 4095 limitation.

Similar to printf/fprintf maximum size according to c99