- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果我希望所有包含 proto.h 的 *.c 文件都使用 int32_t
而不是 int
将其写入名为的头文件是否正确proto.h
:
#ifndef PROTO_H_INCLUDED
#define PROTO_H_INCLUDED
#ifndef STDINT_H_INCLUDED
#define STDINT_H_INCLUDED
typedef int int32_t;
typedef unsigned int uint32_t;
typedef size_t uint32_t;
#endif
然后将 proto.h 包含到所有需要此 typedef
的 *.c 文件中?
或者我应该将 stdint.h 包含到我所有的 *.c 文件中吗?
最佳答案
这是正确的,但由于多种原因,这不是最佳解决方案。
stdint.h
中。uint32_t
,他们希望它是任何架构上的 32 位 unsigned int;这将是一个很难追踪的错误。proto.h
文件的用户并不清楚它包含 stdint.h。有人会说您应该包含尽可能少的文件;在我看来,更重要的是要清楚。删除用户 C 文件中的 proto.h
包含应该只需要删除对其中声明的函数的引用,而不是添加 stdint.h 的包含。为清楚起见,您应该将其添加到 .c
文件中,他们也会这样做。出于这些原因,我建议在任何需要来自另一个头文件的定义的头文件中(例如,要在函数原型(prototype)中使用宏或 typedef),您应该按如下方式构建文件:
proto.h
#ifndef PROTO_H_INCLUDED
#define PROTO_H_INCLUDED
// Typedefs for prototypes
#include <stdint.h>
unit32_t proto(int32_t *value, size_t length);
#endif
proto.c
#include <stdint.h>
#include "proto.h" // Forward declare functions in this file
unit32_t proto(uint32_t *value, size_t length)
{
// Do something
}
main.c
#include <stdint.h>
#include "proto.h"
int main(int argc, char *argv[])
{
uint32_t values[] = { 1, 2, 3 };
uint32_t result;
// Could do 'uint32_t result, values[] = { 1, 2, 3 };' (one line)
// but this is better for clarity
size_t len = sizeof(values) / sizeof(values[0]);
proto(values, len);
}
关于c - 如何以及在何处将 stdint.h 类型定义包含在头文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347879/
是否有任何理由导致下面的代码不起作用? (编译为 C 代码) 代码 #include int main() { int var = 10; if (var == 0) return
所以我正在用 C++ 编程,据我所知,没有与 stdint.h 等效的 C++。这没问题,因为您只需获取 stdint 的拷贝并将其包含在内……但我的问题基本上是这样的, 这两段代码有什么区别: st
由于 C 是一种松散类型的语言并且 stdint.h 只定义了 typedefs(我假设),如何保证 int 的宽度? 我要问的是实现而不是库的使用。 最佳答案 How can types guar
在 Linux 上进行 C++ 编程时应该使用哪些类型?使用 stdint.h 中的类型是个好主意,例如 int16_t 和 uint8_t? 一方面,stdint.h 肯定无法在 Windows 上
我正在处理遗留的嵌入式 C 代码,这些代码使用 typedef 关键字在头文件中定义类型 uint8_t、uint16_t 和 uint32_t . 为了便于讨论,假设文件 typedefs.h 包含
请看我的代码: #include int main(int argc, char *argv[]) { unsigned char s = 0xffU; char ch = 0xff; int va
对于 stdint.h,我必须引用此页面: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdint.h.html 我完全惊讶地发
问题是在一个平台(windows,mvsc2015)上 uint64_t 被定义为 unsigned long long 而在另一个平台(ubuntu,clang)上它是 unsigned long
我正在处理一个专为 iPhone OS 2.0 设计的项目,我打算在提供新的 OS 3.x 功能的同时保持与此版本的兼容性。 当我将基础 SDK 设置为 iPhone OS 3.1.3 并将目标操作系
这个问题在这里已经有了答案: Is there a Java library for unsigned number type wrappers? [closed] (5 个答案) 关闭 9 年前。
我正在尝试使用 Symbian Belle Sdk 为 Pys60 2.0 编译 PyS60USB,但由于一些看似无关的原因,我收到了关于几乎所有未命名类型的看似随机的错误,如下所示: * C:/No
C99 和 C++11(以及之前的 POSIX)在 stdint header 中引入了 least 和 fast 类型,例如int_fast16_t 或 uint_fast8_t。 我想知道这些类型
我在 C99 standard 中读到stdint.h 是 C 标准库的一部分。 我没看错吗,如果我测试 C99 合规性,使用: defined (__STDC_VERSION__) && (__ST
当我将 Apportable 用于我的 C++ 源代码时,它找不到在 stdint.h 中定义的符号. error: use of undeclared identifier 'UINT8_MAX'
我已经知道 stdint 用于当您需要特定的变量大小以实现平台间的可移植性时。我现在真的没有这样的问题,但除了上面已经显示的事实之外,使用它的利弊是什么? 在 stackoverflow 和其他网站上
我希望 char、short 和 int 类型的宽度为 1、2 和 4 字节。我在源代码中包含了一个 stdint.h header 。这是否保证 int8_t、int16_t 和 int32_t 整
stdint.h 定义具有指定宽度的整数类型。我们什么时候应该使用这些类型,例如 uint32_t 而不是 unsigned int?是因为我们可以在不考虑底层机器的情况下获得想要的类型吗? 最佳答案
如果我希望所有包含 proto.h 的 *.c 文件都使用 int32_t 而不是 int 将其写入名为的头文件是否正确proto.h: #ifndef PROTO_H_INCLUDED #defin
我注意到,在使用 native 标量类型(整数、短整型、字符)或 stdint 提供的标量类型时似乎没有一致性或最佳实践:uint32_t uint16_t uint8_t。 这让我很烦恼,因为驱动程
stdint.h和cstdint有什么区别? 它们都在 MSVC (Visual Studio 2010) 和 gcc-4.5.1 中可用。还都定义了 intX_t/uintX_t 类型(其中 X 是
我是一名优秀的程序员,十分优秀!