2016-08-23 67 views
-1
printf("It is currently %s's turn.\n", current->name); 

我想知道为什么在%s之后打印出额外的换行符。我知道C中的字符串总是以\ 0结尾。我如何在没有它的情况下打印它?Printf在printf中插入字符串值的附加换行符

+3

'current-> name'在结尾处有一个换行符。 – byxor

+2

字符串总是以'\ 0'结尾,'\ n'只是一个换行符。 – byxor

+0

是的,但如何去除打印的换行符?因为当前 - >来自用户输入 –

回答

6

你的变量current->name有一个换行符,所以你需要摆脱那个换行符。

current->name[strcspn(current->name, "\n")] = '\0'; 

这段代码将帮助您摆脱不需要的换行符。把它放在printf之前。

+0

欢呼的人,它的工作原理和strcspn是什么意思? –

+0

@LimEweWin典型的神秘C函数;)这里解释[https://www.techonthenet.com/c_language/standard_library_functions/string_h/strcspn.php] – byxor

+0

你可以在这里看到它(http:// www。 tutorialspoint.com/c_standard_library/c_function_strcspn.htm)。这是一个很好的功能。 – Mirakurun