是int x ='fooo'编译器扩展吗? [英] Is int x = 'fooo' a compiler extension?

查看:93
本文介绍了是int x ='fooo'编译器扩展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过并使用了如下的C ++代码:

I have seen and used C++ code like the following:

int myFourcc = 'ABCD';

它适用于最近的GCC版本,不知道最新版本。
标准中的这个功能吗?
它叫什么?

It works in recent versions of GCC, not sure how recent. Is this feature in the standard? What is it called?

我在搜索网络时遇到了麻烦...

I have had trouble searching the web for it...

EDIT:

我还为以后的观察者找到了此信息:

I found this info as well, for future observers:

gcc documentation


编译器一次为一个多字符字符常量赋值一个字符,将先前的值向左移动每个目标字符的位数,然后 - 将新字符的位模式截断为目标字符的宽度。最终的位模式是int类型,因此无论是单字符是否带符号,都是带符号的(与GCC版本3.1和更早版本略有不同)。如果常量中有更多的字符会比目标int更合适,编译器会发出警告,并忽略超出的前导字符。

The compiler values a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character. The final bit-pattern is given type int, and is therefore signed, regardless of whether single characters are signed or not (a slight change from versions 3.1 and earlier of GCC). If there are more characters in the constant than would fit in the target int the compiler issues a warning, and the excess leading characters are ignored.

例如,'ab'对于具有8位字符的目标,将被解释为(int)((unsigned char)'a'* 256 +(unsigned char)'b')'和'\234a' (int)((unsigned char)'\234'* 256 +(unsigned char)'a')'。

For example, 'ab' for a target with an 8-bit char would be interpreted as (int) ((unsigned char) 'a' * 256 + (unsigned char) 'b')', and '\234a' as (int) ((unsigned char) '\234' * 256 + (unsigned char) 'a')'.


推荐答案

请注意,根据C标准,字符常量,但是包含多个字符的字符常量的值是实现定义的。最新版本的GCC提供支持多字节字符常量,而不是错误警告多字符字符常量<在此情况下会生成警告:字符常数太长的类型。

"Note that according to the C standard there is no limit on the length of a character constant, but the value of a character constant that contains more than one character is implementation-defined. Recent versions of GCC provide support multi-byte character constants, and instead of an error the warnings multiple-character character constant or warning: character constant too long for its type are generated in this case."

这篇关于是int x ='fooo'编译器扩展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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