使用常量和使用gcc相关的修饰符 [英] Using constants and their associated modifiers using gcc

查看:330
本文介绍了使用常量和使用gcc相关的修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道怎么称呼这些标志,但是我指的是:

I was not sure what to call these flags, but what I am referring to is:

#define TEST_DEF 50000U //<- the "U" here

谷歌,当你不熟悉的用来描述你的问题是徒劳的行话搜索。

Google searching when you are not familiar with the jargon used to describe your question is futile.

我所试图做的是使用这些常量定义,并确保该值仅是一定的长度,即8位或16位的。

What I am trying to do is use these constant definitions and make sure the value is only of a certain length, namely 8 or 16 bits.

我怎样才能做到这一点,它是什么称?

How can I do this and what is it referred to as?

推荐答案

有五个的整数文字后缀的在C: U UL LL ULL 。不像用C几乎一切他们的区分大小写的;同时, UL ULL 可以写成 分别LLU(但是, LUL 是不能接受的)。

There are five integer literal suffixes in C: u, l, ul, ll, and ull. Unlike nearly everything else in C they are case insensitive; also, ul and ull can be written as lu and llu respectively (however, lul is not acceptable).

它们控制常数的类型。他们工作的的是这样的:

They control the type of the constant. They work approximately like this:

literal │ type
────────┼───────────────────────
500     │ int
500u    │ unsigned int
500l    │ long int
500ul   │ unsigned long int
500ll   │ long long int
500ull  │ unsigned long long int

这只是一个近似值,因为,如果常数是针对所指示的类型太大,它被提升到一个更大的类型。这个规则被充分地复杂,因为我不打算尝试来形容他们。为推广十六进制和八进制文字的规则比推广十进制文本的规则略有不同,他们也是C99 C90相比略有不同,不同又在C ++。

This is only an approximation, because if the constant is too large for the indicated type, it is "promoted" to a larger type. The rules for this are sufficiently complicated that I'm not going to try to describe them. The rules for "promoting" hexadecimal and octal literals are slightly different than the rules for "promoting" decimal literals, and they are also slightly different in C99 versus C90 and different again in C++.

由于的促进作用,是不可能使用这些后缀来限制常数的任何的尺寸。如果你写 281474976710656 的系统中, INT 都是32位宽,不断的将给予键入长长即使你没有说这样做。此外,没有后缀强制不断有键入也不字符。您可以指出您的意图的使用 [U] {INT 8,16,32,64,MAX} _C 宏从&LT; stdint.h&GT; ,但那些不强加任何上限任,并在所有系统上我可以现在(OSX和Linux), * INT8_C方便地获得 * INT16_C 实际产生与类型值(无符号)INT

Because of the promotion effect, is not possible to use these suffixes to limit constants to any size. If you write 281474976710656 on a system where int and long are both 32 bits wide, the constant will be given type long long even though you didn't say to do that. Moreover, there are no suffixes to force a constant to have type short nor char. You can indicate your intent with the [U]INT{8,16,32,64,MAX}_C macros from <stdint.h>, but those do not impose any upper limit either, and on all systems I can conveniently get at right now (OSX, Linux), *INT8_C and *INT16_C actually produce values with type (unsigned) int.

您编译器的可能的,但并不需要,如果你写警告((uint8_t有)512)或类似(其中512是一个编译时间常量类型的范围之外。在C11可以使用 static_assert (从&LT; ASSERT.H&GT; )来强制问题,但它可能是一个有点乏味写。

Your compiler may, but is not required to, warn if you write ((uint8_t) 512) or similar (where 512 is a compile-time constant value outside the range of the type. In C11 you can use static_assert (from <assert.h>) to force the issue but it might be a bit tedious to write.

这篇关于使用常量和使用gcc相关的修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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