为int x ='fooo'一个编译器扩展? [英] Is int x = 'fooo' a compiler extension?

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

问题描述

我所看到的和所用C ++ code这样的:

  INT myFourcc ='ABCD';

它的工作原理在最近版本的GCC,不知道怎么最近。
这是功能的标准呢?
什么叫?

我有麻烦在网上搜索吧...

编辑:

我发现这个信息,以及为未来的观察员:

从<一个href=\"http://gcc.gnu.org/onlinedocs/cpp/Implementation%5F002ddefined-behavior.html#Implementation%5F002ddefined-behavior\"相对=nofollow称号=GCC文档> GCC文档


  

编译器在一个时间值的多字符字符常量的字符,移由每个目标人物的位数离开了previous值,然后或-ING在截断新字符的位模式到的目标字符的宽度。最终位模式给出int类型,并因此被签名,而不管单个字符是否签署与否(从3.1版本的微小变化和更早的GCC)。如果有更多的字符的恒定比将适合在目标的int编译器将发出一个警告,和过量的领先字符被忽略。


  
  

例如,'AB'与8位字符的目标将是PTED为间$ P $(INT)((unsigned char型)'A'* 256 +(无符号字符) 'b')'和'\\ 234A为(INT)((unsigned char型)'\\ 234'* 256 +(无符号字符)'A')。



解决方案

请注意,根据C标准上有一个字符常量的长度没有限制,但包含多个字符的字符常量的值为实现定义GCC的最新版本提供了支持多字节字符常量,而不是错误的警告的多字符字符常量的或的警告:字符常量太长,其类型的是在这种情况下产生的。

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

int myFourcc = 'ABCD';

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:

from gcc documentation

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.

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')'.

解决方案

"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天全站免登陆