c語言中的EOF的詳細情況?

時間 2021-05-30 10:57:45

1樓:

通常EOF在stdio.h裡定義為-1,C把乙個位元組定義為char型別使用的位數,通常是8位,允許char前使用signed和unsigned表示有符號型別和無符號型別。

getchar()返回的unsigned char永遠不為負數,EOF是它不可能的值,所以while裡的判斷條件始終成立,始終不能等於EOF,迴圈會一直繼續下去。

可以使用char,但建議使用int,因為char是以int型別儲存的,int可以用於儲存字元型資料,此外,很多字符集都超過255個值,而char只有8位。

EOF有些是Ctrl+Z,有些是Ctrl+D,看系統。

2樓:鍾宇騰

那是因為你把EOF( 0xffffffff ) 強制轉換成 unsigned char 之後,就變成了 0xff 的乙個unsigned型別,後面再和EOF比較的時候,EOF的型別是int,於是就兩者都被轉換成了unsigned int,那麼就相當於比較

0x000000ff

!=0xffffffff

這個永遠都是true的。

你可以做個實驗

unsigned

charc=

EOF;

printf

("%x, %x, %x\n"

,c,(

unsigned

int)c,

EOF);

printf

("%d\n"

,c==EOF

);-----補充引用-----

引用getchar文件裡的一句

RETURN VALUES

If successful, these routines return the next requested object from the stream. Character values are returned as an unsigned char converted to

an int. If the stream is at end-of-file or a read error occurs, the routines return EOF. The routines feof(3) and ferror(3) must be used to

distinguish between end-of-file and error. If an error occurs, the global variable errno is set to indicate the error. The end-of-file condi-

tion is remembered, even on a terminal, and all subsequent attempts to read will return EOF until the condition is cleared with clearerr(3).

其中The end-of-file condition is remembered, even on a terminal, and all subsequent attempts to read will return EOF until the condition is cleared with clearerr(3).

求問日本大學的編入制度的詳細情況。

編入適合人群 在日本,短期大學 專門學校畢業後想繼續深造的學生。正規四年制大學,讀到大二或者大三,因為專業或者其他原因想更換院校繼續學習的學生,但必須修夠學年學分。國內的專科生,且取得文憑的學生。編入考試內容 絕大部分學校都設有筆試 面試。筆試內容,部分專業會考英語 閱讀和翻譯為主,翻譯是英譯日 專...

怎樣預防我爸的錢被我後媽騙走?詳細情況在下面,求求大家幫幫我吧,謝謝好心人!!!?

yss珊 我來答乙個,前鄰居,他家的孩子大我們幾歲,孩子們都讀小學的時候,他跟乙個死了丈夫的女人看對眼了,那個女人在市郊,有兩個孩子也是小學,死活要跟人家去,老婆孩子求了又求,還是沒辦法,淨身出戶了,乙個女人拖著兩個孩子,靠賣菜硬是把兩個孩子拉扯大了,還好那時候大家日子都不好過,孩子們也都平安長大了...

學生黨適合養什麼品種的狗? 詳細情況在下方說明

七月生的噗寶 學生黨先不要養貓狗,除非是你父母特別喜歡並且有時間精力去一起養。畢竟你現在沒有穩定收入,不要養貓狗。每年的畢業季就是一堆貓狗流浪,請三思。畢竟這是條生命,愛護它保護它教育它是你接下來十多年需要做的事。一開始沒看到補充問題,現回答 適合中小型犬。我自己飼養的是博美,小型犬。體重大概在3斤...