I encountered the problem as follows:
/* result */
a: Test:ABC123
b: Test:CDE456
a: Test:CDE456 b: Test:CDE456
The final printf returned "CDE456" twice instead of "ABC123" and "CDE456". Could you please help to resolve the problem?
Thank you
Karlc
Code:
#include <stdio.h> char* fun(char* b) { char* sTmp; static char d[BUFSIZ]; bzero(d, BUFSIZ); sprintf(d, "%s:%s", "Test", b); sTmp = d; return sTmp; } main() { char a[100]; char b[100]; char tmp[100]; bzero(tmp, 100); strcpy(a, "ABC123"); strcpy(b, "CDE456"); sprintf(tmp, "a: %s\n", fun(a)); printf("%s", tmp); sprintf(tmp, "b: %s\n", fun(b)); printf("%s", tmp); sprintf(tmp, "a: %s b: %s\n", fun(a), fun(b)); printf("%s", tmp); }
a: Test:ABC123
b: Test:CDE456
a: Test:CDE456 b: Test:CDE456
The final printf returned "CDE456" twice instead of "ABC123" and "CDE456". Could you please help to resolve the problem?
Thank you
Karlc
Comment