#include <stdio.h>
int main()
{
int countch=0;
int countwd=1;
printf("Enter your sentence in lowercase: ");
char ch='a';
while(ch!='\r')
{
ch=getche();
if(ch==' ')
countwd++;
else
countch++;
}
printf("\n Words =%d ",countwd);
printf("Characters = %d",countch-1);
getch();
}
This is the program where I came across \r
. What exactly is its role here? I am beginner in C and I appreciate a clear explanation on this.
这是我遇到的程序\ r \ n。它在这里的作用究竟是什么?我是C的初学者,我很欣赏这方面的明确解释。
7 个解决方案
#1
11
It's Carriage Return
. Source: http://msdn.microsoft.com/en-us/library/6aw8xdf2(v=vs.80).aspx
这是回程。资料来源:http://msdn.microsoft.com/en-us/library/6aw8xdf2(v=vs.80).aspx
The following repeats the loop until the user has pressed the Return key.
以下重复循环,直到用户按下Return键。
while(ch!='\r')
{
ch=getche();
}
#2
13
'\r'
is the carriage return character. The main times it would be useful are:
'\ r'是回车符。它有用的主要时间是:
-
When reading text in binary mode, or which may come from a foreign OS, you'll find (and probably want to discard) it due to CR/LF line-endings from Windows-format text files.
当以二进制模式读取文本或者可能来自外部操作系统时,由于Windows格式文本文件中的CR / LF行结尾,您会发现(并且可能想要丢弃)它。
-
When writing to an interactive terminal on
stdout
orstderr
,'\r'
can be used to move the cursor back to the beginning of the line, to overwrite it with new contents. This makes a nice primitive progress indicator.当写入stdout或stderr上的交互式终端时,'\ r'可用于将光标移回行的开头,用新内容覆盖它。这是一个很好的原始进度指标。
The example code in your post is definitely a wrong way to use '\r'
. It assumes a carriage return will precede the newline character at the end of a line entered, which is non-portable and only true on Windows. Instead the code should look for '\n'
(newline), and discard any carriage return it finds before the newline. Or, it could use text mode and have the C library handle the translation (but text mode is ugly and probably should not be used).
您帖子中的示例代码绝对是使用'\ r'的错误方法。它假定回车符将在输入行末尾的换行符之前,这是不可移植的,仅在Windows上为true。相反,代码应该查找'\ n'(换行符),并丢弃它在换行符之前找到的任何回车符。或者,它可以使用文本模式并让C库处理翻译(但文本模式很难看,可能不应该使用)。
#3
10
Once upon a time, people had terminals like typewriters (with only upper-case letters, but that's another story). Search for 'Teletype', and how do you think tty
got used for 'terminal device'?
曾几何时,人们有类似打字机的终端(只有大写字母,但这是另一个故事)。搜索'Teletype',您认为tty如何用于'终端设备'?
Those devices had two separate motions. The carriage return moved the print head back to the start of the line without scrolling the paper; the line feed character moved the paper up a line without moving the print head back to the beginning of the line. So, on those devices, you needed two control characters to get the print head back to the start of the next line: a carriage return and a line feed. Because this was mechanical, it took time, so you had to pause for long enough before sending more characters to the terminal after sending the CR and LF characters. One use for CR without LF was to do 'bold' by overstriking the characters on the line. You'd write the line out once, then use CR to start over and print twice over the characters that needed to be bold. You could also, of course, type X's over stuff that you wanted partially hidden, or create very dense ASCII art pictures with judicious overstriking.
这些设备有两个不同的动作。回车将打印头移回到行的开头而不滚动纸张;换行符将纸张向上移动一行而不将打印头移回到行首。因此,在这些设备上,您需要两个控制字符才能使打印头返回到下一行的开头:回车符和换行符。因为这是机械的,所以需要时间,因此在发送CR和LF字符后,在向终端发送更多字符之前,必须暂停足够长的时间。没有LF的CR的一个用途是通过重写线上的字符来做“大胆”。您将该行写出一次,然后使用CR重新开始并在需要加粗的字符上打印两次。当然,你也可以输入你希望部分隐藏的东西,或者创造非常密集的ASCII艺术图片,并明智地进行重击。
On Unix, all the logic for this stuff was hidden in a terminal driver. You could use the stty
command and the underlying functions (in those days, ioctl()
calls; they were sanitized into the termios
interface by POSIX.1 in 1988) to tweak all sorts of ways that the terminal behaved.
在Unix上,这些东西的所有逻辑都隐藏在终端驱动程序中。您可以使用stty命令和底层函数(在那些日子里,ioctl()调用;它们在1988年被POSIX.1清理到termios接口中)以调整终端表现的各种方式。
Eventually, you got 'glass terminals' where the speeds were greater and and there were new idiosyncrasies to deal with - Hazeltine glitches and so on and so forth. These got enshrined in the termcap
and later terminfo
libraries, and then further encapsulated behind the curses
library.
最终,你得到了“玻璃终端”,其速度更快,并且有新的特性需要处理 - 黑塞尔汀故障等等。这些被载入termcap和后来的terminfo库,然后进一步封装在curses库后面。
However, some other (non-Unix) systems did not hide things as well, and you had to deal with CRLF in your text files - and no, this is not just Windows and DOS that were in the 'CRLF' camp.
但是,其他一些(非Unix)系统也没有隐藏它们,你必须在你的文本文件中处理CRLF - 不,这不仅仅是在'CRLF'阵营中的Windows和DOS。
Anyway, on some systems, the C library has to deal with text files that contain CRLF line endings and presents those to you as if there were only a newline at the end of the line. However, if you choose to treat the text file as a binary file, you will see the CR characters as well as the LF.
无论如何,在某些系统上,C库必须处理包含CRLF行结尾的文本文件,并向您呈现这些文本文件,就好像在行尾只有换行符一样。但是,如果您选择将文本文件视为二进制文件,您将看到CR字符以及LF。
Systems like the old Mac OS (version 9 or earlier) used just CR (aka \r
) for the line ending. Systems like DOS and Windows (and, I believe, many of the DEC systems such as VMS and RSTS) used CRLF for the line ending. Many of the Internet standards (such as mail) mandate CRLF line endings. And Unix has always used just LF (aka NL or newline, hence \n
) for its line endings. And most people, most of the time, manage to ignore CR.
旧Mac OS(版本9或更早版本)等系统仅使用CR(又名\ r \ n)作为行尾。像DOS和Windows这样的系统(我相信很多DEC系统,如VMS和RSTS)都使用CRLF作为行结束。许多互联网标准(如邮件)要求CRLF行结束。并且Unix总是使用LF(也就是NL或换行符,因此\ n)来表示其行结尾。大多数人大多数时候都设法忽略CR。
Your code is rather funky in looking for \r
. On a system compliant with the C standard, you won't see the CR unless the file is opened in binary mode; the CRLF or CR will be mapped to NL by the C runtime library.
你的代码在寻找\ r时非常时髦。在符合C标准的系统上,除非文件以二进制模式打开,否则不会看到CR; CR运行时库将CRLF或CR映射到NL。
#4
2
There are a few characters which can indicate a new line. The usual ones are these two: '\n' or '0x0A' (10 in decimal) -> This character is called "Line Feed" (LF). '\r' or '0x0D' (13 in decimal) -> This one is called "Carriage return" (CR).
有几个字符可以表示新行。通常的是这两个:'\ n'或'0x0A'(十进制10) - >此字符称为“换行”(LF)。 '\ r'或'0x0D'(十进制13) - >这一个称为“回车”(CR)。
Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones:
不同的操作系统以不同的方式处理换行。以下是最常见的简短列表:
DOS and Windows
DOS和Windows
They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).
他们希望换行符是两个字符的组合,即'\ r \ n'(或13后跟10)。
Unix (and hence Linux as well)
Unix(以及Linux也是如此)
Unix uses a single '\n' to indicate a new line.
Unix使用单个'\ n'来表示新行。
Mac
苹果电脑
Macs use a single '\r'.
Mac使用单个'\ r'。
#5
1
' \r ' means carriage return.
'\ r'表示回车。
The \r means nothing special as a consequence.For character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller . Historically a \n was used to move the carriage down, while the \r was used to move the carriage back to the left side of the screen.
\ r \ n意味着没有什么特别的结果。对于字符模式终端(通常模拟上面甚至更旧的打印),在原始模式下,\ r和\ n的行为类似(除了光标之外,因为有没有托架或滚轮。历史上,\ n用于向下移动托架,而\ r用于将托架移回屏幕左侧。
#6
1
That is not always true; it only works in Windows.
For interacting with terminal in putty, Linux shell,... it will be used for returning the cursor to the beginning of line. following picture shows the usage of that:
这并非总是如此;它只适用于Windows。对于与putty中的终端进行交互,Linux shell,...它将用于将光标返回到行的开头。下图显示了该用法的用法:
Without '\r':
没有'\ r':
Data comes without '\r' to the putty terminal, it has just '\n'.
it means that data will be printed just in next line.数据没有'\ r'到putty终端,它只有'\ n'。这意味着数据将在下一行打印。
With '\r':
使用'\ r':
Data comes with '\r', i.e. string ends with '\r\n'. So the cursor in putty terminal not only will go to the next line but also at the beginning of line
数据带有'\ r',即字符串以'\ r \ n'结尾。所以putty终端中的光标不仅会进入下一行,还会进入行的开头
#7
0
It depends upon which platform you're on as to how it will be translated and whether it will be there at all: Wikipedia entry on newline
这取决于你在哪个平台上进行翻译以及它是否会在那里:*在换行上的条目
#1
11
It's Carriage Return
. Source: http://msdn.microsoft.com/en-us/library/6aw8xdf2(v=vs.80).aspx
这是回程。资料来源:http://msdn.microsoft.com/en-us/library/6aw8xdf2(v=vs.80).aspx
The following repeats the loop until the user has pressed the Return key.
以下重复循环,直到用户按下Return键。
while(ch!='\r')
{
ch=getche();
}
#2
13
'\r'
is the carriage return character. The main times it would be useful are:
'\ r'是回车符。它有用的主要时间是:
-
When reading text in binary mode, or which may come from a foreign OS, you'll find (and probably want to discard) it due to CR/LF line-endings from Windows-format text files.
当以二进制模式读取文本或者可能来自外部操作系统时,由于Windows格式文本文件中的CR / LF行结尾,您会发现(并且可能想要丢弃)它。
-
When writing to an interactive terminal on
stdout
orstderr
,'\r'
can be used to move the cursor back to the beginning of the line, to overwrite it with new contents. This makes a nice primitive progress indicator.当写入stdout或stderr上的交互式终端时,'\ r'可用于将光标移回行的开头,用新内容覆盖它。这是一个很好的原始进度指标。
The example code in your post is definitely a wrong way to use '\r'
. It assumes a carriage return will precede the newline character at the end of a line entered, which is non-portable and only true on Windows. Instead the code should look for '\n'
(newline), and discard any carriage return it finds before the newline. Or, it could use text mode and have the C library handle the translation (but text mode is ugly and probably should not be used).
您帖子中的示例代码绝对是使用'\ r'的错误方法。它假定回车符将在输入行末尾的换行符之前,这是不可移植的,仅在Windows上为true。相反,代码应该查找'\ n'(换行符),并丢弃它在换行符之前找到的任何回车符。或者,它可以使用文本模式并让C库处理翻译(但文本模式很难看,可能不应该使用)。
#3
10
Once upon a time, people had terminals like typewriters (with only upper-case letters, but that's another story). Search for 'Teletype', and how do you think tty
got used for 'terminal device'?
曾几何时,人们有类似打字机的终端(只有大写字母,但这是另一个故事)。搜索'Teletype',您认为tty如何用于'终端设备'?
Those devices had two separate motions. The carriage return moved the print head back to the start of the line without scrolling the paper; the line feed character moved the paper up a line without moving the print head back to the beginning of the line. So, on those devices, you needed two control characters to get the print head back to the start of the next line: a carriage return and a line feed. Because this was mechanical, it took time, so you had to pause for long enough before sending more characters to the terminal after sending the CR and LF characters. One use for CR without LF was to do 'bold' by overstriking the characters on the line. You'd write the line out once, then use CR to start over and print twice over the characters that needed to be bold. You could also, of course, type X's over stuff that you wanted partially hidden, or create very dense ASCII art pictures with judicious overstriking.
这些设备有两个不同的动作。回车将打印头移回到行的开头而不滚动纸张;换行符将纸张向上移动一行而不将打印头移回到行首。因此,在这些设备上,您需要两个控制字符才能使打印头返回到下一行的开头:回车符和换行符。因为这是机械的,所以需要时间,因此在发送CR和LF字符后,在向终端发送更多字符之前,必须暂停足够长的时间。没有LF的CR的一个用途是通过重写线上的字符来做“大胆”。您将该行写出一次,然后使用CR重新开始并在需要加粗的字符上打印两次。当然,你也可以输入你希望部分隐藏的东西,或者创造非常密集的ASCII艺术图片,并明智地进行重击。
On Unix, all the logic for this stuff was hidden in a terminal driver. You could use the stty
command and the underlying functions (in those days, ioctl()
calls; they were sanitized into the termios
interface by POSIX.1 in 1988) to tweak all sorts of ways that the terminal behaved.
在Unix上,这些东西的所有逻辑都隐藏在终端驱动程序中。您可以使用stty命令和底层函数(在那些日子里,ioctl()调用;它们在1988年被POSIX.1清理到termios接口中)以调整终端表现的各种方式。
Eventually, you got 'glass terminals' where the speeds were greater and and there were new idiosyncrasies to deal with - Hazeltine glitches and so on and so forth. These got enshrined in the termcap
and later terminfo
libraries, and then further encapsulated behind the curses
library.
最终,你得到了“玻璃终端”,其速度更快,并且有新的特性需要处理 - 黑塞尔汀故障等等。这些被载入termcap和后来的terminfo库,然后进一步封装在curses库后面。
However, some other (non-Unix) systems did not hide things as well, and you had to deal with CRLF in your text files - and no, this is not just Windows and DOS that were in the 'CRLF' camp.
但是,其他一些(非Unix)系统也没有隐藏它们,你必须在你的文本文件中处理CRLF - 不,这不仅仅是在'CRLF'阵营中的Windows和DOS。
Anyway, on some systems, the C library has to deal with text files that contain CRLF line endings and presents those to you as if there were only a newline at the end of the line. However, if you choose to treat the text file as a binary file, you will see the CR characters as well as the LF.
无论如何,在某些系统上,C库必须处理包含CRLF行结尾的文本文件,并向您呈现这些文本文件,就好像在行尾只有换行符一样。但是,如果您选择将文本文件视为二进制文件,您将看到CR字符以及LF。
Systems like the old Mac OS (version 9 or earlier) used just CR (aka \r
) for the line ending. Systems like DOS and Windows (and, I believe, many of the DEC systems such as VMS and RSTS) used CRLF for the line ending. Many of the Internet standards (such as mail) mandate CRLF line endings. And Unix has always used just LF (aka NL or newline, hence \n
) for its line endings. And most people, most of the time, manage to ignore CR.
旧Mac OS(版本9或更早版本)等系统仅使用CR(又名\ r \ n)作为行尾。像DOS和Windows这样的系统(我相信很多DEC系统,如VMS和RSTS)都使用CRLF作为行结束。许多互联网标准(如邮件)要求CRLF行结束。并且Unix总是使用LF(也就是NL或换行符,因此\ n)来表示其行结尾。大多数人大多数时候都设法忽略CR。
Your code is rather funky in looking for \r
. On a system compliant with the C standard, you won't see the CR unless the file is opened in binary mode; the CRLF or CR will be mapped to NL by the C runtime library.
你的代码在寻找\ r时非常时髦。在符合C标准的系统上,除非文件以二进制模式打开,否则不会看到CR; CR运行时库将CRLF或CR映射到NL。
#4
2
There are a few characters which can indicate a new line. The usual ones are these two: '\n' or '0x0A' (10 in decimal) -> This character is called "Line Feed" (LF). '\r' or '0x0D' (13 in decimal) -> This one is called "Carriage return" (CR).
有几个字符可以表示新行。通常的是这两个:'\ n'或'0x0A'(十进制10) - >此字符称为“换行”(LF)。 '\ r'或'0x0D'(十进制13) - >这一个称为“回车”(CR)。
Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones:
不同的操作系统以不同的方式处理换行。以下是最常见的简短列表:
DOS and Windows
DOS和Windows
They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).
他们希望换行符是两个字符的组合,即'\ r \ n'(或13后跟10)。
Unix (and hence Linux as well)
Unix(以及Linux也是如此)
Unix uses a single '\n' to indicate a new line.
Unix使用单个'\ n'来表示新行。
Mac
苹果电脑
Macs use a single '\r'.
Mac使用单个'\ r'。
#5
1
' \r ' means carriage return.
'\ r'表示回车。
The \r means nothing special as a consequence.For character-mode terminals (typically emulating even-older printing ones as above), in raw mode, \r and \n act similarly (except both in terms of the cursor, as there is no carriage or roller . Historically a \n was used to move the carriage down, while the \r was used to move the carriage back to the left side of the screen.
\ r \ n意味着没有什么特别的结果。对于字符模式终端(通常模拟上面甚至更旧的打印),在原始模式下,\ r和\ n的行为类似(除了光标之外,因为有没有托架或滚轮。历史上,\ n用于向下移动托架,而\ r用于将托架移回屏幕左侧。
#6
1
That is not always true; it only works in Windows.
For interacting with terminal in putty, Linux shell,... it will be used for returning the cursor to the beginning of line. following picture shows the usage of that:
这并非总是如此;它只适用于Windows。对于与putty中的终端进行交互,Linux shell,...它将用于将光标返回到行的开头。下图显示了该用法的用法:
Without '\r':
没有'\ r':
Data comes without '\r' to the putty terminal, it has just '\n'.
it means that data will be printed just in next line.数据没有'\ r'到putty终端,它只有'\ n'。这意味着数据将在下一行打印。
With '\r':
使用'\ r':
Data comes with '\r', i.e. string ends with '\r\n'. So the cursor in putty terminal not only will go to the next line but also at the beginning of line
数据带有'\ r',即字符串以'\ r \ n'结尾。所以putty终端中的光标不仅会进入下一行,还会进入行的开头
#7
0
It depends upon which platform you're on as to how it will be translated and whether it will be there at all: Wikipedia entry on newline
这取决于你在哪个平台上进行翻译以及它是否会在那里:*在换行上的条目