注册 登录
编程论坛 C++教室

cout语句中'\n'与endl的作用有什么区别没有?

yi157 发布于 2008-10-19 19:54, 5579 次点击
它们之间有什么区别呢?
以下程序原本输出为:
how about the story?
its very interesting.
为什么不是:
its very interesting.
how about the story?
但是把"\n"改为endl后输出为:
its very interesting.
how about the story?


程序:
#include "string.h"
#include "stdio.h"
#include "iostream.h"
class cstr
{
    char data[100];
public:
    void showme(void)
    {cout<<"its very interesting."<<"\n";}  ////////把"\n"改为endl它们有什么不同呢?
    char *get(void);
    int getlength(void);
    void cpy(char *s);
    void cat(char *s);
};
void main(void)
{
    cstr string1,string2;
    string1.showme();
    string1.cpy("how about ");
    string2.cpy("the story?");
    string1.cat(string2.get());
    puts(string1.get());
}
char *cstr::get(void)
{
    return data;
}
int cstr::getlength(void)
{
    return strlen(data);
}
void cstr::cpy(char *s)
{
    strcpy(data,s);
}
void cstr::cat(char *s)
{
    strcat(data,s);
}
16 回复
#2
newyj2008-10-19 20:10
用dev-c++编译 不管是 '\n' 还是 endl 输出的都是
its very interesting.
how about the story?
#3
yi1572008-10-19 21:13
[bo][un]newyj[/un] 在 2008-10-19 20:10 的发言:[/bo]

用dev-c++编译 不管是 '\n' 还是 endl 输出的都是
its very interesting.
how about the story?

不会吧,不过我用的是vc,书上也是作为一个例题讲的结果就是
how about the story?
its very interesting.
#4
中学者2008-10-19 21:43
'\n'是换行.."\n"这个是什么呢??
#5
lmyouya2008-10-19 22:02
[bo][un]中学者[/un] 在 2008-10-19 21:43 的发言:[/bo]

'\n'是换行.."\n"这个是什么呢??


cout<<'\n';     //可以吖
cout<<"\n";     [color=YellowGreen]//也可以吖[/color]

中学者有点不在状态!
#6
yi1572008-10-19 22:19
[bo][un]中学者[/un] 在 2008-10-19 21:43 的发言:[/bo]

'\n'是换行.."\n"这个是什么呢??

呵呵,"\n"是个字符串,多占一个空间
谢谢参与
#7
yi1572008-10-19 22:21
回复 5# lmyouya 的帖子

感谢参与
#8
blueboy820062008-10-19 22:36
回复 5# lmyouya 的帖子
嗯,功能一样哈...看情况而已
#9
blueboy820062008-10-19 22:47
我加断点一句一句看了一下.
发现写成"\n"这种情况特别有意思...
在运行到string1.showme();时,
照常跳到void showme(void)
    {cout<<"its very interesting."<<"\n";}
只是在执行完:cout<<"its very interesting."<<"\n";后,
屏幕上并没有输出...
另一种endl的和预想的一样,很正常.

目前还不知道原因...
#10
braveboy20082008-10-19 23:03
endl不仅有换行的作用,它还具有刷新输出缓冲区的作用
#11
yi1572008-10-20 22:02
[bo][un]blueboy82006[/un] 在 2008-10-19 22:47 的发言:[/bo]

我加断点一句一句看了一下.
发现写成"\n"这种情况特别有意思...
在运行到string1.showme();时,
照常跳到void showme(void)
    {cout

是这样啊,那如果你得知答案了别忘了告诉我哦(我知道也一样) 谢谢了!
#12
yi1572008-10-20 22:04
[bo][un]braveboy2008[/un] 在 2008-10-19 23:03 的发言:[/bo]

endl不仅有换行的作用,它还具有刷新输出缓冲区的作用

嗯,以前不知道还这功能,多谢赐教!
#13
f66667152008-10-20 22:39
回复 3# yi157 的帖子
利用VC++6.0的运行结果不是一样的,但两者都具有换行的功能,我的运行结果是:
1)endl:
its very interesting.
how about the story?
2)/n
how about the story?
its very interesting.
#14
yi1572008-10-21 10:23
[bo][un]f6666715[/un] 在 2008-10-20 22:39 的发言:[/bo]

利用VC++6.0的运行结果不是一样的,但两者都具有换行的功能,我的运行结果是:
1)endl:
its very interesting.
how about the story?
2)/n
how about the story?
its very interesting.

对,是这样啊..
#15
tjxix2008-10-21 10:32
dev cpp上无区别。
都是:
its very interesting.
how about the story?
#16
随心2008-10-21 13:01
endl:先清空输入缓冲区再换行
'\n':直接输出字符'\n'
#17
yi1572008-10-23 09:50
[bo][un]随心[/un] 在 2008-10-21 13:01 的发言:[/bo]

endl:先清空输入缓冲区再换行
'\n':直接输出字符'\n'

嗯,多谢了
1