Why does strcat not work with empty string in C?

Pankaj Goyal

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?

edtheprogrammerguy

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does %(Identity) expand to an empty string?

분류에서Dev

Why does this C code not work in JavaScript?

분류에서Dev

Why does this C code not work in JavaScript?

분류에서Dev

why sls aka select-string does not work

분류에서Dev

why the function strcat not working?

분류에서Dev

Why does empty Bitbucket repo "contain work" that I "do not have locally"?

분류에서Dev

Why and how does this work?

분류에서Dev

why does (*callback)() work and not *callback() or *callback in c++

분류에서Dev

Why the cycle function cannot work with empty list?

분류에서Dev

Javascript null or empty control does not work

분류에서Dev

Why does pkill not work with rofi?

분류에서Dev

Why does sudo not work with curl?

분류에서Dev

Why does remove(x) not work?

분류에서Dev

Why does this angularjs directive not work?

분류에서Dev

Why does this rename operation not work?

분류에서Dev

Why does this type of inheritance work?

분류에서Dev

C++ why does empty set::emplace() inserts an element into a set of pointers?

분류에서Dev

C-strcat 문제

분류에서Dev

Why does my empty SD-Card is not empty

분류에서Dev

Why does the Dispose pattern in C# not work more like RAII in C++

분류에서Dev

Why does my C++ function, only when it's placed after main(), not work?

분류에서Dev

Why does this command work for logging script output?

분류에서Dev

Why NodeJS KeepAlive does not seem to work as expected?

분류에서Dev

Why does `kill %jobnumber` not work on stopped jobs?

분류에서Dev

Why does the first assert work, but not the second?

분류에서Dev

Why does this CURL work in PHP but not in the shell

분류에서Dev

Why does the wildcard * not work when changing directories?

분류에서Dev

Why does the following code only work with GridLayout?

분류에서Dev

Why does setTo not work (assertion failed)?

Related 관련 기사

뜨겁다태그

보관