Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scanf("%[^\n]",command) inside while loop

I want to have an infinite loop getting command each loop,

and this is my code

while ( 1 )
{
    char * command[100];
    printf("---| ");
    scanf( "%[^\n]",command);
    printf("%s\n",command);

}

for some reason it only inputs once and the loop doesnt terminate with asking the input.

what did i do wrong here?

like image 988
JaemyeongEo Avatar asked Apr 23 '26 00:04

JaemyeongEo


1 Answers

The definition should be

char command[100];

And not char *command[100] - this is a array of 100 char pointers.

Also scanf() is not easy to use, I would use fgets(command, sizeof(command), stdin); and then remove the newline.

while ( 1 )
{
    char command[100];
    printf("---| ");
    scanf( "%s", command);
    printf("%s\n",command);
}
like image 195
suspectus Avatar answered Apr 28 '26 13:04

suspectus