| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 867 人关注过本帖
标题:【小白问题】有句代码不明白什么意思。
只看楼主 加入收藏
tt7721
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:60
专家分:107
注 册:2011-8-7
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:11 
【小白问题】有句代码不明白什么意思。
while ((c=getchar())!='\n') 
我在ASC2 对照表中 没找到那个\n 字符啊?
代码中的意思应该是 当输入的 字符 为字母的时候 执行。


[ 本帖最后由 tt7721 于 2012-1-26 09:59 编辑 ]
搜索更多相关主题的帖子: 字母 
2012-01-26 09:34
回首依依
Rank: 7Rank: 7Rank: 7
来 自:苏州
等 级:黑侠
威 望:1
帖 子:193
专家分:524
注 册:2011-12-3
收藏
得分:10 
\n是转义字符,就是回车Enter
2012-01-26 09:57
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
'\n'是換行符LF,ASCII碼0x0A
'\r'才是回車符CR,ASCII碼0x0D

授人以渔,不授人以鱼。
2012-01-26 10:12
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:0 
呵呵,楼主这分算给错了

重剑无锋,大巧不工
2012-01-26 20:13
tt7721
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:60
专家分:107
注 册:2011-8-7
收藏
得分:0 
回复 4楼 beyondyf
那上面那句代码怎么解释啊。。。。。。。。。。。。。。
2012-01-27 07:50
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
从stackoverflov找到的一段话,
都开始从打字机时代开始讲历史原因了,应该可以明白了
CR an LF are control characters, respectively coded 0x0D (13 decimal) and 0x0A (10 decimal).

They are used to mark a line break in text file. A you indicated, Windows uses two characters the CR LF sequence; Unix only uses LF and MacIntosh CR.

An apocryphal historical perspective
As indicated by Peter, CR = Carriage Return and LF = Line Feed, two expressions which have their roots in the old typewriters / TTY. LF moved the paper up (but kept the horizontal position identical) and CR brought back the "carriage" so that the next character typed would be at the leftmost position on the paper (but on the same line). CR+LF was doing both, i.e. preparing to type a new line. As time went by the physical semantics of the codes were not applicable, and as memory and floppy disk space was at a premium, some OS designers decided to only use one of the characters, they just didn't communicate very well with one another ;-)

Most modern text editors and text-oriented application offer options/settings etc. that allow the automatic detection of the file's end-of-line convention and to display it accordingly.

[ 本帖最后由 madfrogme 于 2012-1-27 09:13 编辑 ]

The quieter you become, the more you can hear
2012-01-27 09:12
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
回复 6楼 madfrogme
嗯,是的,Windows和Unix的處理方式不同,但C標准庫的文件輸入輸出函數自己做了對應處理,程序員一律用'\n'編寫代碼即可,故有前面的疑惑。

其實,LF的'\n',是new line的意思,換新的一行,但打字機頭不會回到行首,只是向下面移一行,因此,在控制臺下type顯示一篇文本文件的內容,經常會看到像鋸齒一樣不斷向右偏移的比薩斜塔現象,就是這個原因。CR的'\r',是僅僅把機頭返回到行首而不換行,這樣打出來的文字等於重疊。只有CR+LF,才會有我們正常的回車換行現象。Unix作了簡化處理,只要看見LF就當CR+LF,但這樣只能視具體環境而區別對待,因為某些時候,編寫文本的人可能並不希望如此,而是按照原始的想法編寫的,這時Unix就自作多情了。

授人以渔,不授人以鱼。
2012-01-27 14:16
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
順便說一下,ASCII體系中的BACKSPACE(0x08),其實不是像我們按鍵盤BackSpace鍵那樣的動作,它僅僅是光標退一格(CR是直接退到行首)。我曾經利用這個特性寫過一段流模式下的帶編輯功能的輸入函數,即可以用BackSpace和Delete鍵、左右光標鍵編輯輸入數據的(沒有用conio.h中的函數),除了速度稍有降低(因為處理中文要判別回退和前進的格數),還可以應用。

[ 本帖最后由 TonyDeng 于 2012-1-27 14:28 编辑 ]

授人以渔,不授人以鱼。
2012-01-27 14:27
madfrogme
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:21
帖 子:1160
专家分:1106
注 册:2009-6-24
收藏
得分:0 
回复 8楼 TonyDeng
后面半段虽然没怎么看懂,
不过我好像明白vi 编辑器中插入模式下backspace只是倒回来一格,
再写内容才会被覆盖掉的原因了
还是不习惯

The quieter you become, the more you can hear
2012-01-27 14:42
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
嗯,你用putchar(0x08)就看到這種效果了。記住ASCII字符都是可以putchar()輸出的。

授人以渔,不授人以鱼。
2012-01-27 14:45
快速回复:【小白问题】有句代码不明白什么意思。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011900 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved