C99的固定宽度整数类型 [英] C99′s Fixed-Width Integer Types

查看:326
本文介绍了C99的固定宽度整数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

未能详细解答我的问题此处。我想我会从不同的角度解决它。

Failing to get a detailed answer to my question here. I thought I would tackle it from a different angle.

是否有人能够解释用于确定C99固定宽度整数类型的基础类型的选择标准:

Would someone be able to explain what selection criteria are used for determining the underlying types for C99's fixed-width integer types:

[u]int_fast[n]_t
[u]int_least[n]_t
[u]int[n]_t

对于给定的处理器,如果'long'和'int'是相同的大小(sizeof(int)== sizeof(long))然后为什么'long'将被用于'int',反之亦然。

For a given processor, if 'long' and 'int' are the same size (sizeof(int) == sizeof(long)) then why would 'long' be used over 'int' or vice versa.

推荐答案

< stdint.h> 的作者的心血来潮。

鉴于 int long 是相同的大小(假设它们具有相同的表示和其他特征),它应该无关紧要所有这些都用于定义 [u] int _ {,_ fast,_least} 32_t ,只要一个或多个类型满足标准的要求。

Given that int and long are the same size (and assuming they have the same representation and other characteristics), it shouldn't matter at all which of them is used to define [u]int_{,_fast,_least}32_t, as long as the type or types satisfy the requirements of the standard.

嗯,那不是相当是真的;它可以在某些情况下有所作为。 int long ,即使它们的大小相同,仍然是不同且不兼容的类型。例如,假设 int32_t typedef ed为 int long ,以下程序:

Well, that's not quite true; it can make a difference in some cases. int and long, even if they're the same size, are still distinct and incompatible types. For example, given that int32_t is typedefed either as int or as long, the following program:

#include <stdint.h>
#include <stddef.h>
int main(void) {
    int32_t *p32 = NULL;
    int  *ip = p32;
    long *lp = p32;
    return  0;
}

违反约束并需要诊断 on在 lp 的声明中声明 ip ,具体取决于<$ c的方式$ c> int32_t 已定义。但是你应该避免编写这样的代码。

violates a constraint and requires a diagnostic either on the declaration of ip or on the declaration of lp, depending on how int32_t is defined. But you should avoid writing such code anyway.

这篇关于C99的固定宽度整数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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