标签: stdint

如何在C中打印int64_t类型

C99标准具有整数类型,字节大小类似于int64_t.我使用以下代码:

#include <stdio.h>
#include <stdint.h>
int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);
Run Code Online (Sandbox Code Playgroud)

我得到这个编译器警告:

warning: format ‘%I64d’ expects type ‘int’, but argument 2 has type ‘int64_t’
Run Code Online (Sandbox Code Playgroud)

我尝试过:

printf("This is my_int: %lld\n", my_int); // long long decimal
Run Code Online (Sandbox Code Playgroud)

但我得到同样的警告.我正在使用这个编译器:

~/dev/c$ cc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)
Run Code Online (Sandbox Code Playgroud)

我应该使用哪种格式打印my_int变量而不发出警告?

c stdint

270
推荐指数
5
解决办法
37万
查看次数

为什么每个人都会在标准C类型上输入类型?

如果你想使用Qt,你必须拥抱quint8,quint16等等.

如果你想使用GLib,你必须欢迎guint8,guint16等等.

Linux上有u32,s16等等.

uC/OS定义SINT32,UINT16等等.

如果你必须使用这些东西的某些组合,你最好为困难做好准备.因为在你的机器上u32将会typedef结束long并且quint32typedef结束int并且编译器会抱怨.

为什么每个人都这样做,如果有的话<stdint.h>?这是图书馆的某种传统吗?

c c++ stdint

103
推荐指数
3
解决办法
5727
查看次数

<cstdint> vs <stdint.h>

stdint.h和之间有什么区别cstdint

它们都可以在MSVC(Visual Studio 2010)和gcc-4.5.1中使用.还定义了intX_t/ uintX_ttypes(其中X是类型的字节大小).

  • 如果两个标题中的基本原理相同(便携式类型),我必须做出哪些决定来决定其中一个?

stdint.h定义了每个类型没有任何名称空间,该cstdint类型在于std命名空间.

  • 是否有任何理由在std命名空间中包含或不包含已定义的类型?这两个标题有什么不同?

cstdint没有文件扩展名并使用c前缀,stdint.h使用.h扩展名.

  • 此标头的命名约定是什么?该c前缀表示这是一个C库?缺少文件扩展的原因是cstdint什么?

c++ cstdint stdint

83
推荐指数
2
解决办法
5万
查看次数

stdint.h和inttypes.h之间的区别

stdint.h和inttypes.h有什么区别?

如果没有使用它们,则无法识别uint64_t,但使用其中任何一个都是定义的类型.

c uint64 stdint

68
推荐指数
2
解决办法
3万
查看次数

为什么数组的最大大小"太大"?

我和这个答案的印象相同,size_t标准总是保证足够大以容纳给定系统的最大可能类型.

但是,此代码无法在gcc/Mingw上编译:

#include <stdint.h>
#include <stddef.h>

typedef uint8_t array_t [SIZE_MAX];
Run Code Online (Sandbox Code Playgroud)

错误:数组'array_t'的大小太大

我在这里误解了标准中的某些内容吗?被size_t允许为给定的实现是否过大?或者这是Mingw的另一个错误?


编辑:进一步的研究表明

typedef uint8_t array_t [SIZE_MAX/2];   // does compile
typedef uint8_t array_t [SIZE_MAX/2+1]; // does not compile
Run Code Online (Sandbox Code Playgroud)

这恰好是相同的

#include <limits.h>

typedef uint8_t array_t [LLONG_MAX];           // does compile
typedef uint8_t array_t [LLONG_MAX+(size_t)1]; // does not compile
Run Code Online (Sandbox Code Playgroud)

所以我现在倾向于认为这是Mingw中的一个错误,因为根据有符号整数类型设置最大允许大小没有任何意义.

c gcc mingw size-t stdint

56
推荐指数
3
解决办法
9743
查看次数

ptrdiff_t在C中定义在哪里?

哪里是ptrdiff_t在C中规定?如果不重要,我怎样才能在Linux上从GCC看到这种类型?

c types libc stdint

53
推荐指数
1
解决办法
3万
查看次数

使用(或不使用)stdint的原因

我已经知道stdint用于当你需要特定的变量大小来实现平台之间的可移植性时,我现在还没有真正有这样的问题,但除了已经在上面显示的事实之外,使用它的缺点和优点是什么?

在stackoverflow和其他网站上寻找它,我找到了2个关于主题的链接:

  • 1 - 这个讨论了stdint的可移植性.

  • 2 - 这个更具体的关于uint8_t.

这两个链接非常好,特别是要了解更多关于这个标题的可移植性的主要原因,但对我来说,我最喜欢它,我认为uint8_t比unsigned char更清洁(例如存储RBG通道值) ,int32_t看起来比简单的int等更有意义.

所以,我的问题是,究竟是什么缺点,特别是除了可移植性之外使用stdint的优点,我应该只在代码的某些特定部分使用它,还是在任何地方?如果到处都是,我怎样才能使用atoi,strtok等功能呢?

谢谢!

c char stdint

46
推荐指数
4
解决办法
2万
查看次数

如何检查是否定义了固定宽度的整数

在 C++ 中,固定宽度整数被定义为optional,但我似乎无法找到推荐的方法来检查它们是否被实际定义。

检查固定宽度整数是否可用的便携式方法是什么?

c++ types cstdint stdint c++11

27
推荐指数
2
解决办法
1558
查看次数

stdint.h中这个神秘的宏加号是什么?

请看我的代码:

#include <stdint.h>

int main(int argc, char *argv[])
{
unsigned char s = 0xffU;
char ch = 0xff;
int val = 78;
((int8_t) + (78)); /*what does this mean*/

INT8_C(val);    /*equivalent to above*/

signed char + 78; /*not allowed*/

return 0;
}
Run Code Online (Sandbox Code Playgroud)

我发现宏定义<stdint.h>是:

#define INT8_C(val) ((int8_t) + (val))
Run Code Online (Sandbox Code Playgroud)

这个加号是什么意思或含义?

c macros stdint

19
推荐指数
3
解决办法
1501
查看次数

在C中定义16位整数

我需要在C中声明一个16位大小的整数.

我知道short和int size是依赖于机器的.

我尝试使用,"stdint.h",但似乎他们只是这样做

typedef short int16_t
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:

我错过了什么,短型保证16位长度?

如果不是,是否有替代方案可以保证呢?

c integer 16-bit stdint

17
推荐指数
2
解决办法
7万
查看次数

标签 统计

stdint ×10

c ×8

c++ ×3

cstdint ×2

types ×2

16-bit ×1

c++11 ×1

char ×1

gcc ×1

integer ×1

libc ×1

macros ×1

mingw ×1

size-t ×1

uint64 ×1