我读了C99标准,它stdint.h是C标准库的一部分.
我是否正确阅读,如果我测试符合C99,请使用:
defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
Run Code Online (Sandbox Code Playgroud)
这意味着stdint.h应该可用吗?
一个C99很好的例子:我可以考虑一个假装符合要求但不提供stdint.h与自己的合规声明不一致的环境,因此有错误吗?
编辑:对于好奇的系统,有问题的系统是带有HP C编译器的OpenVMS(不是gcc,在openVMS上提供stdint.h).因此,根据到目前为止收到的答案和评论,我必须将此实现(假装为C99)视为错误.有关详情,请访问:https://groups.google.com/forum/#!topic/comp.os.vms/Bnh3tIOc7bo%5B101-125%5D
在Visual Studio 14该stdint.h头有固定宽度整数类型定义,但如果你真的看有定义,他们只是委托回原语。定义如下:
typedef signed char int8_t;
typedef short int16_t;
typedef int int32_t;
typedef long long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;
Run Code Online (Sandbox Code Playgroud)
那么,stdint.h如果它所做的只是回退到原始类型,是否有任何理由使用?我也知道Visual Studio不仅会在编译时替换这些定义,因为如果您尝试打印int8_t到控制台,则会得到一个Unicode字符而不是一个数字,因为它实际上只是一个signed char。
因为人们指出,他们没有逻辑上可以定义的其他内容,我认为我的问题需要重申。
为什么在C ++规范中标明它将具有8、16、32和64位固定长度的整数的标头将这些整数定义为类型,根据定义,该类型可以是编译器想要的任何大小(放入别人在另一个问题中说的方式The compiler can decide that an int will a 71 bit number stored in a 128 bit memory space where the additional 57 bits are used to store the …
我读到这stdint.h是用于便携性的,但我很困惑。
如果我在32位系统上编写程序,uint32_t(unsigned int)是4个字节。
但是当这个程序在16位系统上运行时,int是2bytes,uint32_t(unsigned int)是2bytes。
我认为在这种情况下无法保证可移植性。我理解有什么错误吗?
看代码:
#include <stdio.h>
#include <stdint.h>
int main() {
char foo[512]={};
printf("%d", *((uint32_t*)foo));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我很难理解它的*((uint32_t*)foo))作用,在数组中使用不同的值我得到各种返回值。它到底指向什么,那么返回值是什么呢?
考虑以下程序:
#include <stdio.h>
#include <stdint.h>
int main()
{
uint16_t result;
uint16_t ui = 1;
int16_t si = -1;
result = si * ui;
printf("%i", result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会打印值 65535,这是我阅读这篇文章后所期望的:si转换为ui,因此将 max+1 添加到其中。现在,在下一个代码片段中,我将结果类型更改为uint_fast16_t.
#include <stdio.h>
#include <stdint.h>
int main()
{
uint_fast16_t result;
uint16_t ui = 1;
int16_t si = -1;
result = si * ui;
printf("%li", result);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,结果是-1。这里会发生什么?如何签署结果?
我查看了<stdint.h>我的实现中的头文件.我看到以下内容:
typedef long int int_fast16_t;
typedef long int int_fast32_t;
typedef long int int_fast64_t;
Run Code Online (Sandbox Code Playgroud)
我有一个64位系统,所以long int占用64位.为什么所有三种数据类型都被定义为长整数?我理解int_fast64_t的情况,它是64位.但为什么16位和32位数据类型有64位?这是某种错误吗?我创建了一个小程序来检查是否是这种情况:
sizeof(int_fast8_t) : 1
sizeof(int_fast16_t) : 8
sizeof(int_fast32_t) : 8
sizeof(int_fast64_t) : 8
Run Code Online (Sandbox Code Playgroud)
是否定义了这些数据类型的大小?哪些特征或特征将数据类型定义为"快速"?是数据块从RAM加载到CPU的速度吗?如果int_fast16_t且int_fast32_t是8字节宽,性能有哪些好处?在64位系统上访问64位数据类型真的更快吗?