At 01:25 22-8-2002, Marcus Börger wrote: >At 00:56 22.08.2002, you wrote: >> >>Is this what you are looking for? >> >>#include <stdio.h> >> >>int main() >>{ >> double f; >> char *result; >> size_t buf_len=1024; >> >> result = (char *) malloc(buf_len+1); >> f = -.3333333333333333; >> if(snprintf(result, buf_len, "Result is: %f and precision 12: >> %.12f\n", f, f) < 1) >> { >> fprintf(stderr, "Error\n"); >> } >> else >> { >> printf("%s", result); >> } >>} >> >>$ ./snprintf >>Result is: -0.333333 and precision 12: -0.333333333333 > >No there are more important issues in compliancy . >Especially there are libraries out which do not check for the length >parameter (i already have the code needed for that)
Ok. >The code for you provided does not work because you only >check for the number of characters written. Instead you must >check for the '0' in front of the decimal point. E.g: > >char buf[10]; >if (strcmp("0.1", snprintf(buf, sizeof(buf), "%.f", .1))) ... > >Besides the return value is different in the libraries, too. Some return >the length actually written but C99 requires to return the number of >characters that could have been written if buffer was large enough. Then this one is definetely not C99 compliant. Changed the test to: #include <stdio.h> int main() { char buf[5]; int written; written = snprintf(buf, sizeof(buf), "%f", .111); if (strcmp("0.1", buf)==0 && written == 6) { printf("OK\n"); } else { printf("Not ok: %s (%i)\n", buf, written); } } Result: Not ok: 0.11 (5) Additionally - with "%.f" it rounds to integer zero. Result: Not ok: 0 (1) Met vriendelijke groeten / With kind regards, Webmaster IDG.nl Melvyn Sopacua -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php