This question already has answers here:
Removing trailing newline character from fgets() input
                                
                                    (12个答案)
                                
                        
                                4个月前关闭。
            
                    
我正在用C开发一个项目。我需要在同一行中打印四个变量:其中3个是整数,而1个是存储字符串的数组。
我遇到的问题是当我尝试在同一行中全部打印时。在我打印数组后,它会跳行。我不希望它跳行,因为我需要同一行上的所有变量。

这是我做的开始:

#define MAX 100

struct cadastro
{
  int id;
  char nome[MAX];
  int datanascimento;
  int cpf;
};


这就是我要打印的方式:

printf("ID: %i  Nome: %s  Data de nascimento: %i  CPF: %i", cdto[c].id, cdto[c].nome, cdto[c].datanascimento, cdto[c].cpf);


这是我从打印中得到的反馈:

ID: 0  Nome: Nilton
  Data de nascimento: 2  CPF: 2


我正在使用fgets()键入名称。

最佳答案

当我使用fgets()来输入Name时,我只需要添加一个函数:

strtok(cdto[c].nome, "\n");

关于c - 在数组内打印字符串而不会跳过行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58575857/

10-11 22:25
查看更多