My code is:
#include <stdio.h>
#include <string.h>
char *getUserInput() {
char command[65];
//Ask the user for valid input
printf("Please enter a command:\n");
fgets(command, 65, stdin);
//Remove newline
command[strcspn(command, "\n")] = 0;
return command;
}
int main() {
char *recCommand = getUserInput();
printf("%s", recCommand);
return 0;
}
When this code is executed, this is the console:
Please enter a command:
Test <-- this is the command I entered
*weird unknown characters returned to console*
Why are there weird unknown characters being returned to console instead of "Test"?
return strdup(command);this make copy string.( secure the area in heap) thenfree(command);at main.