是不是有语法错误?应的printf(QUOT;一种"&QUOT ;,两个""%S \\ n",",三个");有效code? [英] Isn't there a syntax error? Should printf("one" ", two and " "%s.\n", "three" ); be valid code?

查看:182
本文介绍了是不是有语法错误?应的printf(QUOT;一种"&QUOT ;,两个""%S \\ n",",三个");有效code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看看这个code:

#include <stdio.h>

#define _ONE "one"
#define _TWO_AND ", two and "
int main()
{
    const char THREE[6] = "three" ;
    printf(_ONE _TWO_AND "%s.\n", THREE );
    return 0;
}

的printf 是有效的:

printf("one" ", two and " "%s.\n", "three" );

和的输出是:

一个,两个和三个

GCC 编译此code后给出既不错误,也没有警告消息。
GCC 编译器应该工作的方式,或者是一个错误吗?

gcc gives neither error nor warning messages after compiling this code. Is the gcc compiler supposed work in that way, or is it a bug?

推荐答案

这是标准行为,相邻的字符串被连接到一起,如果我们看的 C99标准草案部分 5.1.1.2 翻译阶段的段落的 6 说:

This is standard behavior, adjacent string literals are concatenated together if we look at the C99 draft standard section 5.1.1.2 Translation phases paragraph 6 says:

相邻的字符串令牌被连接

Adjacent string literal tokens are concatenated

GCC 确实有很多不规范的扩展,但如果你建立一个使用 -pedantic 然后 GCC 要提醒你,如果它是做一些非标准的,你可以在文档部分阅读更多的扩展到C语言家族的。

gcc does have many non-standard extensions, but if you build using -pedantic then gcc should warn you if it is doing something non-standard, you can read more in the documents section Extensions to the C Language Family.

的理由是包括在理由国际标准-Programming语言-C 并在第 6.4.5 字符串字面的说道:

The rationale is covered in the Rationale for International Standard—Programming Languages—C and it says in section 6.4.5 String literals:

一个字符串,可以在多行用反斜杠换行续行继续,但是这需要该字符串的延续,在未来行的第一个位置开始。要允许更灵活的布局,并解决了一些preprocessing问题(见§6.10.3),该委员会C89引入了字符串串联。在连续两个字符串文字粘贴在一起,在中间没有空字符,使一个组合的字符串。这除了C语言允许程序员扩展字符串文字以外的物理行的末尾,而不必使用反斜杠换行机构并因此破坏了程序的缩进方案。显式连接运算符不出台,因为拼接是一个词法结构,而不是一个运行时操作。

A string can be continued across multiple lines by using the backslash–newline line continuation, but this requires that the continuation of the string start in the first position of the next line. To permit more flexible layout, and to solve some preprocessing problems (see §6.10.3), the C89 Committee introduced string literal concatenation. Two string literals in a row are pasted together, with no null character in the middle, to make one combined string literal. This addition to the C language allows a programmer to extend a string literal beyond the end of a physical line without having to use the backslash–newline mechanism and thereby destroying the indentation scheme of the program. An explicit concatenation operator was not introduced because the concatenation is a lexical construct rather than a run-time operation.

这篇关于是不是有语法错误?应的printf(QUOT;一种&QUOT;&QUOT ;,两个&QUOT;&QUOT;%S \\ n&QUOT;,&QUOT;,三个&QUOT;);有效code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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