在多行上定义一个字符串 [英] Defining a string over multiple lines

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

问题描述

请注意以下几点:

char buffer[512];

memset(buffer, 0, sizeof(buffer));
sprintf(&buffer[0],"This Is The Longest String In the World that in text goes on and..");

printf("Buffer:%s\r\n",buffer);

我希望能够在多行上创建此字符串,以便于故障排除和编辑.但是,当我使用 \ 命令时,我的输出被似乎是制表符的东西分隔了?

I would like to be able to create this string over multiple lines, for ease of troubleshooting and editing. However, when I use the \ command my output is separated by what appear to be tabs?

示例:

sprintf(&buffer[0],"This Is The\
    Longest String In the World\
    that in text goes on and..");

产生以下输出:

Buffer:This Is The        Longest String In the World       that in text goes on and..

有什么想法吗?这只是尝试在多行代码中拆分字符串的错误方法吗?

Any ideas? Is this just an incorrect approach to try to break up a string across multiple lines of code?

推荐答案

换行符继续考虑代码中的任何空格.

The newline continuation takes into account any whitespace within the code.

您可以利用字符串文字连接来提高可读性:

You can take advantage of string literal concatenation for better readability:

sprintf(buffer, "This Is The "
                "Longest String In the World "
                "that in text goes on and..");

使用 \ 您需要在第 0 列开始延续您的字符串:

Using \ you'll need to begin the continuation of your string at column 0:

sprintf(buffer, "This Is The \
Longest String In the World \
that in text goes on and..");

这篇关于在多行上定义一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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