3 回答

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
來(lái)自scanf:
成功時(shí),該函數(shù)返回成功讀取的項(xiàng)目數(shù)。如果發(fā)生匹配故障,此計(jì)數(shù)可以匹配預(yù)期的讀數(shù)或更少,甚至為零。如果在成功讀取任何數(shù)據(jù)之前輸入失敗,則返回EOF。

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超5個(gè)贊
從技術(shù)上講,這是UB(未定義的行為)。
int g;
int p=scanf("%d",g);
^
您將一個(gè)整數(shù)化的整數(shù)傳遞給scanf以將其用作要寫(xiě)入的地址。從現(xiàn)在開(kāi)始,任何事情都可能發(fā)生。很可能你的應(yīng)用程序會(huì)崩潰。

TA貢獻(xiàn)1111條經(jīng)驗(yàn) 獲得超0個(gè)贊
從man頁(yè)面:
NAME
scanf, fscanf, sscanf, vscanf, vsscanf, vfscanf
...
RETURN VALUE
These functions return the number of input items successfully matched
and assigned, which can be fewer than provided for, or even zero in the
event of an early matching failure.
The value EOF is returned if the end of input is reached before either
the first successful conversion or a matching failure occurs. EOF is
also returned if a read error occurs, in which case the error indicator
for the stream (see ferror(3)) is set, and errno is set indicate the
error.
在你的情況下,scanf()可以返回0,1或EOF。
PS正如其他人所指出的那樣,你在前面錯(cuò)過(guò)了一個(gè)&符號(hào)g:
int p=scanf("%d",&g);
沒(méi)有&符號(hào),代碼的行為是不確定的。
- 3 回答
- 0 關(guān)注
- 398 瀏覽
添加回答
舉報(bào)