c - 在 C 中使用 typedef 作为无符号类型的快捷方式

标签 c cross-platform typedef

我正在用 C 编写一个 Chip-8 仿真器,我的目标是让它与尽可能多的新旧操作系统兼容多年来一直在添加,所以像这样的东西既可以创建快捷方式(这样我就不必写很多无符号字符/长整数)又可以解释已经定义了数字的编译器是否合理?如果没有,是否有更好/更有效的方法来做到这一点?

#ifdef __uint8_t_defined
    typedef uint8_t uchar;
    typedef int8_t schar;
    typedef uint16_t ushort;
    typedef int16_t sshort;
#else
    typedef unsigned char uchar;
    typedef signed char schar;
    typedef unsigned short ushort;
    typedef signed short sshort;
#endif

最佳答案

您不应该对原始类型的大小做出任何假设。甚至 char 也没有 8 位。检查此讨论:

What does the C++ standard state the size of int, long type to be?

我认为标准整数类型得到了很好的支持。如果您没有 stdint.h,那么您获得交叉兼容性的机会对我来说似乎非常渺茫。期望 stdint.h 可用于编译器似乎是一个合理的先决条件。

关于c - 在 C 中使用 typedef 作为无符号类型的快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40952356/

相关文章:

c - 未解析的外部符号 _malloc

linux - 各种Qt平台插件有什么用?

c - sscanf : get first and last token in a string

c - C 程序的 Makefile

c# - AXML 和 XAML 之间的区别?

c - 错误 [Pe028] : expression must have a constant value

c - 这个typedef是什么意思?

c++ - 常量表达式类型定义

c - 仅搜索目录

java - 在 Java 中从 Windows 操作系统到 Unix 的文件传输