Function in string

#include<stdio.h> #include<string.h> int main() { char s1[10]="Chandan"; printf("%ld\n",sizeof(s1)); char s2[10]="Mahato"; strcat(s1,s2); printf("%ld\n",sizeof(s1)); printf("%s\n",s1); return 0; } why here the size of s1 before concatinating and after conatinating is same . ?
2 Replies
muscle aggregator 82
Ask chatgpt
sigma
sigma2y ago
strcat does not dynamically increase the size of the first argument. If you check https://man7.org/linux/man-pages/man3/strcat.3.html, you'd see that the the expectations for first argument are that it must have neough capacity to hold the second argument as well. Also, sizeof doesn't give you the dynamic size of an array (whatever that could mean), it gives you the static size, which can only be determined during compile time. That is to say, if you are using static arrays, sizeof will give you the amount of bytes that array can hold at the moment you declare it (in this case, because char is a single byte, then sizeof will give 10, because it is 1 byte times the number of elements) If you need dynamic arrays in C, you'd nesd to use malloc and friends, but mind you that you cannot use sizeof, you'd have to keep track of the memory size yourself
Want results from more Discord servers?
Add your server