random unwanted symbols in char array in C -
im trying make array of strings using strcat(), keeps giving me unwanted characters, example: char arr[50] strcat(arr, "test") , when puts(arr) or printf("%s\n", arr), gives me @!*test instead of test, know what's causing problem?
thanks!
you must initialize array before using value, or invoke undefined behavior.
try this:
#include <stdio.h> #include <string.h> int main(void) { char arr[50] = {0}; /* add = {0} */ strcat(arr, "test"); puts(arr); return 0; }
Comments
Post a Comment