在C89中是否有任何建立/标准化的方法来使用固定宽度的整数? [英] Are there any well-established/standardized ways to use fixed-width integers in C89?

查看:97
本文介绍了在C89中是否有任何建立/标准化的方法来使用固定宽度的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些背景:

标头 stdint.h 是C语言的一部分C99以来的标准.它包括确保有符号和无符号的8位,16位,32位和64位长整数的typedef.但是,此标头不是C89标准的一部分,而且我还没有找到任何直截了当的方式来确保我的数据类型具有已知的长度.

the header stdint.h is part of the C standard since C99. It includes typedefs that are ensured to be 8, 16, 32, and 64-bit long integers, both signed and unsigned. This header is not part of the C89 standard, though, and I haven't yet found any straightforward way to ensure that my datatypes have a known length.

进入实际主题

以下代码是SQLite(用C89编写)如何定义64位整数的方法,但我认为它没有说服力.也就是说,我认为它不会在所有地方都可以使用.最糟糕的是,它可能会默默地失败:

The following code is how SQLite (written in C89) defines 64-bit integers, but I don't find it convincing. That is, I don't think it's going to work everywhere. Worst of all, it could fail silently:

/*
** CAPI3REF: 64-Bit Integer Types
** KEYWORDS: sqlite_int64 sqlite_uint64
**
** Because there is no cross-platform way to specify 64-bit integer types
** SQLite includes typedefs for 64-bit signed and unsigned integers.
*/
#ifdef SQLITE_INT64_TYPE
  typedef SQLITE_INT64_TYPE sqlite_int64;
  typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
#elif defined(_MSC_VER) || defined(__BORLANDC__)
  typedef __int64 sqlite_int64;
  typedef unsigned __int64 sqlite_uint64;
#else
  typedef long long int sqlite_int64;
  typedef unsigned long long int sqlite_uint64;
#endif
typedef sqlite_int64 sqlite3_int64;
typedef sqlite_uint64 sqlite3_uint64;

所以,这是我到目前为止所做的:

So, this is what I've been doing so far:

  • Checking that the "char" data type is 8 bits long, since it's not guaranteed to be. If the preprocessor variable "CHAR_BIT" is not equal to 8, compilation fails
  • Now that "char" is guaranteed to be 8 bits long, I create a struct containing an array of several unsigned chars, which correspond to several bytes in the integer.
  • I write "operator" functions for my datatypes. Addition, multiplication, division, modulo, conversion from/to string, etc.

我已经将此过程抽象到头文件中,这是我所能做的最好的事情,但是我想知道是否有更简单的方法来实现此目的.

I have abstracted this process in a header file, which is the best I can do with what I know, but I wonder if there is a more straightforward way to achieve this.

我问是因为我想编写一个可移植的C库.

I'm asking because I want to write a portable C library.

推荐答案

首先,您应该问自己是否真的需要支持不提供<stdint.h>的实现.它在1999年进行了标准化,甚至许多C99之前的实现都可能将其作为扩展.

First, you should ask yourself whether you really need to support implementations that don't provide <stdint.h>. It was standardized in 1999, and even many pre-C99 implementations are likely to provide it as an extension.

假定您确实需要此文件,ISO C标准委员会成员Doug Gwyn为C9x(后来称为C99)创建了几个新标头的实现,这些标头与C89/C90兼容.标头位于公共领域,应具有合理的可移植性.

Assuming you really need this, Doug Gwyn, a member of the ISO C standard committee, created an implementation of several of the new headers for C9x (as C99 was then known), compatible with C89/C90. The headers are in the public domain and should be reasonably portable.

http://www.lysator.liu.se/(nobg)/c/q8/index.html

(据我了解,名称"q8"没有特殊含义;他只是选择它作为一个合理的简短且唯一的搜索词.)

(As I understand it, the name "q8" has no particular meaning; he just chose it as a reasonably short and unique search term.)

这篇关于在C89中是否有任何建立/标准化的方法来使用固定宽度的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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