Here is my program :-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char arrcTest[256] = {0};
strcat(arrcTest,"Hello");
sprintf(arrcTest,"%s","World");
strcat(arrcTest,"!!");
printf("The String is=> %s\n",arrcTest);
return 0;
}
I compiled it with gcc version 4.8.3 and got the following output :-
The String is=> World!!
Why is strcat not working first time but it's working properly from second time onwards?
sprintf
is not the same as strcat
. sprintf
formats the string and puts it at the beginning of the buffer. strcat
, on the other hand, appends the string to the end of the buffer.
strcat(arrcTest,"Hello"); /* after this statement you have "Hello" in arrcTest */
sprintf(arrcTest,"%s","World"); /* after this statement you have "World" in arrcTest */
strcat(arrcTest,"!!"); /* after this statement you have "World!!" in arrcTest */
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다