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变量而不发出警告?
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什么?stdint.h和inttypes.h有什么区别?
如果没有使用它们,则无法识别uint64_t,但使用其中任何一个都是定义的类型.
我和这个答案的印象相同,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中的一个错误,因为根据有符号整数类型设置最大允许大小没有任何意义.
哪里是ptrdiff_t在C中规定?如果不重要,我怎样才能在Linux上从GCC看到这种类型?
我已经知道stdint用于当你需要特定的变量大小来实现平台之间的可移植性时,我现在还没有真正有这样的问题,但除了已经在上面显示的事实之外,使用它的缺点和优点是什么?
在stackoverflow和其他网站上寻找它,我找到了2个关于主题的链接:
这两个链接非常好,特别是要了解更多关于这个标题的可移植性的主要原因,但对我来说,我最喜欢它,我认为uint8_t比unsigned char更清洁(例如存储RBG通道值) ,int32_t看起来比简单的int等更有意义.
所以,我的问题是,究竟是什么缺点,特别是除了可移植性之外使用stdint的优点,我应该只在代码的某些特定部分使用它,还是在任何地方?如果到处都是,我怎样才能使用atoi,strtok等功能呢?
谢谢!
在 C++ 中,固定宽度整数被定义为optional,但我似乎无法找到推荐的方法来检查它们是否被实际定义。
检查固定宽度整数是否可用的便携式方法是什么?
请看我的代码:
#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中声明一个16位大小的整数.
我知道short和int size是依赖于机器的.
我尝试使用,"stdint.h",但似乎他们只是这样做
typedef short int16_t
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
我错过了什么,短型保证16位长度?
如果不是,是否有替代方案可以保证呢?