关于C/C++的换行

可以分为两种情况:

  • 代码换行
  • 长字符串换行

一、 C/C++代码换行直接用enter键换行,同时注意关键字不要切割:

bool a=true;
if(a
==
true)
{
    std::cout<<"Hello"<<std::endl;
}//ok,关键字没有切割
if(a
=
=
true)
{
    std::cout<<"Hello"<<std::endl;
}//error,切分了关键字==

下面可以但是最好不要这么做的用法(可读性):

void fun\
ction()
{   
    std::cout<<"I am function"<<std::endl;
}

宏函数倒是挺常见的:

#define MAX(a,b)\
    ((a)>(b)?(a):(b))

二、长字符串换行

两种方式实现长字符串在编辑区换行。

2.1 双引号

将一个大字符串拆分成属于不同行的、引号字符串。

const char *text =
  "This text is pretty long, but will be "
  "concatenated into just a single string. "
  "The disadvantage is that you have to quote "
  "each part, and newlines must be literal as "
  "usual.";

这种情况下,每一行右侧引号左侧的缩进无需考虑,因为它不属于这个长字符串。

2.2 反斜杠

const char *text2 =
  "Here, on the other hand, I've gone crazy \
and really let the literal span several lines, \
without bothering with quoting each line's \
content. This works, but you can't indent.";

因为字符串是一个整体,所以必须要用反斜杠\

2.3 Raw-string支持

在C++11以后,支持了一种所见即所得的一种方式。

    const char * vogon_poem = R"(
             O freddled gruntbuggly thy micturations are to me
                 As plured gabbleblochits on a lurgid bee.
              Groop, I implore thee my foonting turlingdromes.   
           And hooptiously drangle me with crinkly bindlewurdles,
Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.
)";

    std::cout<<vogon_poem<<std::endl;

[1] https://stackoverflow.com/questions/1135841/c-multiline-string-literal

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值