Shouldn't the return be of type size_t instead? Because the size of objects in C is of this type, including the string passed to printf.
3 Answers
Why printf returns an int in C?
Shouldn't be of typesize_tinstead?
It could have been, but certainly an early design decision was to accommodate a return value of the negative EOF to indicate error.
size_t was something of an afterthought in early design choices. Many functions used int where size_t is used now in those pre-standard days.
fprintf() has an environmental limit "The number of characters that can be produced by any single conversion shall be at least 4095.", so any print that is attempting long output may run into that limit before INT_MAX/SIZE_MAX concerns.
-
@Jean-FrançoisFabre Added note: in 1970, the price of RAM was about $0.70, about 1/2 labor hour per byte. Early design choices need to consider the very expensive cost of computers and tactics used to lessen code/memory space.
intis simple. Aug 17, 2017 at 16:34 -
So
printf, for one reason, or another, doesn't comply to the standard? Because it uses anintto hold the number of characters. Aug 17, 2017 at 16:34 -
2@BiteBytes
printf()an extraordinary common function in those pre-standard days (1970s - 1989), became the target to match once the standard 1989 was created. That creation did not attempt to break existing practice where it could avoid it.printf()complies to the standard because it is the standard on which C89 was based. Aug 17, 2017 at 16:44 -
Ok, but what is the right way to check the return of snprintf? The "obvious" way:
char buff[N];size_t sz = sizeof(buff);int n = snprintf(buff, sz, "...");if (n < sz) ...gives rise to warnings about comparing signed and unsigned. Jul 6, 2018 at 13:38 -
@JohnHascalln Note that
n < 0indicates an encoding error.if (n < 0 || (uintmax_t) n >= sz) error();should certainly cover all platforms. Jul 12, 2018 at 1:11
You're largely right - actually printf should return a larger type, since it's theoretically possible to output many more bytes than the size of the largest object that can fit in memory, e.g. printf("%s%s", largest_string, largest_string) or even more trivial examples using field widths/precisions.
The reason is just a historical mistake that we're stuck with. It's particularly bad with snprintf, which is artificially limited to INT_MAX and is forced to return an error if you attempt to create a longer string with it.
-
There is still the problem of returning negative numbers. To indicate error. If it was up to right now to redesign it, what would you do? Aug 17, 2017 at 16:53
-
1@BiteBytes: Either use a type sufficiently large that the signed form of the type could be used, or use
(T)-1, whereTis an unsigned type, as the error indication. Note that this works conveniently withsnprintfwhereretval>=nis the error condition (truncation or error causing negative return) that you need to check for anyway. Aug 17, 2017 at 18:29 -
@R.. Use
(T)-1is a good idea.int retval = snprintf(... size_t n, ...andretval>=nworks well aside from fantasy platforms whereINT_MAX > SIZE_MAXfor theretval>=nis a signed compare.error = retval < 0 || retval >= n;always works. Aug 17, 2017 at 22:11 -
@chux: Indeed, fantasy platforms are a problem there, but it wouldn't be a problem if
snprintfactually returnedsize_tor some unsigned type. Aug 18, 2017 at 1:15
Back then compilers did not require a function declaration in order to call a function. The return type of a function without declaration or with unspecified return type is int - the implicit int rule. Some code called printf without bothering with pesky #include <stdio.h>.
printfis unlikely to exceed 32k, let alone 2G.printfreturn value can be negative. Thesize_ttype is unsigned.printfwas designed, there was nosize_tand people usedints. Changing it tosize_twould break some old code, so they can’t do that; if they were to designprintffrom scratch now they probably would usesize_t.size_torssize_t, but it really makes no difference for any real world example that I can think of.size_tisn't good cause you need a way to signal errors, and you can't use 0 for that because 0 is kind of a legitimate output count forprintf. I think I'd use ` ssize_t/ptrdiff_t/long` if I was doing my ownprintf.