C语言-书写长字符串时的换行方法
 2021.09.26    |      C/C++    |     AilsonJack    |     暂无评论    |     101 views
广告位C招租:1
By: Ailson Jack
Date: 2021-09-26
个人博客: http://www.only2fire.com/
<p style="text-indent: 2em;">在编写C程序时,如果想要打印某个字符串,而字符串的内容比较多,这就涉及到对这个长字符串进行书写换行,这里的换行并不会对最终的显示结果进行换行,只是为了阅读代码能够更加的清晰,不至于字符串的内容过长影响代码的阅读体验。<br/></p><p class="artical_littlestyle1">1.长字符串示例</p><pre class="brush:cpp;toolbar:false PrismJs">/** &nbsp;*&nbsp;@file&nbsp;test.c &nbsp;*&nbsp;@author&nbsp;Ailson&nbsp;Jack&nbsp;(jackailson@foxmail.com) &nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blog:&nbsp;www.only2fire.com &nbsp;*&nbsp;@brief &nbsp;*&nbsp;@version&nbsp;1.0 &nbsp;*&nbsp;@date&nbsp;2021-09-25 &nbsp;* &nbsp;*&nbsp;@copyright&nbsp;Copyright&nbsp;(c)&nbsp;2021 &nbsp;* &nbsp;*/ #include&nbsp;&lt;stdio.h&gt; int&nbsp;main(void) { &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;name[]&nbsp;=&nbsp;&quot;Ailson&nbsp;Jack&quot;; &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;age&nbsp;=&nbsp;18; &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello,&nbsp;My&nbsp;name&nbsp;is&nbsp;%s.\r\nI&nbsp;am&nbsp;%d&nbsp;years&nbsp;old&nbsp;this&nbsp;year.\r\nNice&nbsp;to&nbsp;meet&nbsp;you.\r\nCan&nbsp;I&nbsp;make&nbsp;friends&nbsp;with&nbsp;you?\r\nI&nbsp;like&nbsp;programming&nbsp;in&nbsp;C&nbsp;very&nbsp;much.\r\n&quot;,&nbsp;name,&nbsp;age); &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(1); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; }</pre><p style="text-indent: 2em;">上述代码需要打印的字符串内容比较长,在代码阅读软件中,可能需要拖动水平方向上的滚动条,才能看清楚字符串的完整内容,这极大的影响了代码的阅读效率。下图是上述代码的运行结果:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2021.09.26/204931768947262.png" onclick="preview_image(&#39;/uploads/AilsonJack/2021.09.26/204931768947262.png&#39;)"/></p><p class="artical_littlestyle2">2.书写长字符串的换行方法</p><p style="text-indent: 2em;"><strong>方法一:利用双引号对长字符串进行换行</strong><br/></p><p style="text-indent: 2em;">在对长字符串进行书写换行时,可以使用双引号将长字符串拆分成多个子字符串,编译器在编译处理时会自动的拼接这些子字符串,不会影响最终想要的显示效果,示例代码如下:</p><pre class="brush:cpp;toolbar:false PrismJs">/** &nbsp;*&nbsp;@file&nbsp;test.c &nbsp;*&nbsp;@author&nbsp;Ailson&nbsp;Jack&nbsp;(jackailson@foxmail.com) &nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blog:&nbsp;www.only2fire.com &nbsp;*&nbsp;@brief &nbsp;*&nbsp;@version&nbsp;1.0 &nbsp;*&nbsp;@date&nbsp;2021-09-25 &nbsp;* &nbsp;*&nbsp;@copyright&nbsp;Copyright&nbsp;(c)&nbsp;2021 &nbsp;* &nbsp;*/ #include&nbsp;&lt;stdio.h&gt; int&nbsp;main(void) { &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;name[]&nbsp;=&nbsp;&quot;Ailson&nbsp;Jack&quot;; &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;age&nbsp;=&nbsp;18; &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello,&nbsp;My&nbsp;name&nbsp;is&nbsp;%s.\r\nI&nbsp;am&nbsp;%d&nbsp;years&nbsp;old&nbsp;this&nbsp;year.\r\n&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;Nice&nbsp;to&nbsp;meet&nbsp;you.\r\nCan&nbsp;I&nbsp;make&nbsp;friends&nbsp;with&nbsp;you?\r\n&quot; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;I&nbsp;like&nbsp;programming&nbsp;in&nbsp;C&nbsp;very&nbsp;much.\r\n&quot;,&nbsp;name,&nbsp;age); &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(1); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; }</pre><p style="text-indent: 2em;">上述示例的运行结果如下:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2021.09.26/204931768947262.png" onclick="preview_image(&#39;/uploads/AilsonJack/2021.09.26/204931768947262.png&#39;)"/></p><p style="text-indent: 2em;">运行结果和长字符串未拆分时的效果一样。<br/></p><p style="text-indent: 2em;"><strong>方法二:利用反斜杠对长字符串进行换行</strong><br/></p><p style="text-indent: 2em;">可以使用反斜杠对长字符串进行拆分,反斜杠后的换行符会被C忽略,所以可以拆分字符串,但是下一行的空格会被计算在内,这反斜杠拆分字符串的一个问题吧,示例代码如下:</p><pre class="brush:cpp;toolbar:false PrismJs">/** &nbsp;*&nbsp;@file&nbsp;test.c &nbsp;*&nbsp;@author&nbsp;Ailson&nbsp;Jack&nbsp;(jackailson@foxmail.com) &nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blog:&nbsp;www.only2fire.com &nbsp;*&nbsp;@brief &nbsp;*&nbsp;@version&nbsp;1.0 &nbsp;*&nbsp;@date&nbsp;2021-09-25 &nbsp;* &nbsp;*&nbsp;@copyright&nbsp;Copyright&nbsp;(c)&nbsp;2021 &nbsp;* &nbsp;*/ #include&nbsp;&lt;stdio.h&gt; int&nbsp;main(void) { &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;name[]&nbsp;=&nbsp;&quot;Ailson&nbsp;Jack&quot;; &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;age&nbsp;=&nbsp;18; &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello,&nbsp;My&nbsp;name&nbsp;is&nbsp;%s.\r\nI&nbsp;am&nbsp;%d&nbsp;years&nbsp;old&nbsp;this&nbsp;year.\r\n\ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nice&nbsp;to&nbsp;meet&nbsp;you.\r\nCan&nbsp;I&nbsp;make&nbsp;friends&nbsp;with&nbsp;you?\r\n\ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I&nbsp;like&nbsp;programming&nbsp;in&nbsp;C&nbsp;very&nbsp;much.\r\n&quot;,&nbsp;name,&nbsp;age); &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(1); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; } /** &nbsp;*&nbsp;@file&nbsp;test.c &nbsp;*&nbsp;@author&nbsp;Ailson&nbsp;Jack&nbsp;(jackailson@foxmail.com) &nbsp;*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;blog:&nbsp;www.only2fire.com &nbsp;*&nbsp;@brief &nbsp;*&nbsp;@version&nbsp;1.0 &nbsp;*&nbsp;@date&nbsp;2021-09-25 &nbsp;* &nbsp;*&nbsp;@copyright&nbsp;Copyright&nbsp;(c)&nbsp;2021 &nbsp;* &nbsp;*/ #include&nbsp;&lt;stdio.h&gt; int&nbsp;main(void) { &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;name[]&nbsp;=&nbsp;&quot;Ailson&nbsp;Jack&quot;; &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;age&nbsp;=&nbsp;18; &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Hello,&nbsp;My&nbsp;name&nbsp;is&nbsp;%s.\r\nI&nbsp;am&nbsp;%d&nbsp;years&nbsp;old&nbsp;this&nbsp;year.\r\n\ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nice&nbsp;to&nbsp;meet&nbsp;you.\r\nCan&nbsp;I&nbsp;make&nbsp;friends&nbsp;with&nbsp;you?\r\n\ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;I&nbsp;like&nbsp;programming&nbsp;in&nbsp;C&nbsp;very&nbsp;much.\r\n&quot;,&nbsp;name,&nbsp;age); &nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(1); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;0; }</pre><p style="text-indent: 2em;">上述代码的运行结果如下图所示:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2021.09.26/204931049151245.png" onclick="preview_image(&#39;/uploads/AilsonJack/2021.09.26/204931049151245.png&#39;)"/></p><p style="text-indent: 2em;">运行结果和最终想要的结果还是有差异的,插入了一些不需要的空格,因为反斜杠拆分的字符串会把下一行的空格也计算在内。<br/></p><p class="artical_littlestyle3">3.总结</p><p style="text-indent: 2em;">对长字符串的书写换行,建议使用双引号进行拆分,这是最完美的,显示效果和最终想要的效果是一致的。<br/></p>
欢迎关注博主的公众号呀,精彩内容随时掌握:
热情邀请仔细浏览下博客中的广告,万一有对自己有用或感兴趣的呢。◕ᴗ◕。。
如果这篇文章对你有帮助,记得点赞和关注博主就行了^_^,当然了能够赞赏博主,那就非常感谢啦!
注: 转载请注明出处,谢谢!^_^
广告位C招租:2
广告位C招租:3
广告位D招租:1
广告位D招租:2
暂无评论,要不要来个沙发
发表评论

 
广告位E招租
Copyright © 2015~2021  说好一起走   保留所有权利   |  百度统计  蜀ICP备15004292号