C语言中反斜杠的作用

来源:互联网 发布:三菱plc编程软件安卓版 编辑:程序博客网 时间:2024/04/30 11:11

反斜杠起到换行作用,用于宏定义和字符串换行。其中宏定义中使用居多。

如果一行代码有很多元素,导致太长影响阅读,可以通过在结尾加\的方式,实现换行,编译时会忽略\及其后的换行符,当做一行处理。

1、在宏定义中,要换行必须使用 \ 结尾。

#define CHECK_ACTION_RETURN(expr) \    if (!expr) { \        printf(":failed(%d)\n", ret); \        return ret; \                } else { \        printf(":ok\n"); \                }

2、在字符串常量中,可以使用 \ 结尾。

"this \is \for \testing"

和”this is for testing”是相同的,但是对于字符串写成

"this ""is ""for ""testing"

效果是相同的,而且更美观。

3、另外,在普通的语句中,也可以通过 \ 实现换行,不过这时没有 \ 也是一样的效果
比如

printf("this is for test %d %d %d\n",\test_output_a,\test_output_b,\test_output_c);

printf("this is for test %d %d %d\n",test_output_a,test_output_b,test_output_c);

是没有区别的,所以这时一般不会使用\。

0 0
原创粉丝点击