-2

我试图用 C 语言制作一个类似于 DOS 的 shell 解释器(显然是为了好玩)

当我键入 clear 时,如下面的代码所示,它应该可以清除屏幕。但事实并非如此。

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    char command[128];
    int loop = 0;
    void main(){
        clrscr();
        printf("Starting shell\n");
        clrscr();
        while ( loop == 0){
            printf("command:");
            scanf("%s", &command);
            if(command=='clear'){
                printf("Clearing screen");
                clrscr();
            }  

/** Other Code **/
4

1 回答 1

1
if(command=='clear')

这不是有效的字符串比较。使用strcmp比较C.

它应该是

if (!strcmp(command, "clear"))
{
   printf("Clearing screen");
   clrscr();
}
于 2014-08-20T11:14:15.313 回答