This is a discussion on Re: distrust snprintf returnvalue within the lucky.openbsd.tech forums, part of the OpenBSD category; --> OK, scratch that. Since the manpage is confusinging me I wrote this to get insight in what happens: % ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| OK, scratch that. Since the manpage is confusinging me I wrote this to get insight in what happens: % cat test.c #include <stdio.h> #include <string.h> int main(void) { int ret; int max = 6; char returnstring1[8] = "yadadad"; char returnstring2[6] = "yadad"; char returnstring3[4] = "yad"; char returnstring4[2] = "y"; printf("strlen(returnstring1) = %i \n", strlen(returnstring1)); printf("strlen(returnstring2) = %i \n", strlen(returnstring2)); printf("strlen(returnstring3) = %i \n", strlen(returnstring3)); printf("strlen(returnstring4) = %i \n", strlen(returnstring4)); ret = snprintf(returnstring1, max, "%s%s", "foo", "bar"); printf("ret = %i, returnstring1 = %s, lenght = %i\n", ret, returnstring1, strlen(returnstring1)); ret = snprintf(returnstring2, max, "%s%s", "foo", "bar"); printf("ret = %i, returnstring2 = %s, lenght = %i\n", ret, returnstring2, strlen(returnstring2)); ret = snprintf(returnstring3, max, "%s%s", "foo", "bar"); printf("ret = %i, returnstring3 = %s, lenght = %i\n", ret, returnstring3, strlen(returnstring3)); ret = snprintf(returnstring4, max, "%s%s", "foo", "bar"); printf("ret = %i, returnstring4 = %s, lenght = %i\n", ret, returnstring4, strlen(returnstring4)); return 0; } So if you are not so sure what the result will be, please write down what you think it will be and only then compile and see what it does. If you do know what happens, /me bows for you in awe, and please tell me why it does. Because it doesn't make sense to me. I expected really something different. # Han |