You have declared string
as a single character but you fill it with a string. This invokes undefined behavior. You should change your code to :
char string [20] = "default"; //20 is random, you should use the maximum length of the input you may have
printf("The default String is: %19s", string);
scanf("%s", string);
printf("You entered: %s", string);
In general, scanf
needs to take a memory address as an argument, and in the code above, string
is a memory address. You can read more about scanf
in this link.
10
solved Why is the & operator used after using scanf? [closed]