Why use asprintf() instead of sprintf()?

If you use sprintf() or vsprintf(), you need to allocate a buffer first, and you need to be sure that the buffer is large enough to contain what sprintf writes. Otherwise sprintf() will happily overwrite whatever memory lies beyond the end of the buffer. char* x = malloc(5 * sizeof(char)); // writes “123456” +null but … Read more

Why wasn’t a specifier for `float` defined in `printf`?

Because in C variadic function calls, any float argument is promoted (i.e. converted) to a double, so printf is getting a double and would use va_arg(arglist, double) to get it inside its implementation. In the past (C89 and K&R C) every float argument was converted to a double. The current standard omits this promotion for … Read more

Code for printf function in C [duplicate]

Here’s the GNU version of printf… you can see it passing in stdout to vfprintf: __printf (const char *format, …) { va_list arg; int done; va_start (arg, format); done = vfprintf (stdout, format, arg); va_end (arg); return done; } See here. Here’s a link to vfprintf… all the formatting ‘magic’ happens here. The only thing … Read more

What is the purpose of the h and hh modifiers for printf?

One possible reason: for symmetry with the use of those modifiers in the formatted input functions? I know it wouldn’t be strictly necessary, but maybe there was value seen for that? Although they don’t mention the importance of symmetry for the “h” and “hh” modifiers in the C99 Rationale document, the committee does mention it … Read more

Printf long long int in C with GCC?

If you are on windows and using mingw, gcc uses the win32 runtime, where printf needs %I64d for a 64 bit integer. (and %I64u for an unsinged 64 bit integer) For most other platforms you’d use %lld for printing a long long. (and %llu if it’s unsigned). This is standarized in C99. gcc doesn’t come … Read more

find lacks the option -printf, now what?

It’s not that Apple removes options, it’s that OS X’s UNIX underpinnings are mostly derived (circuitously) from FreeBSD, many parts of which can be traced back to the original UNIX… as opposed to the GNU utilities, which are re-implementations with many features added. In this case, FreeBSD’s find(1) doesn’t support -printf, so I wouldn’t expect … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)