字符串文字在哪里开始和结束? [英] Where does string-literal begin and end?

查看:96
本文介绍了字符串文字在哪里开始和结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C99标准表示,字符串文字的字符的实现限制为4095(?)。
但是,字面值到底从哪里开始呢?

The C99 standard says the implementation limit for characters of a string literal is 4095(?). But where exactly does a literal end and begin?

printf(
    "First part"
    "second part!\r\n"
    );

这将是单个字符串文字吗?还是这2个字符串文字?

Would this be a single string literal? Or are this 2 string literals?

推荐答案

N1256 5.2.4.1说:

N1256 5.2.4.1 says:



  • 4095个字符串文字或宽字符串文字中的字符(并置后)

连接后是指在翻译阶段6(5.1.1.2)中发生的相邻字符串文字的连接。

The "after concatenation" refers to the concatenation of adjacent string literals that occurs in translation phase 6 (5.1.1.2).

因此对于4095 -个字符的限制,即:

So for purposes of the 4095-character limit, this:

"First part"
"second part!\r\n"

是单个字符串文字。

注意那是最小的上限。实现必须允许长度不超过4095个字符的字符串文字;它们可以允许更长的时间,并且根本不需要施加任何固定的限制。

Note that that's a minimum upper bound. Implementations must permit string literals of any length up to 4095 characters; they may permit longer ones, and needn't impose any fixed limit at all.

实际上,这要复杂得多;该标准要求实现接受一个达到每个限制的翻译单元。但是实际上,除非程序遇到其他实现限制,否则您可以依赖任何符合C99的C99编译器来允许使用4095个字符的文字。

It's actually more complicated than that; the standard requires an implementation to accept one translation unit that hits each of the limits. But in practice, you can rely on any conforming C99 compiler to permit 4095-character literals, unless your program runs into some other implementation limit.

C11 。在C90中,它是509个字符。

The limit is the same in C11. In C90, it was 509 characters.

顺便说一句,没有充分的理由在中打印 \r\n printf 调用。 stdout 是文本流,这意味着'\n'换行符将转换为任何操作系统

Incidentally, there's unlikely to be any good reason to print "\r\n" in a printf call. stdout is a text stream, which means that the '\n' newline character will be translated to whatever the OS uses as an end-of-line indicator.

(N1256是C99之后的草案,由C99官方标准和其中包含的三个技术勘误组成。N1570是C11之前的最新草案,几乎与已发布的C11标准相同。官方标准不是免费提供的。)

(N1256 is a post-C99 draft, consisting of the official C99 standard with the three Technical Corrigenda incorporated into it. N1570 is the last pre-C11 draft, and is nearly identical to the released C11 standard. The official standards are not freely available.)

这篇关于字符串文字在哪里开始和结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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