C 奇怪的运行时问题:printf中途停止工作

C 奇怪的运行时问题:printf中途停止工作,c,networking,gdb,printf,C,Networking,Gdb,Printf,当我在网络实验室写一篇文章时,我发现了一个非常奇怪的行为: printf("Recieved login info.\n"); //works printf("Username: %s\n", ui->username); //works printf("Passward: %s\n", ui->password); //works //TO_AUTHENTICATE, if failure, send appropr

当我在网络实验室写一篇文章时,我发现了一个非常奇怪的行为:

        printf("Recieved login info.\n"); //works
        printf("Username: %s\n", ui->username); //works
        printf("Passward: %s\n", ui->password); //works

        //TO_AUTHENTICATE, if failure, send appropriate reply
        printf("makeitwork"); //does not work
        while(ui != NULL)
        {
                printf("here2");//does not work
                if(strcmp(ui->username, record.username) == 0 && strcmp(ui->password, record.password) == 0)
                {
                        strcpy(userRecords[sockfd].username, record.username);
                        userRecords[sockfd].status = ONLINE;

                        printf("Successfully authenticated %s.\n", userRecords[sockfd].username);

                //      sendOnlineUsers(sockfd, registeredUsers);

                        return SUCCESS;
                }
                ui = ui->next;
        }
在上面的代码段中,第一个、第二个和第三个printf语句可以工作,但从第四个开始,它就不工作了

但是如果我把if语句printfhere2注释掉;,一切正常

我使用gdb逐行执行程序:

(gdb) 
Recieved login info.
94      printf("Username: %s\n", ui->username);
(gdb) 
Username: user-1
95      printf("Passward: %s\n", ui->password);
(gdb) 
Passward: pass
98      printf("makeitwork");
(gdb) 
99      while(ui != NULL)
(gdb) 
101         printf("here2");
(gdb) 
102         if(strcmp(ui->username, record.username) == 0i)//&& strcmp(ui->password, record.password) == 0)
printf正在执行,但终端上没有输出


发生了什么事?

您需要刷新缓冲区

在printf结束时没有\n的地方

您是对的。我输入了a/n,现在它开始工作了。你能解释一下发生了什么,我接受你的回答。Printf把东西放在缓冲区里\n刷新缓冲区并将其发送到终端。如上所述,发送新行或刷新缓冲区
fflush(stdout);