我得到並顯示名稱,並在我輸入密鑰時停止程序。在下面的代碼中,我得到了正確的結果(我知道「被」已經過時):scanf並獲得不同的工作,將' 0'放入數組中?
#include <stdio.h>
main()
{
char name[50];
while(1)
{
printf("Name: ");
scanf("%s", name);
if(name[0]=='\0')
break;
else
printf("Name entered: %s\n", name);
}
}
但是當我嘗試使用scanf函數:
printf("Nome: ");
scanf("%s", nome);
條件name[0]=='\0'
永遠是真實的這段時間。爲什麼? '\0'
在這些功能中的作用是不同的?
因爲'Enter Key'是'\ n' – Shank
http://stackoverflow.com/questions/26391465/why-is-adding-a-leading-space-in-a-scanf-format-string-recommended check這個瞭解@Shank評論 – 2016-07-04 14:15:05
謝謝Olivier。 – Ricardo