麻烦终止字符串 [英] Trouble Terminate strings

查看:117
本文介绍了麻烦终止字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙解释一下这是做什么的吗

字符缓冲区[40];

buffer [0] =``h'';
buffer [1] =``i'';
buffer [2] =''\ 0'';

printf(%s \ r \ n字符串结尾",buffer);

此代码打印

然后是40个空格,然后是字符串的结尾

我想用CR-LF来打个招呼

Can someone help explain what this is doning

Char buffer[40];

buffer[0] = ''h'';
buffer[1] = ''i'';
buffer[2] = ''\0'';

printf("%s\r\n end of string",buffer);

this code prints

hi then 40 spaces then end of string

I whant it to just print hi with a CR-LF

推荐答案

好的,我会尝试的.

首先,它不会先打印"hi",然后再打印40个空格,然后再打印"string of end". -尽管看起来确实很强大,但是看起来确实很相似. ;)

它打印"hi",然后将光标返回到该行的开头,然后前进到下一行的开头,最后它显示字符串的结尾"

不相信我吗?只需从printf语句中删除\ n,您将看到"hi"不可见-这是因为"string of end"已打印在其顶部.

要打印"hi",然后换行,然后字符串结尾",请使用以下命令:

Sure, I''ll try.

Firstly, it does not print "hi" then 40 spaces then "end of string". - Although it does look mighty similar to what would be seen if it did. ;)

It prints "hi", then it returns the cursor to the start of that line then it advances to the start of the next line, finally it prints " end of the string"

Don''t believe me? Just remove the \n from your printf statement and you''ll see that "hi" is not visible - that''s because " end of the string" has been printed over the top of it.

To print "hi", then a new line, then "end of the string" use the following:

printf("%s\nend of the string", buffer);


buffer [2] =''\ r''
buffer [3] =''\ n''
buffer[2] = ''\r''
buffer[3] = ''\n''


这篇关于麻烦终止字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆